"use strict";
// Standard attributes 
this.name = "clipper_tanks_script.js";
this.author = "Smivs";
this.copyright = "(C) Smivs";
this.licence = "Creative Commons Attribution - Non-Commercial - Share Alike 4.0";
this.version = "1.4.3";
this.description = "Script to control external fuel tanks for player 'Clipper' ships.";

//The first two functions are concerned with updating existing users - check for old version at start
this.startUp = function () {
    if (missionVariables.TL_FOR_EQ_LQG_TANK_EMPTY) {
        // set mission flag
        this.missionFlag = "NO";
        this.startUpComplete;
        delete this.startUp;
    }
}
this.missionScreenOpportunity = function () {
    if (missionVariables.TL_FOR_EQ_LQG_TANK_EMPTY) {
        // Do nothing now if tank is damaged
        if (player.ship.docked && player.ship.equipmentStatus("EQ_LQG_TANK_EMPTY") == "EQUIPMENT_DAMAGED") {
            return;
        }
        // otherwise clean up and replace old status variables from previous versions and run mission screen
        if (player.ship.docked && player.ship.equipmentStatus("EQ_LQG_TANK") == "EQUIPMENT_OK" && this.missionFlag && this.missionFlag === "NO") {
            delete missionVariables.TL_FOR_EQ_LQG_TANK_EMPTY;
            missionVariables.clipper_tank_status = "LQG_TANK_FULL";
            mission.runScreen({ title: "!!! Fuel Tank Safety Notice !!!", messageKey: "clipperTank_v1.6_Update" });
            this.missionFlag = "DONE";
        }
        if (player.ship.docked && player.ship.equipmentStatus("EQ_LQG_TANK_EMPTY") == "EQUIPMENT_OK" && this.missionFlag && this.missionFlag === "NO") {
            delete missionVariables.TL_FOR_EQ_LQG_TANK_EMPTY;
            missionVariables.clipper_tank_status = "LQG_TANK_EMPTY";
            mission.runScreen({ title: "!!! Fuel Tank Safety Notice !!!", messageKey: "clipperTank_v1.6_Update" });
            this.missionFlag = "DONE";
        }
    }
}
// handle purchase of new ship
this.playerBoughtNewShip = function () {
    missionVariables.clipper_tank_status = "LQG_TANK_FULL";
}

// handle purchase of refills
this.playerBoughtEquipment = function (equipment) {
    if (equipment == "EQ_LQG_CHARGE") {
        player.ship.removeEquipment("EQ_LQG_CHARGE");
        player.ship.removeEquipment("EQ_LQG_TANK_EMPTY");
        player.ship.awardEquipment("EQ_LQG_TANK");
        missionVariables.clipper_tank_status = "LQG_TANK_FULL";
    }
}

// if tank activated
this.activated = function () {
    if (player.ship.fuel === 0 && player.ship.equipmentStatus("EQ_LQG_TANK") == "EQUIPMENT_OK" && missionVariables.clipper_tank_status == "LQG_TANK_FULL") {
        this.ship.fuel += 3.0;
        player.ship.removeEquipment("EQ_LQG_TANK");
        player.ship.awardEquipment("EQ_LQG_TANK_EMPTY");
        missionVariables.clipper_tank_status = "LQG_TANK_EMPTY";
        player.consoleMessage("Fuel transferred to Main Tank");
    }
    // unless the main tank is not empty
    else if (player.ship.fuel > 0 && player.ship.equipmentStatus("EQ_LQG_TANK") == "EQUIPMENT_OK" && missionVariables.clipper_tank_status == "LQG_TANK_FULL") {
        player.consoleMessage("Fuel transfer cancelled - Main Tank must be empty");
        return;
    }
    // or the external tank is empty - this shouldn't be necessary as the empty tank cannot be primed
    else if (player.ship.fuel > 0 && player.ship.equipmentStatus("EQ_LQG_TANK_EMPTY") == "EQUIPMENT_OK" && missionVariables.clipper_tank_status == "LQG_TANK_EMPTY") {
        player.consoleMessage("Fuel transfer un-available - External Tanks are empty.");
        return;
    }
}

// condition scripting to control availability of refills
this.allowAwardEquipment = function (equipment, ship, context) {
    if (equipment == "EQ_LQG_CHARGE") {
        if (context == "purchase" && player.ship.equipmentStatus("EQ_LQG_TANK_EMPTY") == "EQUIPMENT_OK" && missionVariables.clipper_tank_status == "LQG_TANK_EMPTY") {
            return true;
        }
    }
}
