using System;
using System.Collections;
using Server;
using Server.Network;
using Server.Regions;
using Server.Mobiles;

namespace Server.Items
{
	public class Kindling : Item
	{
		[Constructable]
		public Kindling() : this( 1 )
		{
		}

		[Constructable]
		public Kindling( int amount ) : base( 0xDE1 )
		{
			Stackable = true;
			Weight = 5.0;
			Amount = amount;
		}

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

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

		public static bool EnemiesNearby( Mobile from )
		{
			foreach( Mobile m in from.GetMobilesInRange( 20 ) )
			{
				if ( m is PlayerMobile && m.Combatant != null && m.InRange( m.Combatant.Location, 20 ) && m.Combatant.InLOS( m ) )
					return true;
				else if ( m is BaseCreature && !((BaseCreature)m).Controlled && !((BaseCreature)m).Summoned && ((BaseCreature)m).FightMode == FightMode.Closest )
					return true;
			}

			return false;
		}

		public static bool CampAllowed( Mobile from )
		{
			if ( from.Region.IsPartOf( typeof( InnRegion ) ) )
				return false;

			if ( from.Region.IsPartOf( typeof( HouseRegion ) ) )
				return false;

			if ( from.Region.IsPartOf( typeof( GateRegion ) ) )
				return false;

			if ( from.Region.IsPartOf( typeof( GardenRegion ) ) )
				return false;

			if ( from.Region.IsPartOf( typeof( BuildingRegion ) ) )
				return false;

			return true;
		}

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

		private bool CampsNearby()
		{
			foreach( Item i in GetItemsInRange( 20 ) )
			{
				if ( i is Campfire )
				{
					Campfire fire = (Campfire)i;

					if ( fire.Status != CampfireStatus.Off )
						return true;
				}
			}

			return false;
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( from is PlayerMobile )
			{
				PlayerMobile pm = (PlayerMobile)from;

				if ( CampsNearby() )
				{
					from.SendMessage( "There is already a camp nearby!" );
				}
				else if ( !EnemiesNearby( from ) )
				{
					from.SendMessage( "It is not safe enough to setup camp!" );
				}
				else if ( DateTime.Now >= pm.Camp )
				{
					if ( !this.VerifyMove( from ) )
						return;

					if ( !from.InRange( this.GetWorldLocation(), 2 ) )
					{
						from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 1019045 ); // I can't reach that.
						return;
					}

					Point3D fireLocation = GetFireLocation( from );

					if ( fireLocation == Point3D.Zero )
					{
						from.SendLocalizedMessage( 501695 ); // There is not a spot nearby to place your campfire.
					}
					else
					{
						Consume();

						if ( !this.Deleted && this.Parent == null )
							from.PlaceInBackpack( this );

						new Campfire().MoveToWorld( fireLocation, from.Map );
						pm.Camp = DateTime.Now + TimeSpan.FromMinutes( 10.0 );
					}
				}
				else
				{
					from.SendMessage( "You can only build a camp fire every 10 minutes!" );
				}
			}
		}

		private Point3D GetFireLocation( Mobile from )
		{
			if ( !CampAllowed( from ) )
				return Point3D.Zero;

			if ( this.Parent == null )
				return this.Location;

			ArrayList list = new ArrayList( 4 );

			AddOffsetLocation( from,  0, -1, list );
			AddOffsetLocation( from, -1,  0, list );
			AddOffsetLocation( from,  0,  1, list );
			AddOffsetLocation( from,  1,  0, list );

			if ( list.Count == 0 )
				return Point3D.Zero;

			int idx = Utility.Random( list.Count );
			return (Point3D) list[idx];
		}

		private void AddOffsetLocation( Mobile from, int offsetX, int offsetY, ArrayList list )
		{
			Map map = from.Map;

			int x = from.X + offsetX;
			int y = from.Y + offsetY;

			Point3D loc = new Point3D( x, y, from.Z );

			if ( map.CanFit( loc, 1 ) && from.InLOS( loc ) )
			{
				list.Add( loc );
			}
			else
			{
				loc = new Point3D( x, y, map.GetAverageZ( x, y ) );

				if ( map.CanFit( loc, 1 ) && from.InLOS( loc ) )
					list.Add( loc );
			}
		}
	}
}