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

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

	private final SectorEntityToken convoyDestination;

	public PiratesConvoySpawnPoint(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("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 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 Pirates 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++) {
				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 = { 
									"parrot_assault",
									"dachsund_standard",
									"onslaught_Reconditioned",
									"sheperd_heavyblaster",
									"centipede_mrm",
									"hammerhead_Cutlass",
									"goosemk2_assault",
									"centipede_lrs",
									"goose_assault",
									"dragoon_default",
									"dragoon_strike",
									"enforcer_Outdated",
									"tarsus_Standard",
									"buffalo2_FS",
									"scarab_assault",
									"sheperd_assault",
									"scarab_autopulse",
									"scarab_storm",
                                    "lasher_Standard",
                                    "hound_Assault",
									"lasher_CS",
                                    "venture_Balanced",
                                    "condor_FS",
									"bloodhound_standard",
									"saintbernard_assault",
									"barnacle_assault",
									};

	private static String [] wings = { 
									"talon_wing",
									"piranha_wing",
									"broadsword_wing",
									"ballast_wing",
									};
	
	private static String [] weapons = { 
									"amblaster",
									"pdburst",
									"pdlaser",
									"heavyburst",
									"chaingun",
									"gravitonbeam",
									"pulselaser",
									"ioncannon",
									"taclaser",
									"lightmg",
									"lightac",
									"phasecl",
									"pilum",
									"sabot",
									"harpoon",
									"heatseeker",
									"annihilator",
									"swarmer",
									"atropos",
									"reaper",
									"hvy_laser",
									"lightblaster",
									"sabotsrmlauncher",
									"raptor",
									"guardian"
									};
									
}



