"use strict";
this.name = "GalCopBB_Economy";
this.author = "phkb";
this.copyright = "2026 phkb";
this.description = "Manages market price adjustments for Disease Outbreak/lockdown planets";
this.license = "CC BY-NC-SA 4.0";

this._priceBoost = 0.40; // 40% boost to prices
this._quantityDrop = 0.80; // 80% reduction in quantities
this._debug = true;

this.libSettings = {
	Name: this.name,
	Display: expandDescription("[gcm_economy_config_display]"),
	Alias: expandDescription("[gcm_economy_config_alias]"),
	Alive: "libSettings",
	SInt: {
        S0: { Name: "_priceBoost", Def: 0.4, Min: 0, Max: 10, Float: true, Desc: expandDescription("[gcm_economy_priceboost]")},
        S1: { Name: "_quantityDrop", Def: 0.8, Min: 0, Max: 10, Float: true, Desc: expandDescription("[gcm_economy_quantitydrop]")},
		Info: expandDescription("[gcm_economy_config_sint_info]")
	}
};

//-------------------------------------------------------------------------------------------------------------
this.startUp = function () {
    if (missionVariables.GalCopBBMissions_EconomyPriceBoost) this._priceBoost = parseFloat(missionVariables.GalCopBBMissions_EconomyPriceBoost);
    if (missionVariables.GalCopBBMissions_EconomyQuantityDrop) this._quantityDrop = parseFloat(missionVariables.GalCopBBMissions_EconomyQuantityDrop);

    var msi = worldScripts.MarketScriptInterface_Main;
	msi.$addMarketInterface("system_local", "$updateLocalCommodityDefinition", this.name, "lowest");
}

//-------------------------------------------------------------------------------------------------------------
this.startUpComplete = function() {
	if (worldScripts.Lib_Config) {
		worldScripts.Lib_Config._registerSet(this.libSettings);
    }
}

//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function() {
    missionVariables.GalCopBBMissions_EconomyPriceBoost = this._priceBoost;
    missionVariables.GalCopBBMissions_EconomyQuantityDrop = this._quantityDrop;
}

//-------------------------------------------------------------------------------------------------------------
this.$updateLocalCommodityDefinition = function (goodDefinition, station, systemID) {
    var ds = worldScripts.GalCopBB_DiseaseOutbreak;
    var ec = worldScripts.GalCopBB_Economy;
	if (ds && system.ID >= 0) {
        if (ds._diseaseEvents.indexOf(system.ID) > 0) {
            // apply price boost
            // calculate the new price, based on the price limits in 
            var newprice = Math.round(goodDefinition.price * (1 + ec._priceBoost), 2);
            var newqty = Math.round(goodDefinition.quantity * (1 - ec._quantityDrop), 2);
            if (ec._debug) {
                log(this.name, "changing price on " + goodDefinition.key + " from " + goodDefinition.price + " to " + newprice);
                log(this.name, "changing quantity on " + goodDefinition.key + " from " + goodDefinition.quantity + " to " + newqty);
            }
            // adjust the price property
            goodDefinition.price = newprice;
            goodDefinition.quantity = newqty;
        }
	}
	return goodDefinition;
}
