package data.scripts.world.corvus;

import com.fs.starfarer.api.campaign.CampaignFleetAPI;
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.campaign.CargoAPI;
import com.fs.starfarer.api.campaign.FactionAPI;
import com.fs.starfarer.api.campaign.StarSystemAPI;
import com.fs.starfarer.api.Global;

import data.scripts.world.BaseSpawnPoint;

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

	public GRDefenseSpawnPoint(SectorAPI sector, LocationAPI location, 
									float daysInterval, int maxFleets, SectorEntityToken anchor) {
		super(sector, location, daysInterval, maxFleets, anchor);
	}

	
	@Override
	public CampaignFleetAPI spawnFleet() {
	
    // Faction pardon check
    StarSystemAPI system = getSector().getStarSystem("Corvus");
    SectorEntityToken playerfleet = system.getEntityByName("Fleet");
    SectorEntityToken station = system.getEntityByName("Orbital Depot");
    CargoAPI cargo = station.getCargo();
    CargoAPI playercargo = playerfleet.getCargo();
		FactionAPI player = getSector().getFaction("player");
    float pvh = player.getRelationship("hegemony");
    float pvi = player.getRelationship("independent");
    float pvt = player.getRelationship("tritachyon");
    float pvg = player.getRelationship("gunrunners");
    float pvp = player.getRelationship("pirates");
	float pvn = player.getRelationship("nomads");
	float pvj = player.getRelationship("junk_pirates");
	float pvl = player.getRelationship("lotus_pirates");
	float pvs = player.getRelationship("scrappers");
	float pvtr = player.getRelationship("Trade");
    
    if (pvh < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "hpardon", 1);
      if (haspardon) {
      player.setRelationship("hegemony", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. The Hegemony has granted you a full pardon.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "hpardon", 1);
    }}
    if (pvi < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
      if (haspardon) {
      player.setRelationship("independent", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. The independents will see you as neutral now.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ipardon", 1);
    }}
    if (pvt < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
      if (haspardon) {
      player.setRelationship("tritachyon", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. You've been erased from the Tri-Tachyon kill list.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "tpardon", 1);
    }}
    if (pvg < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "gpardon", 1);
      if (haspardon) {
      player.setRelationship("gunrunners", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have officially forgiven your past transgressions.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "gpardon", 1);
    }}
    if (pvp < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "ppardon", 1);
      if (haspardon) {
      player.setRelationship("pirates", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. Pirates across the sector will no longer hunt you.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "ppardon", 1);
    }}
	 if (pvn < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "npardon", 1);
      if (haspardon) {
      player.setRelationship("nomads", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. Nomads across the sector will no longer have the urge to kill you on sight.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "npardon", 1);
    }}
	if (pvj < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "jpardon", 1);
      if (haspardon) {
      player.setRelationship("junk_pirates", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. Junk Pirates across the sector will no longer beat the crap out of you.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "npardon", 1);
    }}
		if (pvl < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "lpardon", 1);
      if (haspardon) {
      player.setRelationship("lotus_pirates", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. Junk Pirates across the sector will no longer beat the crap out of you.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "lpardon", 1);
    }}
	  if (pvl < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "spardon", 1);
      if (haspardon) {
      player.setRelationship("scrappers", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. The scrapper have for given your transgressions; don't ask why.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "spardon", 1);
    }}
	  if (pvtr < 0) {
      boolean haspardon = playercargo.removeItems(CargoAPI.CargoItemType.RESOURCES, "trpardon", 1);
      if (haspardon) {
      player.setRelationship("Trade", 0);
      Global.getSectorAPI().addMessage("The Gun Runners have worked their magic. The traders have for given your transgressions; don't ask why.");
      cargo.addItems(CargoAPI.CargoItemType.RESOURCES, "trpardon", 1);
    }}
		// Faction pardon check
		
		CampaignFleetAPI fleet = getSector().createFleet("gunrunners", "defenseForce");
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
		
		fleet.addAssignment(FleetAssignment.DEFEND_LOCATION, getAnchor(), 6);
		fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		
		return fleet;
	}
	
}






