package data.scripts.everyframe;

import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
import com.fs.starfarer.api.combat.CombatEngineAPI;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.input.InputEventAPI;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SWP_ModuleFixer extends BaseEveryFrameCombatPlugin {

    private CombatEngineAPI engine;

    private static final Set<String> MODULES = new HashSet<>(11);

    static {
        MODULES.add("swp_cathedral_base");
        MODULES.add("swp_cathedral_rear");
        MODULES.add("swp_cathedral_gun_left");
        MODULES.add("swp_cathedral_gun_right");
        MODULES.add("swp_cathedral_fighter_left");
        MODULES.add("swp_cathedral_fighter_right");
        MODULES.add("swp_cathedral_front");
        MODULES.add("swp_wall_left");
        MODULES.add("swp_wall_right");
        MODULES.add("swp_boss_sporeship_bl");
        MODULES.add("swp_boss_sporeship_br");
        MODULES.add("swp_boss_sporeship_ll");
        MODULES.add("swp_boss_sporeship_lr");
        MODULES.add("swp_boss_sporeship_ml");
        MODULES.add("swp_boss_sporeship_mr");
        MODULES.add("swp_boss_sporeship_top");
        MODULES.add("swp_boss_sporeship_ul");
        MODULES.add("swp_boss_sporeship_ur");
    }

    @Override
    public void advance(float amount, List<InputEventAPI> events) {
        if (engine == null) {
            return;
        }

        if (engine.isPaused()) {
            return;
        }

        for (ShipAPI ship : engine.getShips()) {
            if (MODULES.contains(ship.getHullSpec().getBaseHullId())) {
                if (ship.getParentStation() == null) {
                    ship.setRetreating(true, true);
                } else if (!engine.isEntityInPlay(ship.getParentStation())) {
                    ship.setRetreating(true, true);
                }
            }
        }
    }

    @Override
    public void init(CombatEngineAPI engine) {
        this.engine = engine;
    }

}
