package data.scripts.hullmods;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fs.starfarer.api.campaign.CampaignFleetAPI;
import com.fs.starfarer.api.fleet.FleetMemberAPI;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.combat.BaseHullMod;
import com.fs.starfarer.api.util.Misc;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CHM_commissioned extends BaseHullMod {
    public static JSONArray spreadsheet;

    public CHM_commissioned() {
        try {
            this.spreadsheet = Global.getSettings().getMergedSpreadsheetDataForMod("faction_id", "data/config/CommissionBonus/TechpriestCommission.csv", "timid_commissioned_hull_mods");
        
        }
        catch (JSONException ex) {Logger.getLogger(CHM_commissioned.class.getName()).log(Level.SEVERE, null, ex);
}
        catch (IOException ex) {
            Logger.getLogger(CHM_commissioned.class.getName()).log(Level.SEVERE, null, ex);
        }
    };
    @Override
    public void applyEffectsAfterShipCreation(ShipAPI ship, String id) {
        //if (Factions.HEGEMONY.equals(Misc.getCommissionFactionId())) {
        for (int i = 0; i < spreadsheet.length(); i++) {
            try {
                JSONObject row = spreadsheet.getJSONObject(i);
                String BigFactionEnergy = row.getString("faction_id");
                String BigHullMod = row.getString("hullmod_id");
                if (BigFactionEnergy.equals(Misc.getCommissionFactionId())) {
                    ship.getVariant().addMod(BigHullMod);
                    CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
                    for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
			member.getVariant().addMod(BigHullMod);
                    }
                }
            } catch (JSONException ex) {
                Logger.getLogger(CHM_commissioned.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


