using System;
using System.Collections;
using System.Collections.Generic;

using Server;
using Server.ContextMenus;
using Server.Items;
using Server.Movement;
using Server.Network;
using Server.Multis;
using Server.Gumps;

namespace Server.Mobiles
{
    public class TillerManHS : Mobile
    {
        private BaseGalleon _galleon;
		private uint _keyValue;

        public TillerManHS(BaseGalleon galleon, uint keyValue, Point3D initOffset)
            : base()
        {
			Location = initOffset;
			galleon.Embark(this);
			galleon.TillerManMobile = this;
            _galleon = galleon;
			_keyValue = keyValue;
            CantWalk = true;
			Blessed = true;

            InitStats(31, 41, 51);

            SpeechHue = Utility.RandomDyedHue();

            Hue = Utility.RandomSkinHue();


            if (this.Female = Utility.RandomBool())
            {
                this.Body = 0x191;
                this.Name = NameList.RandomName("female");
                AddItem(new Kilt(Utility.RandomDyedHue()));
                AddItem(new Shirt(Utility.RandomDyedHue()));
                AddItem(new ThighBoots());
                Title = "the tillerman";
            }
            else
            {
                this.Body = 0x190;
                this.Name = NameList.RandomName("male");
                AddItem(new ShortPants(Utility.RandomNeutralHue()));
                AddItem(new Shirt(Utility.RandomDyedHue()));
                AddItem(new Sandals());
                Title = "the tillerman";
            }

            AddItem(new Bandana(Utility.RandomDyedHue()));

            Utility.AssignRandomHair(this);

            Container pack = new Backpack();

            pack.DropItem(new Gold(250, 300));

            pack.Movable = false;

            AddItem(pack);
        }

        public TillerManHS(Serial serial)
            : base(serial)
        {
        }
		
		
        [CommandProperty(AccessLevel.GameMaster)]
        public uint KeyValue { get { return _keyValue; } set { _keyValue = value; } }		

        [CommandProperty(AccessLevel.GameMaster)]
        public BaseGalleon Galleon { get { return _galleon; } }		

        public void SetFacing(Direction dir)
        {
            switch (dir)
            {
                case Direction.South: Direction = Server.Direction.South; break;
                case Direction.North: Direction = Server.Direction.North; break;
                case Direction.West: Direction = Server.Direction.West; break;
                case Direction.East: Direction = Server.Direction.East; break;
            }
        }

        public override void GetProperties(ObjectPropertyList list)
        {
            base.GetProperties(list);
            if (_galleon != null)
				switch ((int)_galleon.Status)
				{
					case 2:
						{
							list.Add(1116580);

							break;
						}
						
					case 1:
						{
							list.Add(1116582);

							break;
						}
						
					case 0:
						{
							list.Add(1116583);

							break;
						}
				}          
        }

        public void TillerManSay(int number)
        {
            PublicOverheadMessage(MessageType.Regular, 0x3B2, number);
        }

        public void TillerManSay(int number, string args)
        {
            PublicOverheadMessage(MessageType.Regular, 0x3B2, number, args);
        }

        public override void AddNameProperties(ObjectPropertyList list)
        {
            if (_galleon != null && _galleon.ShipName != null)
                list.Add(1042884, _galleon.ShipName); // the tiller man of the ~1_SHIP_NAME~
            else
                base.AddNameProperties(list);
        }

        public override void OnSingleClick(Mobile from)
        {
            if (_galleon != null && _galleon.ShipName != null)
                from.SendLocalizedMessage(1042884, _galleon.ShipName); // the tiller man of the ~1_SHIP_NAME~
            else
                base.OnSingleClick(from);
        }

        public override void OnDoubleClick(Mobile from)
        {
			if (_galleon != null && !_galleon.Contains(from))
                _galleon.BeginDryDock(from);
        }

