using System;
using System.Collections;
using Server;
using Server.Gumps;
using Server.Mobiles;
using Server.Accounting;
using Server.Commands;
using Server.Network;

namespace Server.Gumps
{
	public class FullToolbarCommand
	{
		public static void Initialize()
		{
			CommandSystem.Register( "Fubar", AccessLevel.GameMaster, new CommandEventHandler( Fubar_OnCommand ) );
			CommandSystem.Register( "FullToolbar", AccessLevel.GameMaster, new CommandEventHandler( Fubar_OnCommand ) );
		}
		
		[Usage( "FullToolbar" )]
		[Description( "Begins the Full Toolbar Gump." )]
		private static void Fubar_OnCommand( CommandEventArgs e )
		{
			Mobile m = e.Mobile;
			m.SendGump( new FullToolbar( m ) );
		}
	}
	public class FullToolbar : Gump
	{
		private Mobile m_From;
		private Account m_Account;
		private string[] m_Str;
		private int m_X, m_Y, m_W, m_H;
		private bool m_AutoHide;
		private bool m_Locked;
		private int m_Pages;
		private int m_ThisPage;
		
		public FullToolbar( Mobile from ) : base( 0, 0 )
		{
			m_From = from;
			
			m_From.CloseGump( typeof( FullToolbar ) );
			
			m_Account = m_From.Account as Account;
			
			if ( m_Account.GetTag( "FullToolbar" ) == null ) m_Account.SetTag( "FullToolbar", GetStartString() );
			m_Str = m_Account.GetTag( "FullToolbar" ).Split( '~' );
			
			int StrPos = 0;
			try { m_X = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_X = 5; }
			try { m_Y = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_Y = 33; }
			try { m_W = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_W = 700; }
			try { m_H = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_H = 53; }
			try { if ( m_Str[StrPos++] == "Yes" ) m_AutoHide = true; else m_AutoHide = false; } catch { m_AutoHide = false; }
			try { if ( m_Str[StrPos++] == "Yes" ) m_Locked = true; else m_Locked = false; } catch { m_Locked = false; }
			try { m_Pages = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_Pages = 6; }
			try { m_ThisPage = Convert.ToInt32( m_Str[StrPos++] ); } catch { m_ThisPage = 1; }
			
			Closable=true;
			Disposable=true;
			Dragable=true;
			Resizable=false;
			AddPage(0);
			AddBackground(m_X, m_Y, m_W, m_H, 9200);
			AddLabel( m_X + 20, m_Y, 0, @"Hide" );
			AddImageTiledButton( m_X + 604, m_Y + 9, 22153, 22155, 301, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 3010032 ); //Help Button
			AddImageTiledButton( m_X + 4, m_Y + 5, 1210, 1209, 1, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 3002085 ); // Minimize
			if ( m_Locked ) { AddImage( m_X + 2, m_Y + 20, 10850 );
				AddImageTiledButton( m_X + 10, m_Y + 29, 2092, 2092, 2, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 3005142 ); } // Locked
			else { AddImage( m_X + 2, m_Y + 20, 10830 );
				AddImageTiledButton( m_X + 10, m_Y + 29, 2092, 2092, 3, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 3005143 ); } // Unlocked
			AddLabel( m_X + 631, m_Y + 29, 0, "Page" );
			if ( m_ThisPage < 6 )
				AddImageTiledButton( m_X + 674, m_Y + 32, 5402, 5402, 4, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1011066 ); // Next page
			if ( m_ThisPage > 1 )
				AddImageTiledButton( m_X + 604, m_Y + 32, 5401, 5401, 5, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1011067 ); // Previous Page
			if ( m_AutoHide )
				AddImageTiledButton( m_X + m_W - 30, m_Y + 8, 9910, 9910, 6, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1044107 ); // Stealth
			else AddImageTiledButton( m_X + m_W - 30, m_Y + 8, 9903, 9903, 7, GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1044107 ); // Stealth
			
			StrPos = ( ( m_ThisPage - 1 ) * 5 ) + 8;
			int Xpos = 0;
			
			for ( int x = StrPos; x < StrPos + 5; x++ )
			{
				if ( !m_Locked ) AddTextEntry( m_X + 115 + (Xpos * 100), m_Y + 3, 70, 20, 0x769, x, m_Str[x] );
				else AddLabel( m_X + 115 + (Xpos * 100), m_Y + 3, 0, m_Str[x] );
				if ( !m_Locked ) AddImageTiledButton( m_X + 85 + (Xpos * 100), m_Y + 2, 4027, 4028, x,
									GumpButtonType.Reply, 0, 1352, 0, 140, 20, 3005038 ); // Trigger Edit
				AddImageTiledButton( m_X + 115 + (Xpos * 100), m_Y + 27, 4006, 4007, 100 + x,
									GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1005135 ); // Awaiting your command...
				AddImageTiledButton( m_X + 150 + (Xpos++ * 100), m_Y + 27, 4009, 4010, 200 + x,
									GumpButtonType.Reply, 0, 1352, 0, 140, 20, 1048176 ); // Makes as many as possible at once
			}
		}
		
