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

namespace Server.Misc
{
	public class MapFunctions
	{
		public static void Initialize()
		{
			CommandSystem.Register( "Map", AccessLevel.Player, new CommandEventHandler( EventSink_OnCommand ) );
		}

		[Usage( "Map" )]
		[Description( "Brings up the World Map." )]
		public static void EventSink_OnCommand( CommandEventArgs e )
		{
			(e.Mobile).CloseGump( typeof( MapGump ) );
			(e.Mobile).SendGump( new MapGump( e.Mobile ) );
		}

		public static int NumLocations()
		{
			return 83;
		}

		public static bool HasLocation( PlayerMobile pm, int location )
		{
			string s = pm.MapMarkers;
			int i = (location-1) * 2;
			char c = s[i];
			if ( c == '1' )
				return true;

			return false;
		}

		public static string SetLocations( PlayerMobile pm )
		{
			string locations = pm.MapMarkers;

			if ( locations == null || locations.Length < ( NumLocations() * 2 ) )
			{
				int cycle = NumLocations();

				while ( cycle > 0 )
				{
					cycle--;
					locations = locations + "0#";
				}

				pm.MapMarkers = locations;
			}

			return locations;
		}

		public static void AddLocations( Mobile m, int location )
		{
			if ( m != null && m is PlayerMobile )
			{
				string locations = ((PlayerMobile)m).MapMarkers;

				locations = SetLocations( (PlayerMobile)m );

				if ( locations.Length > 0 )
				{
					string[] discoveries = locations.Split('#');
					string entry = "";
					int num = 1;

					foreach ( string keyset in discoveries )
					{
						string sets = "1";
						if ( keyset != "1" ){ sets = "0"; } 
						if ( location == num ){ entry = entry + "1#"; }
						else { entry = entry + sets + "#"; }
						num++;
					}

					while ( num < NumLocations()+1 )
					{
						entry = entry + "0#";
						num++;
					}

					((PlayerMobile)m).MapMarkers = entry;
				}
			}
		}

		public static int Pin( int x, int n )
		{
			if ( x < 1 )
				n++;
			else
				n=0;

			return n;
		}

