using System;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
 
namespace Server
{
	public static class PlayerDeathManager
	{
#if Framework_4_0
		private static bool Processing { get; set; }
		private static Timer DeathTicker { get; set; }

		public static bool DeleteOnCorpseCarved { get; set; }
		public static List<PlayerMobile> DeathRegister { get; private set; }
#else
		private static bool Processing;
		private static Timer DeathTicker;

		private static List<PlayerMobile> _DeathRegister;
		public static List<PlayerMobile> DeathRegister { get { return _DeathRegister; } set { _DeathRegister = value; } }

		private static bool _DeleteOnCorpseCarved;
		public static bool DeleteOnCorpseCarved { get { return _DeleteOnCorpseCarved; } set { _DeleteOnCorpseCarved = value; } }
#endif

		public static void Configure()
		{
			DeathRegister = new List<PlayerMobile>();
			DeleteOnCorpseCarved = true;
		}

		public static void Initialize()
		{
			DeathTicker = Timer.DelayCall(TimeSpan.Zero, TimeSpan.FromSeconds(1.0), DeathOnTick);
			EventSink.PlayerDeath += OnPlayerDeath;
		}

		private static void DeathOnTick()
		{
			if (Processing)
				return;

			Processing = true;

			foreach (PlayerMobile dead in DeathRegister.ToArray())
			{
				if (dead.Deleted || dead.Alive)
				{
					if (dead.Corpse != null)
						dead.Corpse.Delete();

					DeathRegister.Remove(dead);
					continue;
				}

				Corpse corpse = dead.Corpse as Corpse;

				if (corpse != null && !corpse.Deleted && corpse.Carved && DeleteOnCorpseCarved)
				{
					dead.SendMessage("Someone carved your remains into tiny, unrecognisable pieces. You are dead.");
					Timer.DelayCall(TimeSpan.FromSeconds(5), DeletePlayer, dead);
					DeathRegister.Remove(dead);
				}
			}

			Processing = false;
		}

		private static void OnPlayerDeath(PlayerDeathEventArgs e)
		{
			PlayerMobile dead = e.Mobile as PlayerMobile;

			if (dead == null || dead.Deleted || dead.Alive)
				return;

			if (!DeathRegister.Contains(dead))
				DeathRegister.Add(dead);

			dead.Frozen = true;

			if (dead.LastKiller == null || dead.LastKiller is BaseCreature)
			{
				dead.SendMessage("You are dead.");
				Timer.DelayCall(TimeSpan.FromSeconds(60), DeletePlayer, dead);
			}
			else
			{
				dead.SendMessage("You are dead.");
				Timer.DelayCall(TimeSpan.FromSeconds(60), DeletePlayer, dead); // you use DeletePlayer instead DeleteCorpseAndResurrect for instant PVP Kill
			}
		}

		private static void DeletePlayer(Mobile dead)
		{
			if (dead == null || dead.Deleted)
				return;

			dead.Delete();
		}

		private static void DeleteCorpseAndResurrect(Mobile dead)
		{
			if (dead == null || dead.Deleted)
				return;

			if (dead.Corpse != null)
			{
				dead.Corpse.Delete();
				dead.SendMessage("Your corpse has decayed.");
			}

			if (!dead.Alive)
			{
				dead.Resurrect();
				dead.Frozen = false;
				dead.SendMessage("You are back on your feet.");
			}
		}
	}
}