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 data.scripts.world.BaseSpawnPoint;

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

	private final SectorEntityToken convoyDestination;

	public HegeConvoySpawnPoint(SectorAPI sector, LocationAPI location, 
							float daysInterval, int maxFleets, SectorEntityToken anchor,
							SectorEntityToken convoyDestination) {
		super(sector, location, daysInterval, maxFleets, anchor);
		this.convoyDestination = convoyDestination;
	}

	private static int convoyNumber = 0;
	
	@Override
	protected CampaignFleetAPI spawnFleet() {
		String type = null;
		float r = (float) Math.random();
		if (r > .9f) {
			type = "fuelConvoy";
		} else if (r > 0.6f) {
			type = "personnelConvoy";
		} else {
			type = "supplyConvoy";
		}
		
		CampaignFleetAPI fleet = getSector().createFleet("hegemony", type);
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		
		if (type.equals("supplyConvoy")) {
			convoyNumber++;
			CargoAPI cargo = fleet.getCargo();
			if (convoyNumber == 1) {
				cargo.addWeapons("heavymg", 5);
				cargo.addWeapons("dualflak", 5);
				cargo.addWeapons("heavyblaster", 5);
				cargo.addWeapons("lightdualac", 5);
				cargo.addWeapons("lightag", 5);
				cargo.addWeapons("lightneedler", 5);
				cargo.addWeapons("heavymauler", 5);
				cargo.addWeapons("gravitonbeam", 5);
				//cargo.addMothballedShip(type, variantOrWingId, optionalName)
			} else if (convoyNumber == 3) {
				cargo.addWeapons("pdburst", 3);
				cargo.addWeapons("irpulse", 5);
				cargo.addWeapons("taclaser", 5);
				cargo.addWeapons("pulselaser", 5);
				cargo.addWeapons("heavyburst", 5);
				cargo.addWeapons("hil", 3);
				cargo.addWeapons("hveldriver", 5);
				cargo.addWeapons("reaper", 5);
				//cargo.addMothballedShip(type, variantOrWingId, optionalName)
			} else if (convoyNumber == 5) {
				cargo.addWeapons("reaper", 5);
				cargo.addWeapons("heavyneedler", 5);
				cargo.addWeapons("hephag", 3);
				cargo.addWeapons("pilum", 5);
				cargo.addWeapons("typhoon", 5);
				cargo.addWeapons("autopulse", 3);
				cargo.addWeapons("heavyac", 5);
				//cargo.addMothballedShip(type, variantOrWingId, optionalName)
			} else if (convoyNumber == 7) {
				cargo.addWeapons("atropos", 5);
				cargo.addWeapons("phasebeam", 5);
				cargo.addWeapons("plasma", 3);
				cargo.addWeapons("guardian", 3);
				cargo.addWeapons("tachyonlance", 3);
			} else if (convoyNumber == 9) {
				cargo.addWeapons("mjolnir", 5);
				cargo.addWeapons("multineedler", 3);
				cargo.addWeapons("cyclone", 5);
				cargo.addWeapons("hurricane", 5);
				cargo.addWeapons("gauss", 5);				
			} else {
				addRandomWeapons(cargo, 5);
			}
		}
		
		addRandomShips(fleet.getCargo(), (int) (Math.random() * 4f));
		
		Script script = null;
		if (type.equals("supplyConvoy")) {
			script = createArrivedScript();
			Global.getSectorAPI().addMessage("A Hegemony supply convoy is in-system and under way to rendezvous with the station");
		}
		
		fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, convoyDestination, 1000, script);
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		
		return fleet;
	}
	
	private Script createArrivedScript() {
		return new Script() {
			public void run() {
				Global.getSectorAPI().addMessage("A Hegemony supply convoy has delivered new equipment to the orbital station");
			}
		};
	}
	
	private void addRandomWeapons(CargoAPI cargo, int count) {
		List weaponIds = getSector().getAllWeaponIds();
		for (int i = 0; i < count; i++) {
			String weaponId = (String) weaponIds.get((int) (weaponIds.size() * Math.random()));
			int quantity = (int)(Math.random() * 4f + 2f);
			cargo.addWeapons(weaponId, quantity);
			
		}
	}
	
	private void addRandomShips(CargoAPI cargo, int count) {
		List weaponIds = getSector().getAllWeaponIds();
				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 = { 
									"subjugator_Standard",
									"wolf_Assault",
									"hound_Assault",
									"hyperion_Strike",
									"shuttle_Attack",
									"lasher_Standard",
									"tempest_Attack",
									"brawler_Assault",
									"vigilance_FS",
									"dram_Light",
									"hammerhead_Balanced",
									"sunder_CS",
									"condor_FS",
									"enforcer_Assault",
									"buffalo_Standard",
									"gemini_Standard",
									"buffalo2_FS",
									"medusa_Attack",
									"falcon_Attack",
									"eagle_Assault",
									"venture_Balanced",
									"apogee_Balanced",
									"dominator_Outdated",
									"aurora_Balanced",
									"onslaught_Outdated",
									"astral_Elite",
									"atlas_Standard",
									"conquest_Elite",
									"paragon_Elite",
									"odyssey_Balanced",
									"fireant_assault",
									"gate_ancient",
									"creeper_assault",
									"bolter_Standard",
									"scarab_assault",
									"dragoon_default",
									"centipede_lrs",
									"assel_default",
									"stilletto_standard",
									"brawler2_standard",
									"ursidae_standard",
									"buckler_standard",
									"spearhead_standard",
									"spearhead2_standard",
									"axehead_standard",
									};

	private static String [] wings = { 
									"broadsword_wing",
									"xyphos_wing",
									"gladius_wing",
									"warthog_wing",
									"thunder_wing",
									"talon_wing",
									"wasp_wing",
									"dagger_wing",
									"piranha_wing",
									"trident_wing",
									"longbow_wing",
									"mining_drone_wing"
									};
									
}



