using System;
using System.Collections;
using System.Drawing;

namespace EvoXP_Server
{
	/// <summary>
	/// Summary description for Waypoint.
	/// </summary>
	[Serializable]
	public class Waypoint
	{
		public GalaxyNode _GalNode = null;
		public Point _alt_pos = new Point(0,0);
		public ArrayList _TransactionsBuy = new ArrayList();
		public ArrayList _TransactionsSell = new ArrayList();
		public Waypoint _Next = null;

		public string _Route = "def";
		public string _Desc;

		public Waypoint(string sys, string desc)
		{
			_Route = sys;
			_Desc = desc;
		}

		private void ProcessTransactions(TradeShip TS)
		{
			if(_GalNode == null) return;
			if(_GalNode._tradeDepot == null) return;
			if(TS==null) return;
			int i;
			for(i=0;i<_TransactionsSell.Count;i++)
			{
				WaypointTransaction WT = _TransactionsSell[i] as WaypointTransaction;
				WT.Execute(_GalNode._tradeDepot, TS);
			}
			for(i=0;i<_TransactionsBuy.Count;i++)
			{
				WaypointTransaction WT = _TransactionsBuy[i] as WaypointTransaction;
				WT.Execute(_GalNode._tradeDepot, TS);
			}
		}


		public Point GetDest()
		{
			if(_GalNode != null)
			{
				return _GalNode.GetPosition();
			}
			return _alt_pos;
		}

		public Waypoint OnTravel(TradeShip ts, int nx, int ny)
		{
			if(_GalNode != null)
			{
				int gx = _GalNode.GetPosition().X;
				int gy = _GalNode.GetPosition().Y;
				int dist = (gx - nx)*(gx - nx) + (gy - ny)*(gy - ny);
				if(dist<2)
				{
					ProcessTransactions(ts);
					return _Next;
				}
			}
			else
			{
				int gx = _alt_pos.X;
				int gy = _alt_pos.Y;
				int dist = (gx - nx)*(gx - nx) + (gy - ny)*(gy - ny);
				if(dist<2)
				{
					return _Next;
				}
			}
			
			return this;
		}
	}
}