        public override bool OnDragDrop(Mobile from, Item dropped)
        {
            if (dropped is MapItem && _galleon != null && _galleon.CanCommand(from) && _galleon.Contains(from))
            {
                _galleon.AssociateMap((MapItem)dropped);
            }

            return false;
        }

        public override void OnAfterDelete()
        {
            if (_galleon != null)
                _galleon.Delete();
        }
		
        public override void GetContextMenuEntries(Mobile from, List<ContextMenuEntry> list)
        {
			base.GetContextMenuEntries(from, list);
			
			if (_galleon != null && !_galleon.Contains(from))
			{			
				list.Add(new DryDockEntry(from, _galleon));
			}
			else
			{
				list.Add(new SecuritySettingsEntry(from, _galleon));
				list.Add(new RenameEntry(from, _galleon));			
			}
			
			if (_galleon.Durability < 33)
			{
				list.Add(new EmergencyRepairsEntry(from, _galleon));
			}
        }		
		

        public override void Serialize(GenericWriter writer)
        {
            base.Serialize(writer);

            writer.Write((int)0);//version

            writer.Write(_galleon);
        }

        public override void Deserialize(GenericReader reader)
        {
            base.Deserialize(reader);

            int version = reader.ReadInt();

            switch (version)
            {
                case 0:
                    {
                        _galleon = reader.ReadItem() as BaseGalleon;

                        if (_galleon == null)
                            Delete();

                        break;
                    }
            }
        }
		
		private class SecuritySettingsEntry : ContextMenuEntry
		{			
			private readonly Mobile m_from;
			private BaseGalleon _galleon;
			
			public SecuritySettingsEntry(Mobile from, BaseGalleon galleon)
				: base(1116567)
			{				
				m_from = from;
				_galleon = galleon;
			}

			public override void OnClick()
			{
				if (m_from != null)
				{
				
					Dictionary<int, PlayerMobile> PlayersAboard = new Dictionary<int,PlayerMobile>();
					IPooledEnumerable eable = m_from.Map.GetClientsInRange(m_from.Location, _galleon.GetMaxUpdateRange());
					int i = 0;
					foreach (NetState state in eable)
					{
						Mobile m = state.Mobile;

						if (m is PlayerMobile)						
							if (_galleon.IsOnBoard(m))											
								PlayersAboard.Add(i++,(PlayerMobile)m);																            
					}
					eable.Free();					
				
					m_from.SendGump(new SecuritySettingsGump(SecuritySettingsGumpPage.Default, m_from, (BaseShip)_galleon, PlayersAboard, 1, null));
				}
			}
		}

		private class RenameEntry : ContextMenuEntry
		{			
			private readonly Mobile m_from;
			private BaseGalleon _galleon;
			
			public RenameEntry(Mobile from, BaseGalleon galleon)
				: base(1111680)
			{				
				m_from = from;
				_galleon = galleon;
			}

			public override void OnClick()
			{
				if ((m_from != null) && (_galleon != null))
					_galleon.BeginRename(m_from);			
			}
		}	

		private class EmergencyRepairsEntry : ContextMenuEntry
		{			
			private readonly Mobile m_from;
			private BaseGalleon _galleon;
			
			public EmergencyRepairsEntry(Mobile from, BaseGalleon galleon)
				: base(1116589)
			{				
				m_from = from;
				_galleon = galleon;
			}

			public override void OnClick()
			{
				if ((m_from != null) && (_galleon != null))
					_galleon.EmergencyRepairs();			
			}
		}	

		private class DryDockEntry : ContextMenuEntry
		{			
			private readonly Mobile m_From;
			private BaseGalleon _galleon;
			
			public DryDockEntry(Mobile from, BaseGalleon boat)
				: base(1116520)
			{				
				m_From = from;
				_galleon = boat;
			}

			public override void OnClick()
			{
				if ((m_From != null) && (_galleon != null))
					_galleon.BeginDryDock(m_From);			
			}
		}		
    }
}