using System;
using Server;

namespace Server.Items
{
    public abstract class BaseSpawner : Item
	{
		private Mobile m_TrackedMobile;
		private Timer m_TrackerTimer;

		public BaseSpawner() : base( 0x6519 )
		{
			Name = "spawn ref";
			Visible = false;
			Movable = false;
			// Snappy 1-second timer to place the mob
			Timer.DelayCall( TimeSpan.FromSeconds( 1.0 ), new TimerCallback( ExecuteSpawn ) );
		}

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			
			// Save the mobile we are tracking in case the server restarts
			writer.Write( m_TrackedMobile ); 
		}

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

			if ( m_TrackedMobile != null && !m_TrackedMobile.Deleted )
			{
				StartTracking();
			}
			else
			{
				// Clean up abandoned proxies on restart
				Timer.DelayCall( TimeSpan.FromSeconds( 1.0 ), new TimerCallback( this.Delete ) );
			}
		}

		private void ExecuteSpawn()
		{
			if ( !this.Deleted && this.Map != null && this.Map != Map.Internal )
			{
				Point3D savedLocation = this.Location;
				Map savedMap = this.Map;

				m_TrackedMobile = Server.SpawnList.RandomSpawn( this, savedLocation, savedMap );
				
				if ( m_TrackedMobile != null )
				{
					m_TrackedMobile.MoveToWorld( savedLocation, savedMap );
					
					// Hide this proxy item in the void instead of deleting it
					this.Internalize(); 
					StartTracking();
				}
				else
				{
					this.Delete();
				}
			}
			else if ( !this.Deleted )
			{
				this.Delete();
			}
		}

		private void StartTracking()
		{
			if ( m_TrackerTimer != null )
				m_TrackerTimer.Stop();

			// Ping every 5 seconds to check if the animal is still alive
			m_TrackerTimer = Timer.DelayCall( TimeSpan.FromSeconds( 5.0 ), TimeSpan.FromSeconds( 5.0 ), new TimerCallback( TrackMobile ) );
		}

		private void TrackMobile()
		{
			if ( m_TrackedMobile == null || m_TrackedMobile.Deleted || !m_TrackedMobile.Alive )
			{
				if ( m_TrackerTimer != null )
					m_TrackerTimer.Stop();
				
				// Kill this proxy item so the Premium Spawner knows it's time to respawn!
				if ( !this.Deleted )
					this.Delete();
			}
		}
	}

	public class SpawnAnimals : BaseSpawner
	{
		[Constructable]
		public SpawnAnimals(){} public SpawnAnimals( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class SpawnBirds : BaseSpawner
	{
		[Constructable]
		public SpawnBirds(){} public SpawnBirds( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class SpawnCritters : BaseSpawner
	{
		[Constructable]
		public SpawnCritters(){} public SpawnCritters( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class Spawn_A : BaseSpawner
	{
		[Constructable]
		public Spawn_A(){} public Spawn_A( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class Spawn_B : BaseSpawner
	{
		[Constructable]
		public Spawn_B(){} public Spawn_B( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class Spawn_C : BaseSpawner
	{
		[Constructable]
		public Spawn_C(){} public Spawn_C( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class Spawn_D : BaseSpawner
	{
		[Constructable]
		public Spawn_D(){} public Spawn_D( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}

	public class Spawn_E : BaseSpawner
	{
		[Constructable]
		public Spawn_E(){} public Spawn_E( Serial serial ) : base( serial ){} public override void Serialize( GenericWriter writer ){ base.Serialize( writer ); writer.Write( (int) 0 ); } public override void Deserialize( GenericReader reader ){ base.Deserialize( reader ); int version = reader.ReadInt(); }
	}
}
