using System;
using Server;
using Server.Items;
using System.Collections;
using Server.Mobiles;

namespace Server.Items
{
	public class CampDAddon : BaseAddon
	{
		private bool m_Sitting;
		private bool m_North;
		private bool m_South;
		private bool m_East;
		private bool m_West;

        private static int[,] m_AddOnSimpleComponents = new int[,] {
			{Utility.RandomList(749,2200), -1, -2, 0}, 	// WAGON
			{Utility.RandomList(750,2169), -2, 3, 0},	// WAGON2169
			{7134, 3, -1, 0},	// LOGS
			{7137, 0, -2, 0},	// LOGS
			{2861, 0, 3, 0},	// BENCH
			{2861, 1, -2, 0},	// BENCH
			{2860, -2, 0, 0}, 	// BENCH
			{2860, 3, 0, 0},	// BENCH
		};

		[ Constructable ]
		public CampDAddon()
		{
			Weight = -2.0;
			int wagon = Utility.Random(3);
			int logs = Utility.Random(3);
			m_Sitting = true;
				if ( Utility.RandomMinMax(1,3) == 1 )
					m_Sitting = false;

			m_North = true;
			m_South = true;
			m_East = true;
			m_West = true;

			int c = 0;
			int d = 0;
			int e = 0;
			bool doit = true;

            for (int i = 0; i < m_AddOnSimpleComponents.Length / 4; i++)
			{
				c++;

				if ( m_AddOnSimpleComponents[i,0] == 749 || m_AddOnSimpleComponents[i,0] == 750 || m_AddOnSimpleComponents[i,0] == 2169 || m_AddOnSimpleComponents[i,0] == 2200 )
				{
					if ( wagon == 1 && ( m_AddOnSimpleComponents[i,0] == 749 || m_AddOnSimpleComponents[i,0] == 2200 ) )
						AddComponent( new AddonComponent( m_AddOnSimpleComponents[i,0] ), m_AddOnSimpleComponents[i,1], m_AddOnSimpleComponents[i,2], m_AddOnSimpleComponents[i,3] );
					else if ( wagon == 2 && ( m_AddOnSimpleComponents[i,0] == 750 || m_AddOnSimpleComponents[i,0] == 2169 ) )
						AddComponent( new AddonComponent( m_AddOnSimpleComponents[i,0] ), m_AddOnSimpleComponents[i,1], m_AddOnSimpleComponents[i,2], m_AddOnSimpleComponents[i,3] );
				}
				else if ( m_AddOnSimpleComponents[i,0] == 7134 || m_AddOnSimpleComponents[i,0] == 7137 )
				{
					if ( logs == 1 && m_AddOnSimpleComponents[i,0] == 7134 )
						AddComponent( new AddonComponent( m_AddOnSimpleComponents[i,0] ), m_AddOnSimpleComponents[i,1], m_AddOnSimpleComponents[i,2], m_AddOnSimpleComponents[i,3] );
					else if ( logs == 2 && m_AddOnSimpleComponents[i,0] == 7137 )
						AddComponent( new AddonComponent( m_AddOnSimpleComponents[i,0] ), m_AddOnSimpleComponents[i,1], m_AddOnSimpleComponents[i,2], m_AddOnSimpleComponents[i,3] );
				}
				else if ( m_Sitting )
				{
					doit = true;

					if ( d < 4 ){ if ( Utility.RandomMinMax(1,4) == 1 ){ doit = false; } }
					else if ( d < 3 ){ if ( Utility.RandomMinMax(1,3) == 1 ){ doit = false; } }
					else if ( d < 2 ){ if ( Utility.RandomBool() ){ doit = false; } }

					if ( e == 0 && c == 7 ){ doit = true; }
					if ( e == 1 && c == 8 ){ doit = true; }

					if ( !doit )
					{
						d++;
						if ( c == 5 ){ m_North = false; }
						else if ( c == 6 ){ m_South = false; }
						else if ( c == 7 ){ m_East = false; }
						else if ( c == 8 ){ m_West = false; }
					}
					else
					{
						e++;
						AddonComponent ac = new AddonComponent( m_AddOnSimpleComponents[i,0] );
						ac.Weight = -2.0;
						AddComponent( ac, m_AddOnSimpleComponents[i,1], m_AddOnSimpleComponents[i,2], m_AddOnSimpleComponents[i,3] );
					}
				}
			}

			AddComplexComponent( (BaseAddon) this, 4012, 0, 0, 0, 0, 0, "fire", 1);
			AddComplexComponent( (BaseAddon) this, 3555, 0, 0, 2, 0, 0, "fire", 1);
		}