		public static string LocationInfo( int loc, int section )
		{
			string name = "";
			int x = 0;
			int y = 0;
			string c = "";
			string i = "true";
			int d = 0;
			int nxt = 1;
			int hue = 0;
			int s = 0;

			if ( loc == nxt ){		name = "Britain";				x=2169;		y=1579;	}	nxt = Pin( x, nxt ); //1
			if ( loc == nxt ){		name = "Yew";					x=1534;		y=863;	}	nxt = Pin( x, nxt ); //2
			if ( loc == nxt ){		name = "Prison";				x=1003;		y=969;	}	nxt = Pin( x, nxt ); //3
			if ( loc == nxt ){		name = "Minoc";					x=2879;		y=486;	}	nxt = Pin( x, nxt ); //4
			if ( loc == nxt ){		name = "Skara Brae"; d = 2;		x=738;		y=1871;	}	nxt = Pin( x, nxt ); //5
			if ( loc == nxt ){		name = "Jhelom";				x=1065;		y=3157;	}	nxt = Pin( x, nxt ); //6
			if ( loc == nxt ){		name = "Trinsic";				x=2029;		y=2630;	}	nxt = Pin( x, nxt ); //7
			if ( loc == nxt ){		name = "Paws";					x=1983;		y=2163;	}	nxt = Pin( x, nxt ); //8
			if ( loc == nxt ){		name = "Buccaneer's Den";		x=2644;		y=2213;	}	nxt = Pin( x, nxt ); //9
			if ( loc == nxt ){		name = "Magincia";				x=3429;		y=2427;	}	nxt = Pin( x, nxt ); //10
			if ( loc == nxt ){		name = "Moonglow";				x=4153;		y=1982;	}	nxt = Pin( x, nxt ); //11
			if ( loc == nxt ){		name = "Lyceum";	d = 1;		x=3865;		y=1627;	}	nxt = Pin( x, nxt ); //12
			if ( loc == nxt ){		name = "Zoo";					x=4048;		y=1681;	}	nxt = Pin( x, nxt ); //13

			if ( x < 1 ){ hue++; }

			if ( loc == nxt ){		name = "Deceit";	d = 2;		x=4194;		y=1308;	}	nxt = Pin( x, nxt ); //14
			if ( loc == nxt ){		name = "Despise";				x=1649;		y=1223;	}	nxt = Pin( x, nxt ); //15
			if ( loc == nxt ){		name = "Destard";				x=1536;		y=2636;	}	nxt = Pin( x, nxt ); //16
			if ( loc == nxt ){		name = "Wrong";					x=2403;		y=625;	}	nxt = Pin( x, nxt ); //17
			if ( loc == nxt ){		name = "Covetous";				x=2888;		y=658;	}	nxt = Pin( x, nxt ); //18
			if ( loc == nxt ){		name = "Shame";					x=1383;		y=1741;	}	nxt = Pin( x, nxt ); //19
			if ( loc == nxt ){		name = "Hythloth";				x=4240;		y=3397;	}	nxt = Pin( x, nxt ); //20
			if ( loc == nxt ){		name = "Stygian Abyss";	d = 1;	x=4209;		y=2962;	}	nxt = Pin( x, nxt ); //21
			if ( loc == nxt ){		name = "Terathan Keep";			x=5298;		y=1132;	}	nxt = Pin( x, nxt ); //22
			if ( loc == nxt ){		name = "Ancient Pyramid";		x=5494;		y=678;	}	nxt = Pin( x, nxt ); //23
			if ( loc == nxt ){		name = "Wailing Halls";			x=1548;		y=1081;	}	nxt = Pin( x, nxt ); //24
			if ( loc == nxt ){		name = "Exodus Ruins";			x=669;		y=3480;	}	nxt = Pin( x, nxt ); //25
			if ( loc == nxt ){		name = "Silent Tomb";	d = 1;	x=2048;		y=1935;	}	nxt = Pin( x, nxt ); //26

			if ( x < 1 ){ hue++; s=0; }

			if ( loc == nxt ){		name = "Deep Forest";			x=1081;		y=1110;	}	nxt = Pin( x, nxt ); //27
			if ( loc == nxt ){		name = "Lock Lake";				x=2453;		y=1301;	}	nxt = Pin( x, nxt ); //28
			if ( loc == nxt ){		name = "Lost Hope Bay";			x=2569;		y=720;	}	nxt = Pin( x, nxt ); //29
			if ( loc == nxt ){		name = "High Steppes";			x=2297;		y=885;	}	nxt = Pin( x, nxt ); //30
			if ( loc == nxt ){		name = "Bog of Desolation";		x=1925;		y=2381;	}	nxt = Pin( x, nxt ); //31
			if ( loc == nxt ){		name = "Brittany Bay";			x=2017;		y=1721;	}	nxt = Pin( x, nxt ); //32
			if ( loc == nxt ){		name = "Drylands";				x=3685;		y=1017;	}	nxt = Pin( x, nxt ); //33
			if ( loc == nxt ){		name = "Bloody Plains";			x=3033;		y=825;	}	nxt = Pin( x, nxt ); //34
			if ( loc == nxt ){		name = "Isle of the Avatar";	x=3951;		y=3545;	}	nxt = Pin( x, nxt ); //35
			if ( loc == nxt ){		name = "Terfin";				x=3419;		y=3189;	}	nxt = Pin( x, nxt ); //36
			if ( loc == nxt ){		name = "Abandoned Isles";		x=1325;		y=3440;	}	nxt = Pin( x, nxt ); //37
			if ( loc == nxt ){		name = "Deadman Isles";			x=2437;		y=3273;	}	nxt = Pin( x, nxt ); //38
			if ( loc == nxt ){		name = "Valorian Isles";		x=997;		y=2641;	}	nxt = Pin( x, nxt ); //39
			if ( loc == nxt ){		name = "Spiritwood";			x=1061;		y=2001;	}	nxt = Pin( x, nxt ); //40
			if ( loc == nxt ){		name = "Isle of Fire";			x=537;		y=3273;	}	nxt = Pin( x, nxt ); //41
			if ( loc == nxt ){		name = "Verity Isle";			x=4053;		y=1801;	}	nxt = Pin( x, nxt ); //42
			if ( loc == nxt ){		name = "Dagger Isle";			x=4141;		y=960;	}	nxt = Pin( x, nxt ); //43
			if ( loc == nxt ){		name = "Janus Isle";			x=4268;		y=1245;	}	nxt = Pin( x, nxt ); //44
			if ( loc == nxt ){		name = "Spektran";				x=3193;		y=3001;	}	nxt = Pin( x, nxt ); //45
			if ( loc == nxt ){		name = "Cape of Heroes";		x=1770;		y=3161;	}	nxt = Pin( x, nxt ); //46
			if ( loc == nxt ){		name = "Serpent Isle";			x=5277;		y=858;	}	nxt = Pin( x, nxt ); //47
			if ( loc == nxt ){		name = "Restful Dunes";			x=6084;		y=2237;	}	nxt = Pin( x, nxt ); //48
			if ( loc == nxt ){		name = "Daemon Islands";		x=4888;		y=2105;	}	nxt = Pin( x, nxt ); //49
			if ( loc == nxt ){		name = "Isles of the Dead";		x=2185;		y=1928;	}	nxt = Pin( x, nxt ); //50

			if ( x < 1 ){ hue++; s=0; }

			if ( loc == nxt ){		name = "Humility";		d = 2;	x=4127;		y=3000;	}	nxt = Pin( x, nxt ); //51
			if ( loc == nxt ){		name = "Honesty";				x=4137;		y=1091;	}	nxt = Pin( x, nxt ); //52
			if ( loc == nxt ){		name = "Sacrifice";				x=3743;		y=927;	}	nxt = Pin( x, nxt ); //53
			if ( loc == nxt ){		name = "Justice";		d = 1;	x=1653;		y=548;	}	nxt = Pin( x, nxt ); //54
			if ( loc == nxt ){		name = "Compassion";			x=2488;		y=1482;	}	nxt = Pin( x, nxt ); //55
			if ( loc == nxt ){		name = "Spirituality";	d = 1;	x=1003;		y=1852;	}	nxt = Pin( x, nxt ); //56
			if ( loc == nxt ){		name = "Valor";			d = 1;	x=1244;		y=3411;	}	nxt = Pin( x, nxt ); //57
			if ( loc == nxt ){		name = "Honor";					x=1876;		y=2824;	}	nxt = Pin( x, nxt ); //58

			if ( x < 1 ){ hue++; s=1; }

			if ( loc == nxt ){		name = "Brigands";				x=1165;		y=1177;	}	nxt = Pin( x, nxt ); //59
			if ( loc == nxt ){		name = "Hobgoblins";			x=1231;		y=1059;	}	nxt = Pin( x, nxt ); //60
			if ( loc == nxt ){		name = "Cemetery";				x=1548;		y=1016;	}	nxt = Pin( x, nxt ); //61
			if ( loc == nxt ){		name = "Cemetery";				x=2655;		y=524;	}	nxt = Pin( x, nxt ); //62
			if ( loc == nxt ){		name = "Savages";		d = 1;	x=5648;		y=626;	}	nxt = Pin( x, nxt ); //63
			if ( loc == nxt ){		name = "Savages";				x=5774;		y=1301;	}	nxt = Pin( x, nxt ); //64
			if ( loc == nxt ){		name = "Graveyard";				x=3432;		y=1041;	}	nxt = Pin( x, nxt ); //65
			if ( loc == nxt ){		name = "Brigands";				x=3074;		y=1120;	}	nxt = Pin( x, nxt ); //66
			if ( loc == nxt ){		name = "Orcs";					x=2209;		y=819;	}	nxt = Pin( x, nxt ); //67
			if ( loc == nxt ){		name = "Cemetery";				x=547;		y=1758;	}	nxt = Pin( x, nxt ); //68
			if ( loc == nxt ){		name = "Ratmen";				x=1291;		y=2253;	}	nxt = Pin( x, nxt ); //69
			if ( loc == nxt ){		name = "Brigands";				x=4245;		y=1721;	}	nxt = Pin( x, nxt ); //70
			if ( loc == nxt ){		name = "Cemetery";				x=4012;		y=1879;	}	nxt = Pin( x, nxt ); //71
			if ( loc == nxt ){		name = "Hobgoblins";			x=5747;		y=1869;	}	nxt = Pin( x, nxt ); //72
			if ( loc == nxt ){		name = "Savages";				x=6386;		y=1744;	}	nxt = Pin( x, nxt ); //73
			if ( loc == nxt ){		name = "Cemetery";		d = 1;	x=3517;		y=2363;	}	nxt = Pin( x, nxt ); //74
			if ( loc == nxt ){		name = "Brigands";				x=2078;		y=2904;	}	nxt = Pin( x, nxt ); //75
			if ( loc == nxt ){		name = "Cemetery";		d = 2;	x=1888;		y=2658;	}	nxt = Pin( x, nxt ); //76
			if ( loc == nxt ){		name = "Ratmen";				x=1602;		y=2790;	}	nxt = Pin( x, nxt ); //77
			if ( loc == nxt ){		name = "Cemetery";				x=989;		y=3025;	}	nxt = Pin( x, nxt ); //78
			if ( loc == nxt ){		name = "Orcs";					x=2349;		y=3425;	}	nxt = Pin( x, nxt ); //79
			if ( loc == nxt ){		name = "Cemetery";				x=2203;		y=2104;	}	nxt = Pin( x, nxt ); //80
			if ( loc == nxt ){		name = "Spider Cave";			x=739;		y=1206;	}	nxt = Pin( x, nxt ); //81

			if ( x < 1 ){ hue++; s=0; }

			if ( loc == nxt ){		name = "Goblin Keep";			x=1951;		y=1274;	}	nxt = Pin( x, nxt ); //82
			if ( loc == nxt ){		name = "Plunderer's Port";		x=6589;		y=2370;	}	nxt = Pin( x, nxt ); //83

			if ( hue == 0 ){ c = "#FCFF00"; } 						// YELLOW	Towns
			else if ( hue == 1 ){ c = "#FF0000"; } 					// RED		Dungeons
			else if ( hue == 2 ){ c = "#00FF0C"; i = "false"; } 	// GREEN	Lands
			else if ( hue == 3 ){ c = "#00A8FF"; } 					// BLUE		Shrines
			else if ( hue == 4 ){ c = "#FFA200"; i = "false"; } 	// ORANGE 	Places	
			else if ( hue == 5 ){ c = "#D200FF"; } 					// PURPLE	Danger
			else if ( hue == 6 ){ c = "#00FFFF"; } 					// AQUA		???????

			string xx = ((int)(x/4)).ToString();
			string yy = ((int)(y/4)).ToString();

			if ( section == 1 )
				return xx;
			else if ( section == 2 )
				return yy;
			else if ( section == 3 )
				return c;
			else if ( section == 4 )
				return i;
			else if ( section == 5 )
				return d.ToString();
			else if ( section == 6 )
				return s.ToString();

			return name;
		}
	}

