package data.scripts.world;

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;

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

	public TradeConvoySpawnPoint(SectorAPI sector, LocationAPI location, 
							float daysInterval, int maxFleets, SectorEntityToken anchor) {
		super(sector, location, daysInterval, maxFleets, anchor);
	}
    
     	@Override
	    protected CampaignFleetAPI spawnFleet() {
		Script script = null;
		CampaignFleetAPI fleet = null;
		
		StarSystemAPI system = getSector().getStarSystem("corvus");
		SectorEntityToken station = system.getEntityByName("Trade Station");
		if (station == null) {
		fleet = getSector().createFleet("Trade", "supplyConvoy");
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		CargoAPI cargo = fleet.getCargo();
		addCargo(cargo, 10);
		script = createArrivedScript();
		Global.getSectorAPI().addMessage("A Trade Scavengers expeditionary fleet is headed towards Fenris.");
		SectorEntityToken convoyDestination = system.getEntityByName("Fenris");
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION, convoyDestination, 1000, script);
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, convoyDestination, 1000);
		} else {
		fleet = getSector().createFleet("Trade", "supplyConvoy");
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		CargoAPI cargo = fleet.getCargo();
		addCargo(cargo, 5);
		addRandomShips(cargo, 10);
		script = createArrivedScript();
		Global.getSectorAPI().addMessage("A Trade Scavengers weapons detachment is headed for Fenris.");
		fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, station, 1000, script);
		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("Trade Station");
        if (station == null) {
          SectorEntityToken Fenris = system.getEntityByName("Fenris");
          station = system.addOrbitalStation(Fenris, 300, 300, 50, "Trade Station", "Trade");
          Global.getSectorAPI().addMessage("The Trade Scavengers have arrived and have launched an orbital station from Fenris.");
          CargoAPI cargo = station.getCargo();
          addCargo(cargo, 10);
          system.addSpawnPoint(new TradeSpawnPoint(getSector(), system, 7, 2, station));
          TradeSpawnPoint raidSpawn = new TradeSpawnPoint(getSector(), system, 5, 7, station);
          system.addSpawnPoint(raidSpawn);
          for (int i = 0; i < 3; i++) raidSpawn.spawnFleet();
            cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "valhalla_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "valhalla_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "bison_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "bison_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "genesis_Hull", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "genesis_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "einherjar_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "crawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "crawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "crawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "brawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "brawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "brawler_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "eviscerator_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "eviscerator_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "eviscerator_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "ostrich_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "ostrich_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "yak_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "yak_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "yak_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "scorpion_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "scorpion_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "scorpion_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "scorpion_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "lasher_fast_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "lasher_fast_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "lasher_fast_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hercules_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hades_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hades_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hades_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hades_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "hades_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "asphodel_Hull", null);
		cargo.addMothballedShip(FleetMemberType.SHIP, "asphodel_Hull", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "warthog_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "warthog_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "piranha_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "piranha_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "piranha_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "piranha_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "barracuda2_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "barracuda2_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "barracuda2_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "razor_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "roach_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "roach_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "roach_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "roach_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "roach_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "beetle_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "beetle_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "beetle_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "artemis_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "artemis_wing", null);
		cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "artemis_wing", null);

			        } else {
          Global.getSectorAPI().addMessage("A Trading convoy has delivered new equipment to their orbital station");
          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);}
          List weaponIds = getSector().getAllWeaponIds();
          for (int i = 0; i < weaponIds.size(); i++) {
            String weaponId = (String) weaponIds.get((int) i);
            quantity = cargo.getNumWeapons(weaponId);
            if (quantity > 20) {
              quantity = quantity - 20;
              cargo.removeWeapons(weaponId, quantity);}
          }
        }
			}
		};
	}
	
	private void addCargo(CargoAPI cargo, int count) {
		List weaponIds = getSector().getAllWeaponIds();
    for (int i = 0; i < weaponIds.size(); i++) {
      String weaponId = (String) weaponIds.get((int) i);
      cargo.addWeapons(weaponId, count);
    }
    cargo.addCrew(CrewXPLevel.ELITE, 500);
    cargo.addCrew(CrewXPLevel.VETERAN, 2000);
	cargo.addCrew(CrewXPLevel.GREEN, 5000);
	}
	
	private void addRandomShips(CargoAPI cargo, int count) {
		for (int i = 0; i < count; i++) {
			if ((float) Math.random() > 0.2f) {
				String wing = (String) wings[(int) (wings.length * Math.random())];
				cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, wing, null);
			} else {
				String ship = (String) ships[(int) (ships.length * Math.random())];
				cargo.addMothballedShip(FleetMemberType.SHIP, ship, null);
			}
		}
	}

	private static String [] ships = { 
									"einherjar_Hull",
									"valhalla_Hull",
									"bison_Hull",
									"genesis_Hull",
									"scorpion_Hull",
									"lasher_Hull",
									"lasher_fast_Hull",
									"hercules_Hull",
									"hades_Hull",
									"ostrich_Hull",
									"crawler_Hull",
									"yak_Hull",
									"asphodel_Hull",
									"eviscerator_Hull",
									"brawler_Hull",									
									};

	private static String [] wings = { 
									"warthog_wing",
									"wasp_wing",
									"piranha_wing",
									"barracuda2_wing",
									"razor_wing",
									"roach_wing",
									"beetle_wing",
									"artemis_wing",
									};
									
}



                                    



