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

	public TTConvoySpawnPoint(SectorAPI sector, LocationAPI location, 
							float daysInterval, int maxFleets, SectorEntityToken anchor) {
		super(sector, location, daysInterval, maxFleets, anchor);
	}
	
	@Override
	protected CampaignFleetAPI spawnFleet() {
		StarSystemAPI system = getSector().getStarSystem("Corvus");
		SectorEntityToken convoyDestination = system.getEntityByName("Corporate HQ");
		CampaignFleetAPI fleet = null;
		Script dockScript = null;
		dockScript = createArrivedScript();
		
		
		String type = null;
		float r = (float) Math.random();
		if (r > .9f) {
			type = "fuelConvoy";
		} else if (r > 0.6f) {
			type = "personnelConvoy";
		} else {
			type = "supplyConvoy";
		}
		
		fleet = getSector().createFleet("tritachyon", 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() * 10f));
		
		if (type.equals("supplyConvoy")) {
			Global.getSectorAPI().addMessage("A Tri-Tachyon supply convoy is in-system and under way to rendezvous with their station");
		} else if (type.equals("personnelConvoy")) {
      Global.getSectorAPI().addMessage("A Tri-Tachyon personnel convoy is in-system and under way to rendezvous with their station");
		} else {
      Global.getSectorAPI().addMessage("A Tri-Tachyon fuel convoy is in-system and under way to rendezvous with their station");
		}
		
		fleet.addAssignment(FleetAssignment.DELIVER_RESOURCES, convoyDestination, 1000,dockScript);
		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 Tri-Tachyon convoy has delivered new equipment to their station");
				StarSystemAPI system = getSector().getStarSystem("Corvus");
				SectorEntityToken station = system.getEntityByName("Corporate HQ");
				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 addRandomWeapons(CargoAPI cargo, int count) {
		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) {
		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 = { 
									"owl_ap",
									"mynar_strike",
									"wolf_Assault",
									"wolf_CS",
									"hyperion_Strike",
									"wolf_PD",
									"wolf_Strike",
									"tempest_Attack",
									"sunder_CS",
									"medusa_CS",
									"medusa_Attack",
									"medusa_PD",
									"omen_PD",
									"apogee_Balanced",
									"aurora_Balanced",
									"astral_Elite",
									"atlas_Standard",
									"conquest_Elite",
									"paragon_Elite",
									"odyssey_Balanced",
									"pelican_support",
									"aurora2_Standard",
									"nevermore_Hull",
									"templar_Hull",
									"grizzly_Hull",
									"wraith_Hull",
									"imp_Hull",
									"moth_standard",
									"heavy_hyperion_standard",
									};

	private static String [] wings = { 
									"xyphos_wing",
									"gladius_wing",
									"wasp_wing",
									"dagger_wing",
									"trident_wing",
									"longsword_wing",
									};
	
	private static String [] weapons = { 
									"hvy_laser",
									"heavygravitonbeam",
									"hvy_amblaster",
									"impalerburst",
									"dualheavyblaster",
									"amblaster",
									"pdburst",
									"lrpdlaser",
									"pdlaser",
									"pulselaser",
									"plasma",
									"hil",
									"autopulse",
									"guardian",
									"tachyonlance",
									"heavyburst",
									"heavyblaster",
									"gravitonbeam",
									"phasebeam",
									"irpulse",
									"ioncannon",
									"taclaser",
									"hurricane",
									"cyclone",
									"typhoon",
									"phasecl",
									"pilum",
									"sabot",
									"harpoon",
									"heatseeker",
									"annihilator",
									"swarmer",
									"atropos",
									"reaper",
									"longinus_small",
									};
									
}



