using System;
using Server.Network;
using Server.Targeting;
using Server.Items;
using Server.Regions;

namespace Server.Items
{
	public class SkeletonKeys : Item
	{
		[Constructable]
		public SkeletonKeys() : base( 0x176B )
		{
			Name = "skeleton keys";
			Uses = Utility.RandomMinMax( 3, 10 );
			UsesMax = Uses;
		}

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

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

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

		public override void OnDoubleClick( Mobile from )
		{
			from.SendMessage( "What do you want to the keys on?" );
			from.Target = new InternalTarget( this );
		}

		public override void AddNameProperty( ObjectPropertyList list )
		{
			base.AddNameProperty( list );

			if ( Uses > 0 )
			{
				if ( Uses > 1 )
					list.Add( 1062518, "{0}", Uses );
				else
					list.Add( 1062519, "{0}", Uses );
			}
		}

		private class InternalTarget : Target
		{
			private SkeletonKeys m_Item;

			public InternalTarget( SkeletonKeys item ) : base( 1, false, TargetFlags.None )
			{
				m_Item = item;
			}

			protected override void OnTarget( Mobile from, object targeted )
			{
				if ( m_Item.Deleted )
					return;

				if ( targeted is LootChest )
					((LootChest)targeted).Setup();

				if ( targeted is BaseDoor && from.Region is DungeonRegion )
				{
					BaseDoor door = (BaseDoor)targeted;

					if ( door.Sealed > DateTime.Now )
					{
						from.PlaySound( 0x241 );
						door.Sealed = DateTime.MinValue;
						door.SendLocalizedMessageTo( from, 502076 );
					}
					else
					{
						from.SendLocalizedMessage( 502069 ); // This does not appear to be locked
					}
				}
				else if ( targeted is ILockpickable )
				{
					Item item = (Item)targeted;
					from.Direction = from.GetDirectionTo( item );

					if ( ((ILockpickable)targeted).Locked )
					{
						from.PlaySound( 0x241 );

						new InternalTimer( from, (ILockpickable)targeted, m_Item ).Start();
					}
					else
					{
						// The door is not locked
						from.SendLocalizedMessage( 502069 ); // This does not appear to be locked
					}
				}
				else
				{
					from.SendLocalizedMessage( 501666 ); // You can't unlock that!
				}
			}

			private class InternalTimer : Timer
			{
				private Mobile m_From;
				private ILockpickable m_Item;
				private SkeletonKeys m_SkeletonKeys;
			
				public InternalTimer( Mobile from, ILockpickable item, SkeletonKeys keys ) : base( TimeSpan.FromSeconds( 3.0 ) )
				{
					m_From = from;
					m_Item = item;
					m_SkeletonKeys = keys;
					Priority = TimerPriority.TwoFiftyMS;
				}

				protected void BrokeSkeletonKeysTest()
				{
					// When failed, a 25% chance to break the SkeletonKeys
					if ( Utility.Random( 4 ) == 0 )
					{
						Item item = (Item)m_Item;
						m_From.SendMessage( "You broke one of the keys!" );
						m_From.PlaySound( 0x3A4 );
						m_SkeletonKeys.Uses--;
						if ( m_SkeletonKeys.Uses < 1 )
							m_SkeletonKeys.Delete();
					}
				}
				
				protected override void OnTick()
				{
					Item item = (Item)m_Item;

					if ( !m_From.InRange( item.GetWorldLocation(), 1 ) )
						return;

					if ( m_Item.LockLevel == 0 || m_Item.LockLevel == -255 )
					{
						// LockLevel of 0 means that the door can't be picklocked
						// LockLevel of -255 means it's magic locked
						m_From.SendMessage( "This lock cannot be unlocked by normal means." );
						return;
					}

					if ( (m_From.Skills[SkillName.Lockpicking].Value+20) < m_Item.RequiredSkill )
					{
						/*
						// Do some training to gain skills
						m_From.CheckSkill( SkillName.Lockpicking, 0, m_Item.LockLevel );*/

						// The LockLevel is higher thant the Lockpicking of the player
						item.SendLocalizedMessageTo( m_From, 502072 ); // You don't see how that lock can be manipulated.
						return;
					}

					if ( m_From.CheckTargetSkill( SkillName.Lockpicking, m_Item, m_Item.LockLevel-20, m_Item.MaxLockLevel-20 ) )
					{
						// Success! Pick the lock!
						m_From.SendMessage( "The lock opens with the keys." );
						m_From.PlaySound( 0x4A );
						m_Item.LockPick( m_From );
					}
					else
					{
						// The player failed to pick the lock
						BrokeSkeletonKeysTest();
						m_From.SendMessage( "You are unable to unlock this." );
					}
				}
			}
		}
	}
}