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;

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

	private final SectorEntityToken convoyDestination;

	public LotusPiratesConvoySpawnPoint(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("lotus_pirates", type);
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		
		if (type.equals("supplyConvoy")) {
			CargoAPI cargo = fleet.getCargo();
			addRandomWeapons(cargo, 5);
		}		
		else if (type.equals("personnelConvoy")) {
			CargoAPI cargo = fleet.getCargo();
			cargo.addCrew(CrewXPLevel.ELITE, 25);
      cargo.addCrew(CrewXPLevel.VETERAN, 200);
		}
		
		addRandomShips(fleet.getCargo(), (int) (Math.random() * 4f));
		
		Script script = null;
		if (type.equals("supplyConvoy")) {
			script = createArrivedScript();
			Global.getSectorAPI().addMessage("A supply fleet has arrived for the Lotus Conglomerate Pirates, bound for rendezvous with their 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 Lotus Conglomerate supply convoy has delivered new equipment to their station");
			}
		};
	}
	
	private void addRandomWeapons(CargoAPI cargo, int count) {
		List weaponIds = getSector().getAllWeaponIds();
		for (int i = 0; i < count; i++) {
      String weapon = (String) weapons[(int) (weapons.length * Math.random())];
			int quantity = (int)(Math.random() * 4f + 2f);
			cargo.addWeapons(weapon, quantity);
			
		}
	}
	
	private void addRandomShips(CargoAPI cargo, int count) {
		List weaponIds = getSector().getAllWeaponIds();
		for (int i = 0; i < count; i++) {
			if ((float) Math.random() > 0.4f) {
				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 = { 
                                      "blizzard_Hull",
									  "ajax_Hull",
				                      "hydra_Hull",
									  "rive_Hull",
				                      "eventide_Hull",
									  "jackal_Hull",
									  "vanquish_Hull",
									  "stingray_Hull",
									  "arcadia_Hull",
									  "argos_Hull",
									  "aria_Hull",
									};

	private static String [] wings = { 
									"stigma_wing",
									"javelin_wing",
									"cutlass_wing",
									"bolt_wing",
									
									};
	
	private static String [] weapons = { 
									"annihilator", 
									"annihilatorpod", 
									"arbalest", 
									"atropos", 
									"atropos_single", 
									"bomb", 
									"clusterbomb", 
									"chaingun", 
									"cyclone", 
									"dualflak", 
									"flak", 
									"fragbomb", 
									"gauss", 
									"harpoon", 
									"harpoon_single",
									"harpoonpod", 
									"hdstinger", 
									"heatseeker", 
									"heavyac", 
									"heavymauler", 
									"heavymg", 
									"heavyneedler", 
									"hellbore", 
									"hephag", 
									"hurricane", 
									"hveldriver", 
									"lightac", 
									"lightag", 
									"lightdualac", 
									"lightmg", 
									"lightmortar", 
									"lightneedler",
									"mjolnir", 
									"multineedler", 
									"phasecl", 
									"pilum", 
									"railgun", 
									"reaper", 
									"revolver", 
									"sabot", 
									"sabot_single",
									"sabotpod", 
									"salamanderpod", 
									"shredder", 
									"stinger_single",
									"stinger", 
									"stingerlauncher", 
									"swarmer", 
									"typhoon", 
									"vulcan", 
									};
									
}



