package data.scripts.world.systems;

import java.awt.Color;

import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.campaign.CargoAPI;
import com.fs.starfarer.api.campaign.JumpPointAPI;
import com.fs.starfarer.api.campaign.LocationAPI;
import com.fs.starfarer.api.campaign.OrbitAPI;
import com.fs.starfarer.api.campaign.PlanetAPI;
import com.fs.starfarer.api.campaign.SectorAPI;
import com.fs.starfarer.api.campaign.SectorEntityToken;
import com.fs.starfarer.api.campaign.StarSystemAPI;
import com.fs.starfarer.api.campaign.econ.MarketAPI;
import com.fs.starfarer.api.campaign.econ.MarketAPI.SurveyLevel;
import com.fs.starfarer.api.impl.campaign.JumpPointInteractionDialogPluginImpl;
import com.fs.starfarer.api.impl.campaign.DerelictShipEntityPlugin.DerelictShipData;
import com.fs.starfarer.api.impl.campaign.ids.Commodities;
import com.fs.starfarer.api.impl.campaign.ids.Conditions;
import com.fs.starfarer.api.impl.campaign.ids.Entities;
import com.fs.starfarer.api.impl.campaign.ids.Factions;
import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
import com.fs.starfarer.api.impl.campaign.ids.Submarkets;
import com.fs.starfarer.api.impl.campaign.ids.Terrain;
import com.fs.starfarer.api.impl.campaign.procgen.DefenderDataOverride;
import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator;
import com.fs.starfarer.api.impl.campaign.procgen.themes.BaseThemeGenerator;
import com.fs.starfarer.api.impl.campaign.procgen.themes.DerelictThemeGenerator;
import com.fs.starfarer.api.impl.campaign.procgen.themes.SalvageSpecialAssigner.ShipRecoverySpecialCreator;
import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special.BaseSalvageSpecial;
import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special.ShipRecoverySpecial.PerShipData;
import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special.ShipRecoverySpecial.ShipCondition;
import com.fs.starfarer.api.impl.campaign.terrain.BaseTiledTerrain;
import com.fs.starfarer.api.impl.campaign.terrain.DebrisFieldTerrainPlugin.DebrisFieldParams;
import com.fs.starfarer.api.impl.campaign.terrain.DebrisFieldTerrainPlugin.DebrisFieldSource;
import com.fs.starfarer.api.impl.campaign.tutorial.GalatiaMarketScript;
import com.fs.starfarer.api.util.Misc;

/**
 * Starting/tutorial system.
 * 
 * @author Alex Mosolov
 *
 * Copyright 2017 Fractal Softworks, LLC
 */
// in Nexerelin the GalatiaMarketScripts are commented out 
// (they add commodities to simulate normal economic behaviour during the tutorial)
// Mission boards too
public class Galatia {