		public override void OnResponse( NetState state, RelayInfo info )
		{
			int choice = info.ButtonID;
			if 		( choice == 0 ) { m_From.CloseGump( typeof( FullToolbar ) ); }
			else if ( choice == 1 ) { m_From.SendGump( new MiniToolbar( m_From ) ); }
			else if ( choice == 2 ) { m_Str[5] = "No"; SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice == 3 ) { m_Str[5] = "Yes"; SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice == 4 ) { m_Str[7] = ( m_ThisPage + 1 ).ToString(); SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice == 5 ) { m_Str[7] = ( m_ThisPage - 1 ).ToString(); SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice == 6 ) { m_Str[4] = "No"; SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice == 7 ) { m_Str[4] = "Yes"; SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice < 100 ) { m_Str[choice] = info.GetTextEntry( choice ).Text; SetTheTag(); RelaunchToolbar( false ); }
			else if ( choice < 200 ) { UseCommand( m_Str[choice - 100] ); RelaunchToolbar( true ); }
			else if ( choice < 300 ) { UseMultiCommand( m_Str[choice - 200] ); RelaunchToolbar( true ); }
			else if ( choice == 301 ) { m_From.SendGump( new FubarHelpGump( m_From ) ); RelaunchToolbar( false ); }
			else { m_Str[0] = this.X.ToString(); m_Str[1] = this.Y.ToString(); SetTheTag(); m_From.CloseGump( typeof( FullToolbar ) ); }
			
		}
		
		public void RelaunchToolbar( bool cmd )
		{
			m_From.CloseGump( typeof( FullToolbar ) );
			if ( cmd && m_AutoHide ) m_From.SendGump( new MiniToolbar( m_From ) );
			else m_From.SendGump( new FullToolbar( m_From ) );
		}
		
		public void UseCommand( string command )
		{
			string Pfix = CommandSystem.Prefix;
			CommandSystem.Handle( m_From, String.Format( "{0}{1}", Pfix, command ) );
		}
		
		public void UseMultiCommand( string command )
		{
			string Pfix = CommandSystem.Prefix;
			CommandSystem.Handle( m_From, String.Format( "{0}m {1}", Pfix, command ) );
		}
		
		public string GetStartString()
		{
			string start = "5~33~700~53~No~No~6~1~Who~Where~Bank~Move~Props~Tele~Wipe~Skills~Stats~PM";
			start += "~Save~Restart~Freeze~Unfreeze~Admin~Kill~Go~Kick~Restock~WipeMultis~Add Gold 60000~DupeInBag~Add PremiumSpawner";
			start += "~OWLTR~Speak~ShaveBeard~ShaveHair~DoorGen~VendorGen~WorldTeleGen";
			return start;
		}
		
		public void SetTheTag()
		{
			int StrPos = 8;
			string tagstring = m_Str[0];
			try
			{
				for ( int z = 1; z <= ( m_Pages * 5 ) + 7; z++ ) tagstring += "~" + m_Str[z];
				m_Account.SetTag( "FullToolbar", tagstring );
			}
			catch {	m_Account.SetTag( "FullToolbar", GetStartString() ); }
		}
	}
}