/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\ML Peerless + misc areas\zzz-items\baseexpirekey.cs
Lines of code: 132
***********************************************************/


using System;
using Server;
using Server.Mobiles;

namespace Server.Items
{

	public class baseexpirekey : Item
	{
	
		public override TimeSpan DecayTime
		{
			get
			{
				return TimeSpan.FromDays(1);
			}
		}	
	
	//to serialize
	private TimeSpan m_expiretime;            //time till it expires
	private DateTime m_activatedate;         //date it was activated (ex: looted)

	
	//set/get
	
		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime ExpireDate
		{
			get
			{
				return m_activatedate+m_expiretime;
			}
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime ActivateDate
		{
			get
			{
				return m_activatedate;
			}
		}		

		[CommandProperty( AccessLevel.GameMaster )]
		public TimeSpan ExpireTime
		{
			get
			{
				return m_expiretime;
			}
			set
			{
				m_expiretime = value;Modify();
			}
		}					
	
		[Constructable]
		public baseexpirekey(string name, int itemid,int hue) : base( itemid )
		{
			Name=name;
			m_expiretime=TimeSpan.FromHours(1.5); //default expiary
			Hue=hue;
			
			Stackable = false;
			Weight=1;
			
			LootType=LootType.Blessed;
			
			m_activatedate=DateTime.Now;
			new InternalTimer(this).Start();			
		}
		
		[Constructable]
		public baseexpirekey(string name, int itemid,int hue,TimeSpan expire) : base( itemid )
		{
			Name=name;
			m_expiretime=expire; //default expiary of 10 hours
			Hue=hue;
			
			Stackable = false;
			Weight=1;
			
			LootType=LootType.Blessed;
			
			m_activatedate=DateTime.Now;
			new InternalTimer(this).Start();			
		}		
		
		
        public override void GetProperties( ObjectPropertyList list )
        {
            base.GetProperties( list );
			
			TimeSpan TLifeSpan = (m_activatedate+m_expiretime)-DateTime.Now;
			
			int LifeSpan=(int)TLifeSpan.TotalSeconds;			
			
            list.Add( 1072517, LifeSpan.ToString() ); // Lifespan: ~1_val~ seconds
        }		
		
		
		
		
	
		
		public baseexpirekey( Serial serial ) : base( serial )
		{
		}
		
		
		
		
		public bool DoExpire()
		{
			if(m_expiretime==TimeSpan.Zero)return false;
		
			if(m_activatedate+m_expiretime<DateTime.Now)
			{
				if(RootParentEntity!=null && RootParentEntity is PlayerMobile)
				{
					PlayerMobile pm = RootParentEntity as PlayerMobile;
					pm.SendMessage("{0} has expired!",Name);
				}
				
				Delete();
				return true;
			}
		
		return false;
		}
		
	
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );
			
			writer.Write(m_expiretime);
			writer.WriteDeltaTime(m_activatedate);			
		}
		
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			
			m_expiretime = reader.ReadTimeSpan();
			m_activatedate = reader.ReadDeltaTime();			
			
			if(!DoExpire())//start timer if we need to
			{
			new InternalTimer(this).Start();			
			}
		}
		
		
		
		
		
		
		
	//start internal timer		
	public class InternalTimer : Timer
	{
	baseexpirekey m_key;
	
		public InternalTimer(baseexpirekey key) : base( TimeSpan.FromSeconds( 0 ), TimeSpan.FromSeconds( 5.0 ) )
		{
		m_key=key;
		
		Priority = TimerPriority.FiveSeconds;
		}	
		
		
		protected override void OnTick()
		{
		if(m_key==null || m_key.Deleted || m_key.m_expiretime==TimeSpan.Zero)Stop();
		
		m_key.InvalidateProperties();
		
			if(m_key.m_activatedate+m_key.m_expiretime<DateTime.Now)
			{
				m_key.DoExpire();
				Stop();
			}		
	
		}
		

		
	}
	//end internal timer		
		
		
		
		
		
		
		
	}

}


