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.StarSystemAPI;

import data.scripts.world.BaseSpawnPoint;

public class TTSpawnPoint extends BaseSpawnPoint {

	private final SectorEntityToken station;

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

	@Override
	protected CampaignFleetAPI spawnFleet() {
		//if ((float) Math.random() < 0.5f) return null;
		
		String type = null;
		float r = (float) Math.random();
    if (r > 0.6f) {
			type = "raiders";
		} else if (r > 0.3f) {
			type = "attackFleet";
		} else if (r > 0.1f) {
			type = "securityDetachment";
		} else if (r > 0.1f) {
			type = "systemdefence";
		} else  if (r > 0.1f) {
			type = "capitalfleet";
		}
		
		CampaignFleetAPI fleet = getSector().createFleet("tritachyon", type);
		getLocation().spawnFleet(getAnchor(), 0, 0, fleet);
    
    StarSystemAPI system = getSector().getStarSystem("Corvus");
    SectorEntityToken target = system.getEntityByName("Orbital Depot");
    if (target == null) target = station;
    
		if (type.equals("raiders")) {
			fleet.addAssignment(FleetAssignment.RAID_SYSTEM, null, 30);
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		} else if (type.equals("attackFleet")) {
      if ((float) Math.random() > 0.5f) {
        fleet.addAssignment(FleetAssignment.ATTACK_LOCATION, station, 50);
			} else {
        fleet.addAssignment(FleetAssignment.ATTACK_LOCATION, target, 50);
			}
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		} else {
			fleet.addAssignment(FleetAssignment.DEFEND_LOCATION, getAnchor(), 30);
			fleet.addAssignment(FleetAssignment.GO_TO_LOCATION_AND_DESPAWN, getAnchor(), 1000);
		}
		return fleet;
	}

}
