/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Sep-24-2011 10:26:41pm
Filepath: scripts\Items\Artifacts\leviathan\MagicKey.cs
Lines of code: 135

Description: 

***********************************************************/


using System;
using Server.Network;
using Server;
using Server.Targeting; 
using Server.Items;
using Server.Mobiles;
using Server.Targets;

namespace Server.Items
{

	
	public class MagicKey : Item, IUsesRemaining
	{	
		private int m_UsesRemaining;
		public bool m_ShowUsesRemaining{ get{ return true; } set{} }
		private DateTime m_lastrecharge;
		
		
		

		[CommandProperty( AccessLevel.GameMaster )]
		public int UsesRemaining
		{
			get { return m_UsesRemaining; }
			set { m_UsesRemaining = value; InvalidateProperties(); Modify(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public bool ShowUsesRemaining
		{
			get { return m_ShowUsesRemaining; }
			set { m_ShowUsesRemaining = value; InvalidateProperties(); Modify(); }
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime LastRecharge
		{
			get { return m_lastrecharge; }
			set { m_lastrecharge = value; Modify(); }
		}		

		[Constructable]
		public MagicKey() : this( 10 )
		{
		}

		[Constructable]
		public MagicKey( int uses ) : base( 4111 )
		{
			Weight = 1.0;
			Hue = 1153;
			UsesRemaining = uses;
			
			Name = "Skeleton Key of Davy Jones";
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( !IsChildOf( from.Backpack ) )
			{
				from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
			}
			else
			{
				if(DateTime.Now > (LastRecharge + TimeSpan.FromHours(24)))
				{
					LastRecharge=DateTime.Now;
					UsesRemaining=11;
				}
				
				if (this.UsesRemaining > 0)
				{
					from.Target = new KeyTarget( this );
					from.SendMessage( "Which chest do you wish to unlock" );
				}
				else
				{
					from.SendMessage("You will need to wait up to 24 hours to use this item again");
				}
			}					
		}
		
		private class KeyTarget : Target 
		{ 
			private MagicKey m_Key;

			public KeyTarget( MagicKey knife ) : base( 15, false, TargetFlags.None )
			{
				m_Key = knife;
			}
			
			protected override void OnTarget( Mobile from, object target ) 
			{ 
				if ( target is LockableContainer ) 
				{	
					LockableContainer c = (LockableContainer)target; 
					
					if (c.Locked)
					{
						if (m_Key != null )
						{

							if (m_Key.UsesRemaining > 0)
							{
								m_Key.UsesRemaining--; 
								from.SendMessage("Your key magically unlocks the chest.");
								from.PlaySound( 0x4A );
								from.PlaySound( 0x1FF );
								c.Locked = false; 
							}
						}
					}
					else
					{
						from.SendMessage("That item is already unlocked");
					}
				}
				else
				{
					from.SendMessage("You cannot unlock that item");
				}
			}
		}
		
		public MagicKey( Serial serial ) : base( serial )
		{
		}
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
			
			writer.Write( (bool) m_ShowUsesRemaining );

			writer.Write( (int) m_UsesRemaining );
			
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			m_ShowUsesRemaining = reader.ReadBool();
			m_UsesRemaining = reader.ReadInt();
			if ( m_UsesRemaining < 1 )
				m_UsesRemaining = 50;
		}
		
		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			list.Add( 1060584, m_UsesRemaining.ToString() ); // uses remaining: ~1_val~
		}
		
		public override void OnAfterLoad()
		{
			Misc.ObjectConverter.ConvertItem(this,new SkeletonKeyOfDavyJones());
		}		
		
	}
	

	
	
}