//////////////////////////
//Created by LostSinner//
////////////////////////
using System;
using Server.Items;

namespace Server.Items
{
    public class DracsKey : Item
    {
        private int m_Lifespan;
        private Timer m_Timer;

        [CommandProperty(AccessLevel.GameMaster)]
        public int Lifespan
        {
            get { return m_Lifespan; }
            set { m_Lifespan = value; InvalidateProperties(); }
        }

        [Constructable]
        public DracsKey()
            : base(0x176B)
        {
            Weight = 1.0;
            Name = "Key to Draculas Chamber";
            LootType = LootType.Blessed;
            Hue = 1153;
            m_Lifespan = 172800;

            if (m_Lifespan > 0 && m_Timer == null)
                StartTimer();
        }

        public override void GetProperties(ObjectPropertyList list)
        {
            base.GetProperties(list);
            if (m_Lifespan > 0)
                list.Add(1072517, m_Lifespan.ToString()); // Lifespan: ~1_val~ seconds
        }

        public DracsKey(Serial serial)
            : base(serial)
        {
        }

        public override void Serialize(GenericWriter writer)
        {
            base.Serialize(writer);
            writer.Write((int)1); // version

            writer.Write((int)m_Lifespan);
        }

        public override void Deserialize(GenericReader reader)
        {
            base.Deserialize(reader);
            int version = reader.ReadInt();

            switch (version)
            {
                case 1:
                    {
                        m_Lifespan = reader.ReadInt();
                        goto case 0;
                    }
                case 0:
                    {
                        break;
                    }
            }

            if (version > 0)
                StartTimer();
        }

        public override void OnAfterDelete()
        {
            StopTimer();
            base.OnAfterDelete();
        }

        #region lifespan
        public virtual void StartTimer()
        {
            if (m_Timer != null)
                return;

            m_Lifespan = Lifespan;
            m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), new TimerCallback(Slice));
            m_Timer.Priority = TimerPriority.OneSecond;
        }

        public virtual void StopTimer()
        {
            if (m_Timer != null)
                m_Timer.Stop();

            m_Timer = null;
        }

        public virtual void Slice()
        {
            m_Lifespan -= 10;

            InvalidateProperties();

            if (m_Lifespan <= 0)
                Decay();
        }

        public virtual void Decay()
        {
            if (RootParent is Mobile)
            {
                Mobile parent = (Mobile)RootParent;

                if (Name == null)
                    parent.SendLocalizedMessage(1072515, "#" + LabelNumber); // The ~1_name~ expired...
                else
                    parent.SendLocalizedMessage(1072515, Name); // The ~1_name~ expired...

                Effects.SendLocationParticles(EffectItem.Create(parent.Location, parent.Map, EffectItem.DefaultDuration), 0x3728, 8, 20, 5042);
                Effects.PlaySound(parent.Location, parent.Map, 0x201);
            }
            else
            {
                Effects.SendLocationParticles(EffectItem.Create(Location, Map, EffectItem.DefaultDuration), 0x3728, 8, 20, 5042);
                Effects.PlaySound(Location, Map, 0x201);
            }

            StopTimer();
            Delete();
        }
        #endregion
    }
}