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

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

	private final SectorEntityToken convoyDestination;

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

	private 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("scrappers", type);
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		
		if (type.equals("supplyConvoy")) {
			convoyNumber++;
			CargoAPI cargo = fleet.getCargo();
			if (convoyNumber == 1) {
				cargo.addWeapons("hadd_dartergun", 10);
				cargo.addWeapons("hadd_infernorockets", 5); 
				cargo.addWeapons("hadd_microwavebeam", 5);  
				cargo.addWeapons("hadd_spines", 10); 
				cargo.addWeapons("hadd_pointsnap", 10); 
				cargo.addWeapons("hadd_spreadchaingun", 10); 
				cargo.addWeapons("hadd_volleyrocket", 10); 
				cargo.addWeapons("hadd_wrecker", 10); 
				cargo.addWeapons("hadd_pounderassaultgun", 10);
				cargo.addWeapons("hadd_pointmissiles", 5);
				cargo.addWeapons("hadd_hellfirerockets", 5);
			} else if (convoyNumber == 2) {
				cargo.addWeapons("hadd_flakbombardier", 3); 
				cargo.addWeapons("hadd_shredder", 3);
				cargo.addWeapons("hadd_titancannon", 1);
				cargo.addWeapons("hadd_deathbringer", 2);
				cargo.addWeapons("hadd_dancingblade", 2);
				cargo.addWeapons("hadd_worldshammer", 1);
				cargo.addWeapons("hadd_pyrostorm", 2);
			} else {
				addRandomWeapons(cargo, 1);
			}
		}
		
		addRandomShips(fleet.getCargo(), (int) (Math.random() * 4f));
		
		Script script = null;
		if (type.equals("supplyConvoy")) {
			script = createArrivedScript();
			Global.getSectorAPI().addMessage("A Scrapper supply convoy is in-system and under way to rendezvous with the Scrapyard");
		}
		
		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 Scrapper supply convoy has delivered new equipment to the Scrapyard");
			}
		};
	}
	
	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();
		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 = { 
									"hadd_battleborn_Hull",
									"hadd_beast_Hull",
									"hadd_bite_Hull",
									"hadd_juggernaut_Hull",
									"hadd_manta_Hull",
									"hadd_halberd_Hull",
									"hadd_array_Hull",
									"hadd_menace_Hull",
									"hadd_raptor_Hull",
									"hadd_refuge_Hull",
									"hadd_striker_Hull",
									"hadd_terra_Hull",
									"hadd_turtle_Hull",
									"hadd_vice_Hull",
									};

	private static String [] wings = { 
									"hadd_sparrow_wing",
									"hadd_raptor_wing",
									"hadd_bat_wing",
									"hadd_platypus_wing",
									"hadd_blade_wing",
									"hadd_fang_wing",
									};
									
}



