using Server.Gumps;
using Server.Network;

namespace Server.Items
{
	public class BodyDye : Item
	{
		[Constructable]
		public BodyDye() : base( 0xEFF )
		{
            Name = "Body Paint";
			Weight = 1.0;
		}

        public BodyDye(Serial serial)
            : base(serial)
		{
		}
		
		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();
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( from.InRange( GetWorldLocation(), 1 ) )
			{
				from.CloseGump( typeof( BodyDyeGump ) );
				from.SendGump( new BodyDyeGump( this ) );
			}
			else
			{
				from.LocalOverheadMessage( MessageType.Regular, 906, 1019045 ); // I can't reach that.
			}	
		}
	}

	public class BodyDyeGump : Gump
	{
		private readonly BodyDye m_BodyDye;

		private class BodyDyeEntry
		{
			private readonly string m_Name;
			private readonly int m_HueStart;
			private readonly int m_HueCount;

			public string Name
			{
				get
				{
					return m_Name;
				}
			}

			public int HueStart
			{
				get
				{
					return m_HueStart;
				}
			}

			public int HueCount
			{
				get
				{
					return m_HueCount;
				}
			}

			public BodyDyeEntry( string name, int hueStart, int hueCount )
			{
				m_Name = name;
				m_HueStart = hueStart;
				m_HueCount = hueCount;
			}
		}

		private static readonly BodyDyeEntry[] m_Entries = new BodyDyeEntry[]
			{
				new BodyDyeEntry( "*****", 1602, 26 ),
				new BodyDyeEntry( "*****", 1628, 27 ),
				new BodyDyeEntry( "*****", 1502, 32 ),
				new BodyDyeEntry( "*****", 1302, 32 ),
				new BodyDyeEntry( "*****", 1402, 32 ),
				new BodyDyeEntry( "*****", 1202, 24 ),
				new BodyDyeEntry( "*****", 2402, 29 ),
				new BodyDyeEntry( "*****", 2213, 6 ),
				new BodyDyeEntry( "*****", 1102, 8 ),
				new BodyDyeEntry( "*****", 1110, 8 ),
				new BodyDyeEntry( "*****", 1118, 16 ),
				new BodyDyeEntry( "*****", 1134, 16 )
			};

        public BodyDyeGump(BodyDye dye)
            : base(50, 50)
		{
			m_BodyDye = dye;

			AddPage( 0 );

			AddBackground( 100, 10, 350, 355, 2600 );
			AddBackground( 120, 54, 110, 270, 5100 );

            AddHtml(70, 25, 400, 35, "<center>Body Paint Color Selection Menu</center>", false, false); // <center>Hair Color Selection Menu</center>

			AddButton( 149, 328, 4005, 4007, 1, GumpButtonType.Reply, 0 );
			AddHtml( 185, 329, 250, 35, "Dye my body this color!", false, false ); // Dye my hair this color!

			for ( int i = 0; i < m_Entries.Length; ++i )
			{
				AddLabel( 130, 59 + (i * 22), m_Entries[i].HueStart - 1, m_Entries[i].Name );
				AddButton( 207, 60 + (i * 22), 5224, 5224, 0, GumpButtonType.Page, i + 1 );
			}

			for ( int i = 0; i < m_Entries.Length; ++i )
			{
				BodyDyeEntry e = m_Entries[i];

				AddPage( i + 1 );

				for ( int j = 0; j < e.HueCount; ++j )
				{
					AddLabel( 278 + ((j / 16) * 80), 52 + ((j % 16) * 17), e.HueStart + j - 1, "*****" );
					AddRadio( 260 + ((j / 16) * 80), 52 + ((j % 16) * 17), 210, 211, false, (i * 100) + j );
				}
			}
		}

		public override void OnResponse( NetState from, RelayInfo info )
		{
			if ( m_BodyDye.Deleted )
				return;

			Mobile m = from.Mobile;
			int[] switches = info.Switches;

            if (!m_BodyDye.IsChildOf(m.Backpack)) 
			{
				m.SendLocalizedMessage( 1042010 ); //You must have the objectin your backpack to use it.
				return;
			}

			if ( info.ButtonID != 0 && switches.Length > 0 )
			{
				// To prevent this from being exploited, the hue is abstracted into an internal list

				int entryIndex = switches[0] / 100;
				int hueOffset = switches[0] % 100;

                if (entryIndex >= 0 && entryIndex < m_Entries.Length)
                {
                    BodyDyeEntry e = m_Entries[entryIndex];

                    if (hueOffset >= 0 && hueOffset < e.HueCount)
                    {
                        int hue = e.HueStart + hueOffset;

                        m.Hue = hue;

                        m.SendMessage("You cover your body in paint!");  // You dye your hair
                        m_BodyDye.Delete();
                        m.PlaySound(0x4E);
                    }
                }
					
			}
			else
			{
				m.SendMessage( "You decide not to paint your body." ); // You decide not to dye your hair
			}
		}
	}
}