using System;
using System.Data;
using System.Collections;
using System.Windows.Forms;

namespace EvoXP
{
	/// <summary>
	/// Summary description for EconomyClient.
	/// </summary>
	public class EconomyClient
	{
		public GameClient _gameclient = null;
		public WaypointRepository _Waypoints = new WaypointRepository();
		public TradeDepotRepository _TradeDepots = new TradeDepotRepository();
		public ShipRepository _LocalShips = new ShipRepository();
		public ArrayList _LocalGalaxy = new ArrayList();
		public double _balance;

		public EconomyClient(GameClient gc)
		{
			_gameclient = gc;
		}

		public void OnMessageDetail(string[] str)
		{
			if(str[0]=="START")
			{
				_gameclient.StartData();
				int n_td = Int32.Parse(str[1]);
				int n_c = Int32.Parse(str[2]);
				if(n_td >= 1 && n_c >= 1)
				{
					_TradeDepots._depot_names = new string[n_td];
					_TradeDepots._taxes = new double[n_td];
					_TradeDepots._comm_names = new string[n_c];
					_TradeDepots._values = new double[n_td * n_c];
					
					int i;
					_TradeDepots._RelativeData.Columns.Add("Name");
					for(i=0;i<n_c;i++)
					{
						_TradeDepots._comm_names[i] = str[i+3+n_td];
						_TradeDepots._RelativeData.Columns.Add(_TradeDepots._comm_names[i]);
					}
					_TradeDepots._RelativeData.Columns.Add("Taxes");
					for(i=0;i<n_td;i++)
					{
						_TradeDepots._depot_names[i] = str[i+3];
						_TradeDepots._map.Add(_TradeDepots._depot_names[i], i);
						string[] tVal = new string[n_c+2];
						tVal[0] = _TradeDepots._depot_names[i];
						int j;
						for(j=0;j<n_c+1;j++)
						{
							tVal[j+1] = "na";
						}
						DataRow nr =  _TradeDepots._RelativeData.NewRow();
						nr.ItemArray = tVal;
						_TradeDepots._RelativeData.Rows.Add(nr);
					}
					for(i=0;i<n_c * n_td;i++)
					{
						_TradeDepots._values[i] = -1;
					}
					_gameclient.WriteString("BEGIN_TD<end>");
				}
			} 
			else if(str[0]=="UTD")
			{
				int td = Int32.Parse(str[1]);
				int c = Int32.Parse(str[2]);
				double v = Double.Parse(str[3]);
				_TradeDepots.SetValue(td, c, v);
			}
			else if(str[0]=="UTT")
			{
				int td = Int32.Parse(str[1]);
				_TradeDepots.SetTaxes(td, Double.Parse(str[2]));
			}
			else if(str[0]=="DONE_TD")
			{
				_gameclient.WriteString("BEGIN_G<end>");
			}
			else if(str[0]=="UG")
			{
				//MessageBox.Show(str[1]);
				LocalGalaxyDetail LGD = new LocalGalaxyDetail();
				LGD.name = str[1];
				LGD.x = Int32.Parse(str[2]);
				LGD.y = Int32.Parse(str[3]);
				LGD.isTradeDepot = false;
				LGD.isJumpGate = false;
				LGD.isShipYard = false;
				if(str[4]=="T") LGD.isTradeDepot = true;
				if(str[5]=="T") LGD.isJumpGate = true;
				if(str[6]=="T") LGD.isShipYard = true;
				_LocalGalaxy.Add(LGD);
			}
			else if(str[0]=="DONE_G")
			{
				_gameclient.WriteString("BEGIN_REST:<end>");
			}
			else if(str[0]=="DONE_INIT")
			{
				_gameclient.GotData();
			}
			else if( str[0]=="SHIP" )
			{
				_LocalShips.IncomingShip( str[1], Double.Parse(str[2]), Double.Parse(str[3]), Double.Parse(str[4]), Int32.Parse(str[5]), Int32.Parse(str[6]), str[7]);
			} 
			else if( str[0] == "USH" )
			{
				_LocalShips.UpdateShip(str[1], Double.Parse(str[2]), Double.Parse(str[3]), Int32.Parse(str[4]), str[5]);
			}
			else if( str[0] == "RSH" )
			{
				_LocalShips.RemoveShip(str[1]);
			} 
			else if( str[0] == "BAL")
			{
				_balance = Double.Parse(str[1]);
			}
			else if( str[0] == "ERROR")
			{
				_gameclient.OnError(str[1]);
			}
			else if(str[0] == "R_NEW")
			{
				_Waypoints.NewRoute(str[1]);
			}
			else if(str[0] == "R_DEL")
			{
				_Waypoints.DelRoute(str[1]);
			}
			else if(str[0]=="W_ADD")
			{
				_Waypoints.AddWP(str[1], str[2]);
			}
			else if(str[0]=="W_DEL")
			{
				_Waypoints.DelWP(str[1], Int32.Parse(str[2]));
			}
			else if(str[0]=="WT_ADD")
			{
				_Waypoints.AddWPT(str[1], Int32.Parse(str[2]), str[3], Int32.Parse(str[4]), str[5]=="T", str[6]=="T", str[7]=="T", str[8]=="T", Int32.Parse(str[9]));
			}
			else if(str[0]=="WT_DEL")
			{
				_Waypoints.DelWPT(str[1], Int32.Parse(str[2]), Int32.Parse(str[3]), str[4]=="T");
			}
		}
	}
}
