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

namespace Server.Items
{
	public enum CampfireStatus
	{
		Burning,
		Extinguishing,
		Off
	}

	public class Campfire : Item
	{
		private Timer m_Timer;
		private DateTime m_Created;

		public Campfire() : base( 0xDE3 )
		{
			Movable = false;
			Light = LightType.Circle300;
			m_Created = DateTime.Now;
			m_Timer = Timer.DelayCall( TimeSpan.FromSeconds( 1.0 ), TimeSpan.FromSeconds( 1.0 ), new TimerCallback( OnTick ) );
		}

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

		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime Created
		{
			get{ return m_Created; }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public CampfireStatus Status
		{
			get
			{
				switch ( this.ItemID )
				{
					case 0xDE3:
						return CampfireStatus.Burning;

					case 0xDE9:
						return CampfireStatus.Extinguishing;

					default:
						return CampfireStatus.Off;
				}
			}
			set
			{
				if ( this.Status == value )
					return;

				switch ( value )
				{
					case CampfireStatus.Burning:
						this.ItemID = 0xDE3;
						this.Light = LightType.Circle300;
						break;

					case CampfireStatus.Extinguishing:
						this.ItemID = 0xDE9;
						this.Light = LightType.Circle150;
						break;

					default:
						this.ItemID = 0xDEA;
						this.Light = LightType.ArchedWindowEast;
						break;
				}
			}
		}

		private bool EnemiesNearby()
		{
			foreach( Mobile m in 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;
		}

		private void OnTick()
		{
			DateTime now = DateTime.Now;
			TimeSpan age = now - this.Created;

			if ( age >= TimeSpan.FromSeconds( 100.0 ) )
				this.Delete();
			else if ( age >= TimeSpan.FromSeconds( 90.0 ) )
				this.Status = CampfireStatus.Off;
			else if ( age >= TimeSpan.FromSeconds( 60.0 ) )
				this.Status = CampfireStatus.Extinguishing;

			if ( this.Status == CampfireStatus.Off || this.Deleted )
				return;

			List<Mobile> toRest = new List<Mobile>();
			if ( !EnemiesNearby() )
			{
				foreach( Mobile m in GetMobilesInRange( 6 ) )
				{
					if ( m is PlayerMobile )
						toRest.Add( m );
				}

				for ( int i = 0; i < toRest.Count; i++ )
					Rest( toRest[i] );
			}
		}

		public void Rest( Mobile m )
		{
			if ( m.Hunger > 4 && m.Thirst > 4 )
			{
				if ( m.Stam < m.StamMax )
					m.Stam++;
				if ( m.Hits < m.HitsMax )
				{
					int hits = (int)(1 * Server.Misc.Settings.HitPoints());
						if ( hits < 1 )
							hits = 1;

					m.Hits = m.Hits + hits;
				}
			}
		}

		public override void OnAfterDelete()
		{
			if ( m_Timer != null )
				m_Timer.Stop();
		}

		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();
			this.Delete();
		}
	}
}