/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\ML Peerless + misc areas\Jormundgand\Rare Drop\SeaHairDye.cs
Lines of code: 165
***********************************************************/


using System;
using System.Text;
using Server.Gumps;
using Server.Network;

namespace Server.Items
{
	public class SeaHairDye : Item
	{
		public override string DefaultName
		{
			get { return "Sea Hair Dye"; }
		}

		[Constructable]
		public SeaHairDye() : base( 0xE26 )
		{
			Weight = 1.0;
			Hue = 88;

		}

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

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

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

	public class SeaHairDyeGump : Gump
	{
		private SeaHairDye m_SeaHairDye;

		private class SeaHairDyeEntry
		{
			private string m_Name;
			private int m_HueStart;
			private 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 SeaHairDyeEntry( string name, int hueStart, int hueCount )
			{
				m_Name = name;
				m_HueStart = hueStart;
				m_HueCount = hueCount;
			}
		}

		private static SeaHairDyeEntry[] m_Entries = new SeaHairDyeEntry[]
			{
				new SeaHairDyeEntry( "Seahag Green", 687, 1 ),
				new SeaHairDyeEntry( "Blue Water", 88, 1 ),
				new SeaHairDyeEntry( "Seaweed", 286, 1 ),
				new SeaHairDyeEntry( "Leviath Blue", 1154, 1 ),
				new SeaHairDyeEntry( "Sea Green", 84, 1 ),
				new SeaHairDyeEntry( "Deep Blue", 95, 1 ),
				new SeaHairDyeEntry( "Pale Blue", 691, 1 ),
				new SeaHairDyeEntry( "Marine", 6, 1 )
		};

		public SeaHairDyeGump( SeaHairDye dye ) : base( 0, 0 )
		{
			m_SeaHairDye = dye;

			AddPage( 0 );
			AddBackground( 150, 60, 350, 358, 2600 );
			AddBackground( 170, 104, 110, 270, 5100 );
			AddHtmlLocalized( 230, 75, 200, 20, 1011013, false, false );		// Hair Color Selection Menu
			AddHtmlLocalized( 235, 380, 300, 20, 1011014, false, false );		// Dye my hair this color!
			AddButton( 200, 380, 0xFA5, 0xFA7, 1, GumpButtonType.Reply, 0 );        // DYE HAIR

			for ( int i = 0; i < m_Entries.Length; ++i )
			{
				AddLabel( 180, 109 + (i * 22), m_Entries[i].HueStart - 1, m_Entries[i].Name );
				AddButton( 257, 110 + (i * 22), 5224, 5224, 0, GumpButtonType.Page, i + 1 );
			}

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

				AddPage( i + 1 );

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

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

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

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

			if ( info.ButtonID != 0 && switches.Length > 0 )
			{
				if( m.HairItemID == 0 )
				{
					m.SendLocalizedMessage( 502623 );	// You have no hair to dye and cannot use this
				}
				else
				{
					// 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 )
					{
						SeaHairDyeEntry e = m_Entries[entryIndex];

						if ( hueOffset >= 0 && hueOffset < e.HueCount )
						{
							m_SeaHairDye.Delete();

							int hue = e.HueStart + hueOffset;

							m.HairHue = hue;
							
							m.SendLocalizedMessage( 501199 );  // You dye your hair
							m.PlaySound( 0x4E );
						}
					}
				}
			}
			else
			{
				m.SendLocalizedMessage( 501200 ); // You decide not to dye your hair
			}
		}
	}
}