	public class MapGump : Gump
	{
		public MapGump( Mobile m ) : base( 0, 0 )
		{
			MapFunctions.SetLocations( (PlayerMobile)m );

			int cycle = MapFunctions.NumLocations();
			int spot = 0;
			int xx = 30;
			int yy = 5;
			int x = 0;
			int y = 0;

            this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			this.Resizable=false;

			AddPage(0);
			AddImage(xx, yy, 12105);
			AddImage(0, 0, 12104);

			bool icon = true;
			int d = 0;
			int s = 0;
			string s1 = "";
			string s2 = "";

			while ( cycle > 0 )
			{
				cycle--;
				spot++;

				if ( MapFunctions.HasLocation( (PlayerMobile)m, spot ) )
				{
					x = xx-7 + int.Parse( MapFunctions.LocationInfo( spot, 1 ) );
					y = yy-7 + int.Parse( MapFunctions.LocationInfo( spot, 2 ) );
					icon = bool.Parse( MapFunctions.LocationInfo( spot, 4 ) );
					d = int.Parse( MapFunctions.LocationInfo( spot, 5 ) );
					s = int.Parse( MapFunctions.LocationInfo( spot, 6 ) );

					if ( x > 0 )
					{
						s1 = "<BIG>";
						s2 = "</BIG>";
						if ( s == 1 )
						{
							s1 = "";
							s2 = "";
						}

						if ( icon ){ AddImage(x, y, 12106); x=x+15; }
						if ( d == 1 ){ 		x = x - 16;		y = y - 18;	}
						else if ( d == 2 ){ x = x - 15;		y = y + 12;	}
						AddHtml( x, y, 200, 48, @"<BODY><BASEFONT Color=" + MapFunctions.LocationInfo( spot, 3 ) + ">" + s1 + MapFunctions.LocationInfo( spot, 0 ) + s2 + "</BASEFONT></BODY>", (bool)false, (bool)false);
					}
				}
			}
		}
	}
}