	public void generate(SectorAPI sector) {
		
		StarSystemAPI system = sector.createStarSystem("Galatia");
		LocationAPI hyper = Global.getSector().getHyperspace();
		
		system.setBackgroundTextureFilename("graphics/backgrounds/background5.jpg");
		
		PlanetAPI star = system.initStar("galatia", // unique id for this star 
									    "star_yellow",  // id in planets.json
									    600f, 		  // radius (in pixels at default zoom)
									    700, 		  // corona radius, from star edge
									    10, 0.5f, 3f);// solar wind/flares/CR loss
		
		system.setLightColor(new Color(255, 220, 190)); // light color in entire system, affects all entities
		
		
		PlanetAPI ancyra = system.addPlanet("ancyra", star, "Ancyra", "arid", 55, 150, 2800, 170);
		ancyra.setFaction(Factions.HEGEMONY);
		ancyra.getSpec().setGlowTexture(Global.getSettings().getSpriteName("hab_glows", "asharu"));
		ancyra.getSpec().setGlowColor(new Color(255,255,255,255));
		ancyra.getSpec().setUseReverseLightForGlow(true);
		ancyra.applySpecChanges();
		ancyra.setCustomDescriptionId("planet_ancyra");

		SectorEntityToken ancyraStation = system.addCustomEntity("ancyra_station", "Ancyra Research Facility", "station_side00", Factions.HEGEMONY);
		ancyraStation.setCircularOrbitPointingDown(ancyra, 300, 350, 30);
		ancyraStation.setInteractionImage("illustrations", "orbital");
		ancyraStation.setCustomDescriptionId("station_ancyra");
		
		// create a market for galatia1 - not connected to the rest of the economy to start with
		MarketAPI market = Global.getFactory().createMarket("ancyra_market", ancyra.getName(), 0);
		market.setSize(5);
		market.setFactionId(Factions.HEGEMONY);
		
		market.setSurveyLevel(SurveyLevel.FULL);
		market.setPrimaryEntity(ancyra);
		market.getConnectedEntities().add(ancyraStation);
		
		market.setFactionId(ancyraStation.getFaction().getId());
		market.addCondition(Conditions.ARID);
		market.addCondition(Conditions.HOT);
		market.addCondition(Conditions.HABITABLE);
		market.addCondition(Conditions.POPULATION_5);
		market.addCondition(Conditions.ORBITAL_STATION);
		market.addCondition(Conditions.LIGHT_INDUSTRIAL_COMPLEX);
		
		market.addSubmarket(Submarkets.SUBMARKET_OPEN);
		market.addSubmarket(Submarkets.SUBMARKET_BLACK);
		market.addSubmarket(Submarkets.SUBMARKET_STORAGE);
		
		ancyraStation.setMarket(market);
		ancyra.setMarket(market);
		
		//market.getCommDirectory().addMissionBoard();
		
		//ancyra.addScript(new GalatiaMarketScript(market));
		
		
		// Ancyra Relay - L4 (ahead)
		SectorEntityToken relay = system.addCustomEntity("ancyra_relay", // unique id
				 "Ancyra Relay", // name - if null, defaultName from custom_entities.json will be used
				 Entities.COMM_RELAY, // type of object, defined in custom_entities.json
				 Factions.HEGEMONY); // faction
		relay.setCircularOrbitPointingDown(star, 
										   ancyra.getCircularOrbitAngle() - 60f, 
										   ancyra.getCircularOrbitRadius(),
										   ancyra.getCircularOrbitPeriod());
		relay.getMemoryWithoutUpdate().set(MemFlags.COMM_RELAY_NON_FUNCTIONAL, true);
		
		
		SectorEntityToken pontus = system.addPlanet("pontus", star, "Pontus", "gas_giant", 200, 300, 6200, 400);
		SectorEntityToken probe = DerelictThemeGenerator.addSalvageEntity(system, Entities.DERELICT_SURVEY_PROBE, Factions.DERELICT);
		probe.setId("galatia_probe");
		probe.setCircularOrbit(star, 200, 10500, 400f);
		Misc.setDefenderOverride(probe, new DefenderDataOverride(Factions.DERELICT, 1f, 2, 2));
		CargoAPI extraProbeSalvage = Global.getFactory().createCargo(true);
		extraProbeSalvage.addCommodity(Commodities.GAMMA_CORE, 1);
		BaseSalvageSpecial.setExtraSalvage(extraProbeSalvage, probe.getMemoryWithoutUpdate(), -1);
		
		
		system.addRingBand(pontus, "misc", "rings_ice0", 256f, 2, Color.white, 256f, 650, 45, Terrain.RING, null);
		system.addRingBand(pontus, "misc", "rings_ice0", 256f, 3, Color.white, 256f, 800, 50, Terrain.RING, null);
		
		float beltOrbitRadius = 8000;
		system.addAsteroidBelt(star, 150, beltOrbitRadius, 500, 150, 300, Terrain.ASTEROID_BELT, null);
		system.addRingBand(star, "misc", "rings_dust0", 256f, 3, Color.white, 256f, beltOrbitRadius - 50, 305f, null, null);
		system.addRingBand(star, "misc", "rings_asteroids0", 256f, 3, Color.white, 256f, beltOrbitRadius + 50, 295f, null, null);
		
		
		SectorEntityToken tetra = system.addPlanet("tetra", star, "Tetra", "barren", 135f + 180f, 70, beltOrbitRadius + 1000f, 310);
		addDerelict(system, tetra, "wolf_Assault", ShipCondition.BATTERED, 200f, true);
		addDerelict(system, tetra, "lasher_CS", ShipCondition.BATTERED, 270f, false);
		addDerelict(system, tetra, "kite_Standard", ShipCondition.AVERAGE, 300f, true);
		addDerelict(system, tetra, "kite_Standard", ShipCondition.BATTERED, 350f, false);
		addDerelict(system, tetra, "tarsus_d_Standard", ShipCondition.BATTERED, 375f, false);
		addDerelict(system, tetra, "buffalo2_FS", ShipCondition.BATTERED, 400f, false);
		addDerelict(system, tetra, "hammerhead_Balanced", ShipCondition.AVERAGE, 450f, true);
		addDerelict(system, tetra, "condor_Support", ShipCondition.BATTERED, 500f, true);
			
		
		SectorEntityToken derinkuyuStation = system.addCustomEntity("derinkuyu_station", "Derinkuyu Mining Station", "station_mining00", Factions.PIRATES);
		derinkuyuStation.setCircularOrbitWithSpin(star, 135, beltOrbitRadius - 100f, 300f, 3f, 5f);
		//derinkuyuStation.setCircularOrbitPointingDown(galatia1, 300, 350, 30);
		derinkuyuStation.setInteractionImage("illustrations", "orbital");
		
		// create a market for derinkuyuStation - not connected to the rest of the economy to start with
		market = Global.getFactory().createMarket("derinkuyu_market", derinkuyuStation.getName(), 0);
		market.setSize(4);
		market.setFactionId(Factions.PIRATES);
		
		market.setSurveyLevel(SurveyLevel.FULL);
		market.setPrimaryEntity(derinkuyuStation);
		
		market.setFactionId(derinkuyuStation.getFaction().getId());
		market.addCondition(Conditions.POPULATION_4);
		market.addCondition(Conditions.ORBITAL_STATION);
		market.addCondition(Conditions.ORE_COMPLEX);
		market.addCondition(Conditions.ORE_COMPLEX);
		market.addCondition(Conditions.FREE_PORT);
		
		market.addSubmarket(Submarkets.SUBMARKET_OPEN);
		market.addSubmarket(Submarkets.SUBMARKET_BLACK);
		market.addSubmarket(Submarkets.SUBMARKET_STORAGE);
		
		derinkuyuStation.setMarket(market);
		//market.getCommDirectory().addMissionBoard();
		//derinkuyuStation.addScript(new GalatiaMarketScript(market));
		
		// Galatia Gate
		SectorEntityToken gate = system.addCustomEntity("galatia_gate", // unique id
				 "Galatia Gate", // name - if null, defaultName from custom_entities.json will be used
				 Entities.INACTIVE_GATE, // type of object, defined in custom_entities.json
				 null); // faction
		gate.setCircularOrbit(star, 120, 4200, 400);
		
		
		DebrisFieldParams params = new DebrisFieldParams(
				500f, // field radius - should not go above 1000 for performance reasons
				1f, // density, visual - affects number of debris pieces
				10000000f, // duration in days 
				0f); // days the field will keep generating glowing pieces
		params.source = DebrisFieldSource.MIXED;
		params.baseSalvageXP = 250; // base XP for scavenging in field
		SectorEntityToken debrisNextToBelt = Misc.addDebrisField(system, params, StarSystemGenerator.random);
		debrisNextToBelt.setSensorProfile(null);
		debrisNextToBelt.setDiscoverable(null);
		debrisNextToBelt.setCircularOrbit(star, 210f, beltOrbitRadius - 500f, 320f);
		debrisNextToBelt.setId("galatia_debrisNextToBelt");
		
		params = new DebrisFieldParams(
				500f, // field radius - should not go above 1000 for performance reasons
				1f, // density, visual - affects number of debris pieces
				10000000f, // duration in days 
				0f); // days the field will keep generating glowing pieces
		params.source = DebrisFieldSource.MIXED;
		params.baseSalvageXP = 250; // base XP for scavenging in field
		SectorEntityToken debrisNextToGate = Misc.addDebrisField(system, params, StarSystemGenerator.random);
		debrisNextToGate.setSensorProfile(null);
		debrisNextToGate.setDiscoverable(null);
		debrisNextToGate.setCircularOrbit(gate, 0f, 0f, 100f);
		debrisNextToGate.setId("galatia_debrisNextToGate");
		
		
		JumpPointAPI inner = Global.getFactory().createJumpPoint("galatia_jump_point_alpha", "Inner System Jump-point");
		//OrbitAPI orbit = Global.getFactory().createCircularOrbit(star, 0, 5200, 400);
		OrbitAPI orbit = Global.getFactory().createCircularOrbit(star, 
										pontus.getCircularOrbitAngle() + 180f,
										pontus.getCircularOrbitRadius() + 1000f,
										pontus.getCircularOrbitPeriod());
		inner.setOrbit(orbit);
		//jumpPoint_inner.setRelatedPlanet(galatia1);
		inner.setStandardWormholeToHyperspaceVisual();
		system.addEntity(inner);
		
		JumpPointAPI fringe = Global.getFactory().createJumpPoint("galatia_jump_point_fringe", "Fringe Jump-point");
		orbit = Global.getFactory().createCircularOrbit(star, 160, 11200, 600);
		fringe.setOrbit(orbit);
		fringe.setStandardWormholeToHyperspaceVisual();
		system.addEntity(fringe);
		
		inner.getMemoryWithoutUpdate().set(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY, true);
		fringe.getMemoryWithoutUpdate().set(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY, true);
			
		// L4 & L5 mini-nebulas
		SectorEntityToken pontus_L4_nebula = system.addTerrain(Terrain.NEBULA, new BaseTiledTerrain.TileParams(
				"  x   " +
				"  xx x" +
				"xxxxx " +
				" xxx  " +
				" x  x " +
				"   x  ",
				6, 6, // size of the nebula grid, should match above string
				"terrain", "nebula_blue", 4, 4, null));
		pontus_L4_nebula.setId("pontus_L4");
		SectorEntityToken pontus_L5_nebula = system.addTerrain(Terrain.NEBULA, new BaseTiledTerrain.TileParams(
				"  x   " +
				" xx xx" +
				"x  xx " +
				" xxxx " +
				" x x x" +
				"  x   ",
				6, 6, // size of the nebula grid, should match above string
				"terrain", "nebula_blue", 4, 4, null));
		pontus_L5_nebula.setId("pontus_L5");
		pontus_L4_nebula.setCircularOrbit(star, 200 + 60, 7800, 500);
		pontus_L5_nebula.setCircularOrbit(star, 200 - 60, 7800, 500);
		
		
		
		params = new DebrisFieldParams(
						350f, // field radius - should not go above 1000 for performance reasons
						1f, // density, visual - affects number of debris pieces
						10000000f, // duration in days 
						0f); // days the field will keep generating glowing pieces
		params.source = DebrisFieldSource.MIXED;
		params.baseSalvageXP = 250; // base XP for scavenging in field
		SectorEntityToken debris = Misc.addDebrisField(system, params, StarSystemGenerator.random);
		debris.setId("debris_tutorial");
		CargoAPI extraSalvage = Global.getFactory().createCargo(true);
		
		extraSalvage.addSupplies(20);
		extraSalvage.addCommodity(Commodities.HEAVY_MACHINERY, 10);
		//extraSalvage.addCommodity(Commodities.RARE_METALS, 10);
		
		BaseSalvageSpecial.setExtraSalvage(extraSalvage, debris.getMemoryWithoutUpdate(), -1);
		
		// makes the debris field always visible on map/sensors and not give any xp or notification on being discovered
		debris.setSensorProfile(null);
		debris.setDiscoverable(null);

		//debris.setCircularOrbit(mayasura_star, 45 + 10, 4500, 250);
		debris.getLocation().set(1000, -12000);
		
		
		
		// generates hyperspace destinations for in-system jump points
		system.autogenerateHyperspaceJumpPoints(true, false);
		
		// done automatically for systems with markets in them,
		// but since the markets in Galatia aren't part of the economy to start,
		// need to do manually here.
		system.setEnteredByPlayer(true);
		//Misc.setAllPlanetsKnown(system);
		Misc.setAllPlanetsSurveyed(system);
		
		
		
		// added in NGCAddStandardStartingScript
		//system.addScript(new CampaignTutorialScript(system));
	}
	
	protected void addDerelict(StarSystemAPI system, SectorEntityToken focus, String variantId, 
								ShipCondition condition, float orbitRadius, boolean recoverable) {
		DerelictShipData params = new DerelictShipData(new PerShipData(variantId, condition), false);
		SectorEntityToken ship = BaseThemeGenerator.addSalvageEntity(system, Entities.WRECK, Factions.NEUTRAL, params);
		ship.setDiscoverable(true);
		
		float orbitDays = orbitRadius / (10f + (float) Math.random() * 5f);
		ship.setCircularOrbit(focus, (float) Math.random() * 360f, orbitRadius, orbitDays);
		
		if (recoverable) {
			ShipRecoverySpecialCreator creator = new ShipRecoverySpecialCreator(null, 0, 0, false, null, null);
			Misc.setSalvageSpecial(ship, creator.createSpecial(ship, null));
		}
		
	}
}









