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 GRConvoySpawnPoint extends BaseSpawnPoint {

	public GRConvoySpawnPoint(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("Orbital Depot");
		if (station == null) {
		fleet = getSector().createFleet("gunrunners", "supplyConvoy");
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		CargoAPI cargo = fleet.getCargo();
    addCargo(cargo, 10);
		script = createArrivedScript();
		Global.getSectorAPI().addMessage("A convoy of known weapon smugglers has been spotted in system heading for Corvus I.");
		SectorEntityToken convoyDestination = system.getEntityByName("Corvus I");
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION, convoyDestination, 1000, script);
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, convoyDestination, 1000);
		} else {
		fleet = getSector().createFleet("gunrunners", "supplyConvoy");
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		CargoAPI cargo = fleet.getCargo();
    addCargo(cargo, 5);
    addRandomShips(cargo, 10);
		script = createArrivedScript();
		Global.getSectorAPI().addMessage("A Gun Runners convoy has been spotted in system heading for their outpost orbiting Corvus I.");
		SectorEntityToken convoyDestination = system.getEntityByName("Corvus I");
		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("Orbital Depot");
        if (station == null) {
          SectorEntityToken CorvusV = system.getEntityByName("Corvus I");
          SectorEntityToken CorvusIIIA = system.getEntityByName("Corvus IIIA");
          station = system.addOrbitalStation(CorvusV, 45, 300, 30, "Orbital Depot", "gunrunners");
          Global.getSectorAPI().addMessage("The Gun Runners have been detected launching an orbital station from Corvus I.");
          Global.getSectorAPI().addMessage("Hegemony command warns that dealing with such criminals is dangerous.");
          CargoAPI cargo = station.getCargo();
          addCargo(cargo, 10);
          system.addSpawnPoint(new GRDefenseSpawnPoint(getSector(), system, 7, 2, station));
          GRRaiderSpawnPoint raidSpawn = new GRRaiderSpawnPoint(getSector(), system, 5, 7, station);
          system.addSpawnPoint(raidSpawn);
          for (int i = 0; i < 5; i++) raidSpawn.spawnFleet();
          system.addSpawnPoint(new GRIndySpawnPoint(getSector(), system, 5, 5));
          system.addSpawnPoint(new GRPirateSpawnPoint(getSector(), system, 5, 5, CorvusIIIA));
          cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "hpardon", 1);
          cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
          cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
          cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "gpardon", 1);
          cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ppardon", 1);
		  cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "npardon", 1);//test nomad pardon
		  cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "jpardon", 1);//test junk pirate pardon
		  cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "lpardon", 1);//test lotus pirate pardon
		  cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "spardon", 1);//scrappers pardon
		  cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "trpardon", 1);//Traders pardon
			cargo.addMothballedShip(FleetMemberType.SHIP, "riht_support", null);
		    cargo.addMothballedShip(FleetMemberType.SHIP, "lyft_assault", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Hades_Assault", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Hades_Assault", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Hades_FS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Hades_FS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Helios_FS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Helios_FS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Helios_FS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_Attack", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_Attack", null);
            cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_CS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_CS", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_PD", null);
	        cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Gorgon_PD", null);
        	cargo.addMothballedShip(FleetMemberType.SHIP, "GR_Paria_FS", null);
        	cargo.addMothballedShip(FleetMemberType.SHIP, "oort_wp", null);//my test codes
			cargo.addMothballedShip(FleetMemberType.SHIP, "aurora2_Standard", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "nom_wurm_assault", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "assel_default", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "dragoon_default", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "tipi_fs", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "aghast_attack", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "nom_komodo_assault", null);//end of test codes
			cargo.addMothballedShip(FleetMemberType.SHIP, "knifedge_cutter", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "knifedge_cutter", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "trident_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "trident_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "xyphos_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "xyphos_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "xyphos_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "wasp_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "wasp_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "wasp_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "wasp_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "dagger_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "dagger_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "dagger_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "dagger_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "thunder_wing", null);
        	cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "thunder_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "gladius_wing", null);
	        cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "gladius_wing", 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, "piranha_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "nom_scarab_wing", null);//test codes
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "nom_scarab_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "sparrow_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "sparrow_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "deco_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "deco_wing", null);//end of test codes
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_battleborn_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_beast_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_bite_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_juggernaut_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_manta_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_menace_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_refuge_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_striker_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_terra_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_turtle_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_vice_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hobbler_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "mended_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "hadd_vice_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "grizzly_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "boomer_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "scorcher_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "boom_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "chiron_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "phalanx_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "snap_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "tourbus_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "vermin_Hull", null);
			cargo.addMothballedShip(FleetMemberType.SHIP, "redoubt_Hull", null);
   
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_sparrow_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_sparrow_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_bat_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_bat_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_raptor_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_raptor_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_platypus_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_platypus_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_blade_wing", null);
			cargo.addMothballedShip(FleetMemberType.FIGHTER_WING, "hadd_blade_wing", null);
			
				} else {
          Global.getSectorAPI().addMessage("A Gun Runner convoy has delivered new equipment to their 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, 25);
    cargo.addCrew(CrewXPLevel.VETERAN, 100);
	}
	
	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 = { 
									"GR_Hades_FS",
									"GR_Hades_Assault",
									"GR_Helios_FS",
									"GR_Gorgon_Attack",
									"GR_Gorgon_CS",
									"GR_Gorgon_PD",
									"GR_Paria_FS",
									"nom_iguana_assault",
									"nom_komodo_assault",
									"nom_wurm_assault",
									"aghast_attack",
									"assel_default",
									"centipede_lrs",
									"dragoon_strike",
									"goose_assault",
									"goosemk2_outdated",
									"mynar_strike",
									"oort_wp",
									"owl_ap",
									"scarab_storm",
									"sheperd_assault",
									"tipi_fs",
									"aurora2_Standard",
									"riht_support",
									"lyft_assault",
									"knifedge_cutter",
									};

	private static String [] wings = { 
									"xyphos_wing",
									"gladius_wing",
									"warthog_wing",
									"thunder_wing",
									"wasp_wing",
									"dagger_wing",
									"piranha_wing",
									"trident_wing",
									"nom_scarab_wing",
									"sparrow_wing",
									"deco_wing",
									};
									
}