		public override void OnAfterSpawn()
		{
			base.OnAfterSpawn();

			int mod = 0;

			RemoveCitizens();

			BaseCreature north = new CitizenNorth();
			BaseCreature south = new CitizenSouth();
			BaseCreature east = new CitizenEast();
			BaseCreature west = new CitizenWest();

			if ( !m_Sitting )
			{
				north.Delete(); north = new AdventurerNorth();
				south.Delete(); south = new AdventurerSouth();
				east.Delete(); east = new AdventurerEast();
				west.Delete(); west = new AdventurerWest();

				if ( Utility.RandomBool() )
				{
					mod = 1;
					BaseMount mountN = new Horse(); if ( Utility.RandomMinMax(1,20) == 1 ){ mountN.Delete(); mountN = new Llama(); }
					mountN.Rider = north;
					north.Direction = Direction.North;
					BaseMount mountS = new Horse(); if ( Utility.RandomMinMax(1,20) == 1 ){ mountS.Delete(); mountS = new Llama(); }
					mountS.Rider = south;
					south.Direction = Direction.South;
					BaseMount mountE = new Horse(); if ( Utility.RandomMinMax(1,20) == 1 ){ mountE.Delete(); mountE = new Llama(); }
					mountE.Rider = east;
					east.Direction = Direction.East;
					BaseMount mountW = new Horse(); if ( Utility.RandomMinMax(1,20) == 1 ){ mountW.Delete(); mountW = new Llama(); }
					mountW.Rider = west;
					west.Direction = Direction.West;
				}
			}

			north.MoveToWorld( new Point3D( X, Y+3+mod, Z ), Map );
			south.MoveToWorld( new Point3D( X+1, Y-2-mod, Z ), Map );
			east.MoveToWorld( new Point3D( X-2-mod, Y, Z ), Map );
			west.MoveToWorld( new Point3D( X+3+mod, Y, Z ), Map );

			north.RangeHome = 0;
			north.Home = north.Location;
			north.OnAfterSpawn();
			if ( !m_North ){ north.Delete(); }
			south.RangeHome = 0;
			south.Home = south.Location;
			south.OnAfterSpawn();
			if ( !m_South ){ south.Delete(); }
			east.RangeHome = 0;
			east.Home = east.Location;
			east.OnAfterSpawn();
			if ( !m_East ){ east.Delete(); }
			west.RangeHome = 0;
			west.Home = west.Location;
			west.OnAfterSpawn();
			if ( !m_West ){ west.Delete(); }
		}

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

		public override void OnDelete()
		{
			RemoveCitizens();
			base.OnDelete();
		}

		public void RemoveCitizens()
		{
			ArrayList list = new ArrayList();

			foreach ( Mobile m in this.GetMobilesInRange( 8 ) )
			{
				if ( m is Citizen )
					list.Add( m );
			}
			for ( int i = 0; i < list.Count; ++i )
			{
				Mobile from = (Mobile)list[i];
				from.Delete();
			}
		}

        private static void AddComplexComponent(BaseAddon addon, int item, int xoffset, int yoffset, int zoffset, int hue, int lightsource)
        {
            AddComplexComponent(addon, item, xoffset, yoffset, zoffset, hue, lightsource, null, 1);
        }

        private static void AddComplexComponent(BaseAddon addon, int item, int xoffset, int yoffset, int zoffset, int hue, int lightsource, string name, int amount)
        {
            AddonComponent ac;
            ac = new AddonComponent(item);
			ac.Weight = -2.0;
            if (name != null && name.Length > 0)
                ac.Name = name;
            if (hue != 0)
                ac.Hue = hue;
            if (amount > 1)
            {
                ac.Stackable = true;
                ac.Amount = amount;
            }
            if (lightsource != -1)
                ac.Light = LightType.Circle300;
            addon.AddComponent(ac, xoffset, yoffset, zoffset);
        }

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

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