package data.scripts.world;

import java.util.List;
import java.util.ArrayList;
import java.awt.Color;

import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.Script;
import com.fs.starfarer.api.campaign.CampaignFleetAPI;
import com.fs.starfarer.api.campaign.FleetAssignment;
import com.fs.starfarer.api.campaign.LocationAPI;
import com.fs.starfarer.api.campaign.SectorAPI;
import com.fs.starfarer.api.campaign.SectorEntityToken;
import com.fs.starfarer.api.campaign.StarSystemAPI;
import com.fs.starfarer.api.campaign.CargoAPI;
import com.fs.starfarer.api.campaign.CargoAPI.CrewXPLevel;
import com.fs.starfarer.api.fleet.FleetMemberType;

//import data.scripts.ShipList;

public class ShipBuildersGuildSpawnPoint extends BaseSpawnPoint 
{
 public ShipBuildersGuildSpawnPoint(SectorAPI sector, LocationAPI location,float daysInterval, int maxFleets,SBGDatabase sbgd) 
	{super(sector, location, daysInterval, maxFleets, null);sbg_database=sbgd;}

	private SBGDatabase sbg_database;

	@Override
	protected CampaignFleetAPI spawnFleet() 
	{
			 
	 float angle = (float) ((float) Math.random() * Math.PI * 2f);
	 float x = (float) (Math.cos(angle) * 15000f);
	 float y = (float) (Math.sin(angle) * 15000f);
	 SectorEntityToken spawnPoint = getLocation().createToken(x, y);					
													
	 StarSystemAPI system = getSector().getStarSystem("Corvus");
	
	
	 String station_name=select_station(system);
	 SectorEntityToken station=system.getEntityByName(station_name);
	  
	 if (station==null)	//prevent crash
		{
		 Global.getSectorAPI().addMessage("getEntityByName",new Color(255,0,255));
		 getSector().addMessage(station_name,new Color(255,0,255));
		 return null;
		}	 
	 CampaignFleetAPI fleet = getSector().createFleet("shipbuilders_guild", "trade_convoy");
	 getLocation().spawnFleet(spawnPoint, 0, 0, fleet);

	 getSector().addMessage("A Shipbuilders Guild fleet has arrived in the system and is headed to "+station_name);
	 Script arrived_script = select_script(station_name);			
	
	 fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, station, 1000,arrived_script);
	 fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, spawnPoint, 1000);	

	 CargoAPI cargo = fleet.getCargo();
	 sbg_database.add_random_weapons(getSector(),cargo,4);
	 sbg_database.add_wp_weapons(cargo,3);
	 sbg_database.add_hulls(cargo,8);
	 sbg_database.add_wings(cargo,3);

	 return fleet; 
	}

	public static String select_station(StarSystemAPI system)
	{
	 ArrayList available_stations=new ArrayList();
	 available_stations.add("Orbital Station");
	 available_stations.add("Corporate HQ");									 
	 available_stations.add("Hidden Base");	 
	 if (system.getEntityByName("Orbital Depot")!=null)	available_stations.add("Orbital Depot");
	 if (system.getEntityByName("Junk Yard")!=null)	available_stations.add("Junk Yard");
	 if (system.getEntityByName("Anchorage")!=null)	available_stations.add("Anchorage");
	 if (system.getEntityByName("Scrapyard")!=null)	available_stations.add("Scrapyard");
	 if (system.getEntityByName("Imperial Orbital Station")!=null)	available_stations.add("Imperial Orbital Station");
	 return (String)(available_stations.get((int)(Math.random()*available_stations.size())));
	}

	private Script select_script(String station_name)
	{
	 if (station_name=="Orbital Station")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Orbital Station and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Orbital Station"); 
				 trade(station);
				}
			};
	 else if (station_name=="Corporate HQ")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Corporate HQ and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Corporate HQ"); 
				 trade(station);
				}
			};
	 else if (station_name=="Hidden Base")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Hidden Base and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Hidden Base"); 
				 trade(station);
				}
			};
	 else if (station_name=="Orbital Depot")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Orbital Depot and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Orbital Depot"); 
				 trade(station);
				}
			};
	 else if (station_name=="Junk Yard")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Junk Yard and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Junk Yard"); 
				 trade(station);
				}
			};
	 else if (station_name=="Scrapyard")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Scrapyard and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Scrapyard"); 
				 trade(station);
				}
			};
	  else if (station_name=="Anchorage")
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Anchorage and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Anchorage"); 
				 trade(station);
				}
			};
	  else 
		return new Script() {
			 public void run() 
				{Global.getSectorAPI().addMessage("A Shipbuilders Guild fleet has traded with the Imperial Orbital Station and is leaving the system");
				 StarSystemAPI system = getSector().getStarSystem("Corvus");SectorEntityToken station=system.getEntityByName("Imperial Orbital Station"); 
				 trade(station);
				}
			};
	}
			

   private void trade(SectorEntityToken station)
   {
	CargoAPI cargo=station.getCargo();
	float f;
	f=cargo.getSupplies();	if (f>2000) cargo.removeSupplies(f-2000);
    f=cargo.getFuel();		if (f>500) cargo.removeFuel(f-500);
    int i;
	i=cargo.getCrew(CrewXPLevel.GREEN);		if (i>3000) cargo.removeCrew(CrewXPLevel.GREEN,i-3000);
	i=cargo.getCrew(CrewXPLevel.REGULAR);	if (i>2000) cargo.removeCrew(CrewXPLevel.REGULAR,i-2000);
	i=cargo.getCrew(CrewXPLevel.VETERAN);	if (i>2000) cargo.removeCrew(CrewXPLevel.VETERAN,i-2000);
	i=cargo.getCrew(CrewXPLevel.ELITE);		if (i>1000) cargo.removeCrew(CrewXPLevel.ELITE,i-1000);
   	i=cargo.getMarines();					if (i>3000) cargo.removeMarines(i-3000);
   }

  


}