using System;
using System.Collections;
using Server.Network;
using Server.Targeting;
using Server.Prompts;

namespace Server.Items
{
	public class BottleOfAcid : Item
	{
		public override int Hue{ get { return 0x428; } }

		public override double DefaultWeight
		{
			get { return 1.0; }
		}

		[Constructable]
		public BottleOfAcid() : base( 0x2038 )
		{
			Name = "bottle of acid";
			Stackable = true;
		}

		public override void OnDoubleClick( Mobile from )
		{
			Target t;

			if ( !IsChildOf( from.Backpack ) )
			{
				from.SendLocalizedMessage( 1060640 ); // The item must be in your backpack to use it.
			}
			else
			{
				from.SendMessage( "What chest do you want to use the acid on?" );
				t = new UnlockTarget( this );
				from.Target = t;
			}
		}

		public override void AddNameProperties( ObjectPropertyList list )
		{
			base.AddNameProperties( list );
			list.Add( 1070722, "Dissolve Chest Traps & Locks" );
		}

		private class UnlockTarget : Target
		{
			private BottleOfAcid m_Key;

			public UnlockTarget( BottleOfAcid key ) : base( 1, false, TargetFlags.None )
			{
				m_Key = key;
				CheckLOS = true;
			}

			protected override void OnTarget( Mobile from, object targeted )
			{
				if ( !m_Key.IsChildOf( from.Backpack ) )
				{
					from.SendLocalizedMessage( 1060640 ); // The item must be in your backpack to use it.
				}
				else if ( targeted == m_Key )
				{
					from.SendMessage( "This acid is to dissolve locks and traps on most chests." );
				}
				else if ( targeted is LootChest || targeted is SunkenChest || targeted is AncientChest || targeted is TreasureMapChest )
				{
					if ( targeted is LootChest )
						((LootChest)targeted).Setup();

					ILockable o = (ILockable)targeted;
					LockableContainer cont2 = (LockableContainer)o;
					TrapableContainer cont3 = (TrapableContainer)o;

					if ( ( o.Locked ) || ( cont3.TrapType != TrapType.None ) )
					{
						if ( o is BaseDoor && !((BaseDoor)o).UseLocks() )  // this seems to check house doors also
						{
							from.SendMessage( "This acid is used to dissolve locks and traps on chests." );
						}
						else
						{
							o.Locked = false;

							if ( o is LockableContainer )
							{
								LockableContainer cont = (LockableContainer)o;
								if ( cont.LockLevel == -255 )
								{
									cont.LockLevel = cont.RequiredSkill - 10;
									if ( cont.LockLevel == 0 )
										cont.LockLevel = -1;
								}

								cont.Picker = from;  // sets "lockpicker" to the user.
							}

							if ( o is TrapableContainer )
							{
								TrapableContainer cont = (TrapableContainer)o;

								if ( cont.TrapType != TrapType.None )
									cont.TrapType = TrapType.None;
							}

							from.SendMessage( "The acid seems to have eaten away at the mechanism inside." );
							from.RevealingAction();
							from.PlaySound( 0x231 );
							from.AddToBackpack( new Bottle() );
							m_Key.Consume();
						}
					}
					else
					{
						from.SendMessage( "You don't need to use acid on that." );
					}
				}
				else
				{
					from.SendMessage( "This acid is to dissolve locks and traps on chests." );
				}
			}
		}

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

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

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