using System;
using Server.Items;
using Server.Mobiles;
using Server.Multis;
using Server.Targeting;
using Server.Regions;

namespace Server.SkillHandlers
{
	public class Searching
	{
		public static void Initialize()
		{
			SkillInfo.Table[(int)SkillName.Searching].Callback = new SkillUseCallback( OnUse );
		}

		public static TimeSpan OnUse( Mobile src )
		{
			src.SendLocalizedMessage( 500819 );//Where will you search?
			src.Target = new InternalTarget();

			return TimeSpan.FromSeconds( 6.0 );
		}

		public static int TotalSearchSkill( Mobile m )
		{
			m.CheckSkill( SkillName.Searching, -5.0, 110.0 ); // Passive Check

			int skill = (int)(m.Skills[SkillName.Searching].Value/2);

			if ( m.Region is DungeonRegion )
			{
				skill = (int)(m.Skills[SkillName.Searching].Value);

				if ( !( m.BeginAction( typeof( LightCycle ) ) ) )
					skill = skill + 25;

				if ( m.FindItemOnLayer( Layer.TwoHanded ) != null )
				{ 
					Item item = m.FindItemOnLayer( Layer.TwoHanded );

					if ( item is BaseEquipableLight && ( item.ItemID == 0xA15 || item.ItemID == 0xA17 || item.ItemID == 0xA22 || item.ItemID == 0xA0F || item.ItemID == 0xA12 ) )
						skill = skill + 25;
				}
			}

			return skill;
		}

		private class InternalTarget : Target
		{
			public InternalTarget() : base( 12, true, TargetFlags.None )
			{
			}

			protected override void OnTarget( Mobile src, object targ )
			{
				bool foundAnyone = false;

				Point3D p;
				if ( targ is Mobile )
					p = ((Mobile)targ).Location;
				else if ( targ is Item )
					p = ((Item)targ).Location;
				else if ( targ is IPoint3D )
					p = new Point3D( (IPoint3D)targ );
				else 
					p = src.Location;

				double srcSkill = src.Skills[SkillName.Searching].Value;
				int range = (int)(srcSkill / 10.0);

				if ( !src.CheckSkill( SkillName.Searching, 0.0, 100.0 ) )
					range /= 2;

				BaseHouse house = BaseHouse.FindHouseAt( p, src.Map, 16 );

				bool inHouse = ( house != null && house.IsFriend( src ) );

				if ( inHouse )
					range = 22;

				if ( range > 0 )
				{
					IPooledEnumerable inRange = src.Map.GetMobilesInRange( p, range );

					foreach ( Mobile trg in inRange )
					{
						if ( trg.Hidden && src != trg )
						{
							double ss = srcSkill + Utility.Random( 21 ) - 10;
							double ts = trg.Skills[SkillName.Hiding].Value + Utility.Random( 21 ) - 10;

							if ( src.AccessLevel >= trg.AccessLevel && ( ss >= ts || ( inHouse && house.IsInside( trg ) ) ) )
							{
								trg.RevealingAction();
								trg.SendLocalizedMessage( 500814 ); // You have been revealed!
								foundAnyone = true;
							}
						}
					}

					inRange.Free();

					IPooledEnumerable inArea = src.Map.GetItemsInRange( p, range );

					foreach ( Item trg in inArea )
					{
						if ( 
							trg is SecretBookDoor || 
							trg is SecretStoneDoor1 || 
							trg is SecretDungeonDoor || 
							trg is SecretStoneDoor2 || 
							trg is SecretWoodenDoor || 
							trg is SecretLightWoodDoor || 
							trg is SecretStoneDoor3 )
						{
							BaseDoor door = (BaseDoor)trg;
							door.OnDoubleClick(src);
							foundAnyone = true;
						}
					}

					inArea.Free();
				}

				if ( !foundAnyone )
				{
					src.SendLocalizedMessage( 500817 ); // You can see nothing hidden there.
				}
			}
		}
	}
}
