package data.scripts.world.corvus;

import java.util.List;

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.CargoAPI;
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.fleet.FleetMemberType;
import com.fs.starfarer.api.campaign.CargoAPI.CrewXPLevel;
import com.fs.starfarer.api.campaign.StarSystemAPI;

import data.scripts.world.BaseSpawnPoint;

@SuppressWarnings("unchecked")
public class AlienProphetSpawnPoint extends BaseSpawnPoint {

	public AlienProphetSpawnPoint(SectorAPI sector, LocationAPI location, 
							float daysInterval, int maxFleets, SectorEntityToken anchor) {
		super(sector, location, daysInterval, maxFleets, anchor);
	}

	
	@Override
	public CampaignFleetAPI spawnFleet() {
		Script script = null;
		CampaignFleetAPI fleet = null;
		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");
		SectorEntityToken station = system.getEntityByName("Outpost");
		if (station == null) 
		{
			fleet = getSector().createFleet("alien", "prophet");
			getLocation().spawnFleet(spawnPoint, 0, 0, fleet);
			CargoAPI cargo = fleet.getCargo();
			addCargo(cargo, 10);
			script = createArrivedScript();
			Global.getSectorAPI().addMessage("An alien prophet has been spotted in system heading for Corvus VII.");
			SectorEntityToken convoyDestination = system.getEntityByName("Corvus VII");
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION, convoyDestination, 1000, script);
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, convoyDestination, 1000);
		} 
		else 
		{
			fleet = getSector().createFleet("alien", "prophet");
			getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
			CargoAPI cargo = fleet.getCargo();
			addCargo(cargo, 1);
			addRandomShips(cargo, 2);
			script = createArrivedScript();
			Global.getSectorAPI().addMessage("A alien prophet has been spotted in system heading for their outpost orbiting Corvus VII.");
			SectorEntityToken convoyDestination = system.getEntityByName("Corvus VII");
			fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, station, 1000, script);
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		}

		getLocation().spawnFleet(spawnPoint, 0, 0, fleet);
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		
		return fleet;
	}
	private Script createArrivedScript() {
		return new Script() 
		{
			public void run() 
			{
				StarSystemAPI system = getSector().getStarSystem("Corvus");
				SectorEntityToken station = system.getEntityByName("Outpost");
				if (station == null) 
				{
					SectorEntityToken CorvusVII = system.getEntityByName("Corvus VII");
					station = system.addOrbitalStation(CorvusVII, 45, 1500, 30, "Outpost", "alien");
					Global.getSectorAPI().addMessage("The Aliens have been detected launching an orbital station from Corvus VII.");
					CargoAPI cargo = station.getCargo();
					addCargo(cargo, 10);
					cargo.addMothballedShip(FleetMemberType.SHIP, "behemoth_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "mephisto_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "mephisto_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "cerebus_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "cerebus_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "magister_Hull", null);
					cargo.addMothballedShip(FleetMemberType.SHIP, "magister_Hull", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "phobos_wing", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "phobos_wing", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "nipper_wing", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "nipper_wing", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "demos_wing", null);
					cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "demos_wing", null);
				}
				else 
				{
					Global.getSectorAPI().addMessage("An alien prophet has delivered new equipment to their outpost");
					CargoAPI cargo = station.getCargo();
					float supplies = cargo.getSupplies();
					if (supplies > 2000) 
					{
						supplies = supplies - 2000;
						cargo.removeSupplies(supplies);
					}
					supplies = cargo.getFuel();
					if (supplies > 1000) 
					{
						supplies = supplies - 1000;
						cargo.removeFuel(supplies);
					}
					int quantity = cargo.getCrew(CrewXPLevel.REGULAR);
					if (quantity > 1000) 
					{
						quantity = quantity - 1000;
						cargo.removeCrew(CrewXPLevel.REGULAR, quantity);
					}
					quantity = cargo.getCrew(CrewXPLevel.VETERAN);
					if (quantity > 500) 
					{
						quantity = quantity - 500;
						cargo.removeCrew(CrewXPLevel.VETERAN, quantity);
					}
					quantity = cargo.getCrew(CrewXPLevel.ELITE);
					if (quantity > 500) 
					{
						quantity = quantity - 500;
						cargo.removeCrew(CrewXPLevel.ELITE, quantity);
					}
					quantity = cargo.getMarines();
					if (quantity > 1000) 
					{
						quantity = quantity - 1000;
						cargo.removeMarines(quantity);
					}
				}
			}
		};
	}
	
	private void addCargo(CargoAPI cargo, int count) 
	{
		cargo.addCrew(CrewXPLevel.ELITE, 25);
		cargo.addCrew(CrewXPLevel.VETERAN, 50);
		cargo.addCrew(CrewXPLevel.REGULAR, 70);
	}
	
	private void addRandomShips(CargoAPI cargo, int count) 
	{
		for (int i = 0; i < count; i++) 
		{
				String wing = (String) wings[(int) (wings.length * Math.random())];
				cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, wing, null);
				String ship = (String) ships[(int) (ships.length * Math.random())];
				cargo.addMothballedShip(FleetMemberType.SHIP, ship, null);
		}
	}

	private static String [] ships = { 
									"behemoth_Hull",
									"cerebus_Hull",
									"mephisto_Hull",
									"magister_Hull",
									};

	private static String [] wings = { 
									"demos_wing",
									"phobos_wing",
									"nipper_wing",
									};
}