using System;
using System.IO;
using Server;

namespace Server.Misc
{
	public class Strandedness
	{
		private static Point2D[] m_Britannia = new Point2D[]
			{
				new Point2D( 582, 874 ),
				new Point2D( 606, 924 ),
				new Point2D( 723, 1026 ),
				new Point2D( 683, 1125 ),
				new Point2D( 731, 1210 ),
				new Point2D( 873, 1191 ),
				new Point2D( 952, 1221 ),
				new Point2D( 974, 1268 ),
				new Point2D( 970, 1319 ),
				new Point2D( 880, 1276 ),
				new Point2D( 758, 1360 ),
				new Point2D( 637, 1495 ),
				new Point2D( 690, 1596 ),
				new Point2D( 769, 1685 ),
				new Point2D( 905, 1765 ),
				new Point2D( 922, 1875 ),
				new Point2D( 934, 2060 ),
				new Point2D( 989, 2165 ),
				new Point2D( 1025, 2310 ),
				new Point2D( 1073, 2487 ),
				new Point2D( 1189, 2536 ),
				new Point2D( 1357, 2588 ),
				new Point2D( 1452, 2668 ),
				new Point2D( 1578, 2717 ),
				new Point2D( 1696, 2834 ),
				new Point2D( 1692, 2989 ),
				new Point2D( 1830, 3088 ),
				new Point2D( 1997, 3190 ),
				new Point2D( 1966, 3042 ),
				new Point2D( 1978, 2933 ),
				new Point2D( 2060, 3060 ),
				new Point2D( 2147, 2980 ),
				new Point2D( 2199, 2824 ),
				new Point2D( 2086, 2466 ),
				new Point2D( 2088, 2274 ),
				new Point2D( 1861, 2033 ),
				new Point2D( 1777, 1946 ),
				new Point2D( 1682, 1816 ),
				new Point2D( 2418, 1566 ),
				new Point2D( 2536, 1579 ),
				new Point2D( 2671, 1578 ),
				new Point2D( 2844, 1511 ),
				new Point2D( 3052, 1430 ),
				new Point2D( 3134, 1346 ),
				new Point2D( 3269, 1311 ),
				new Point2D( 3549, 1118 ),
				new Point2D( 3607, 1217 ),
				new Point2D( 3736, 1198 ),
				new Point2D( 3874, 1148 ),
				new Point2D( 3957, 964 ),
				new Point2D( 4091, 886 ),
				new Point2D( 4037, 771 ),
				new Point2D( 3823, 736 ),
				new Point2D( 3620, 725 ),
				new Point2D( 3481, 609 ),
				new Point2D( 3364, 561 ),
				new Point2D( 3192, 562 ),
				new Point2D( 3071, 580 ),
				new Point2D( 2515, 629 ),
				new Point2D( 2370, 533 ),
				new Point2D( 2029, 592 ),
				new Point2D( 1874, 565 ),
				new Point2D( 1673, 640 ),
				new Point2D( 1534, 676 ),
				new Point2D( 1473, 551 ),
				new Point2D( 1389, 498 ),
				new Point2D( 1301, 438 ),
				new Point2D( 1225, 577 ),
				new Point2D( 1153, 677 ),
				new Point2D( 1026, 783 ),
				new Point2D( 1008, 887 ),
				new Point2D( 805, 923 )
			};

		public static void Initialize()
		{
			EventSink.Login += new LoginEventHandler( EventSink_Login );
		}

		private static bool IsStranded( Mobile from )
		{
			Map map = from.Map;

			if ( map != Map.Britannia )
				return false;

			object surface = map.GetTopSurface( from.Location );

			if ( surface is LandTile ) {
				int id = ((LandTile)surface).ID;

				return (id >= 168 && id <= 171)
					|| (id >= 310 && id <= 311);
			} else if ( surface is StaticTile ) {
				int id = ((StaticTile)surface).ID;

				return (id >= 0x1796 && id <= 0x17B2);
			}

			return false;
		}

		public static void EventSink_Login( LoginEventArgs e )
		{
			Mobile from = e.Mobile;

			if ( !IsStranded( from ) )
				return;

			Map map = from.Map;

			Point2D[] list = m_Britannia;

			Point2D p = Point2D.Zero;
			double pdist = double.MaxValue;

			for ( int i = 0; i < list.Length; ++i )
			{
				double dist = from.GetDistanceToSqrt( list[i] );

				if ( dist < pdist )
				{
					p = list[i];
					pdist = dist;
				}
			}

			int x = p.X, y = p.Y;
			int z;
			bool canFit = false;

			z = map.GetAverageZ( x, y );
			canFit = map.CanSpawnMobile( x, y, z );

			for ( int i = 1; !canFit && i <= 40; i += 2 )
			{
				for ( int xo = -1; !canFit && xo <= 1; ++xo )
				{
					for ( int yo = -1; !canFit && yo <= 1; ++yo )
					{
						if ( xo == 0 && yo == 0 )
							continue;

						x = p.X + (xo * i);
						y = p.Y + (yo * i);
						z = map.GetAverageZ( x, y );
						canFit = map.CanSpawnMobile( x, y, z );
					}
				}
			}

			if ( canFit )
				from.Location = new Point3D( x, y, z );
		}
	}
}