package space.animefu.starfallhaven;

import java.util.ArrayList;

/** View-only colony management state. Simulation state remains owned by GameWorld. */
final class ColonyUiState {
    enum Panel { NONE, ORDERS, ARCHITECT, WORK }
    enum Order { INSPECT, GATHER, HAUL, MOVE, ATTACK }
    enum BuildCategory { STRUCTURE, FURNITURE, ZONES, PRODUCTION }
    enum DesignateMode { NONE, HARVEST, HAUL, BUILD }

    static final class Profile {
        final String callSign;
        final String role;
        final String[] traits;
        final String[] skillNames = {"Build", "Harvest", "Research", "Guard"};
        final int[] skills;
        final int[] priorities = {2, 2, 2, 2};
        boolean drafted;

        Profile(String callSign, String role, String[] traits, int[] skills) {
            this.callSign = callSign;
            this.role = role;
            this.traits = traits;
            this.skills = skills;
        }
    }

    static final class BuildItem {
        final String id, name, detail;
        final BuildCategory category;
        final int wood, stone, crystal;

        BuildItem(String id, String name, String detail, BuildCategory category,
                  int wood, int stone, int crystal) {
            this.id=id; this.name=name; this.detail=detail; this.category=category;
            this.wood=wood; this.stone=stone; this.crystal=crystal;
        }

        String cost() {
            StringBuilder out=new StringBuilder();
            if (wood>0) out.append(wood).append("w ");
            if (stone>0) out.append(stone).append("s ");
            if (crystal>0) out.append(crystal).append("c");
            return out.toString().trim();
        }
    }

    final Profile[] profiles = {
        new Profile("Mira", "Trail medic", new String[]{"Steady", "Kind"}, new int[]{2,3,1,2}),
        new Profile("Taro", "Habitat smith", new String[]{"Inventive", "Restless"}, new int[]{4,2,2,2}),
        new Profile("Sol", "Signal keeper", new String[]{"Watchful", "Bright"}, new int[]{2,2,4,3})
    };

    final BuildItem[] buildItems = {
        new BuildItem("barrier", "Glow barrier", "Drag a straight barrier run", BuildCategory.STRUCTURE, 4,2,0),
        new BuildItem("door", "Sealed door", "Drag a straight doorway run", BuildCategory.STRUCTURE, 5,1,0),
        new BuildItem("floor", "Haven floor", "Drag to mark a floor area", BuildCategory.STRUCTURE, 2,0,0),
        new BuildItem("bed", "Rest cradle", "Restores a settler", BuildCategory.FURNITURE, 8,0,0),
        new BuildItem("storage", "Supply field", "Designate a storage zone", BuildCategory.ZONES, 0,0,0),
        new BuildItem("beacon", "Star beacon", "Long-range rescue signal", BuildCategory.PRODUCTION, 30,20,12),
        new BuildItem("workbench", "Field bench", "Craft colony equipment", BuildCategory.PRODUCTION, 12,6,2)
    };

    Panel panel=Panel.NONE;
    Order order=Order.INSPECT;
    DesignateMode designateMode=DesignateMode.NONE;
    BuildCategory category=BuildCategory.STRUCTURE;
    int selectedColonist=0;
    BuildItem selectedBuild;
    String toast="";
    float toastSeconds;

    Profile profile(int index) { return profiles[Math.max(0, Math.min(index, profiles.length-1))]; }
    void announce(String text) { toast=text; toastSeconds=3.2f; }
    void update(float dt) { toastSeconds=Math.max(0,toastSeconds-dt); }
    void cyclePriority(int colonist, int work) {
        Profile p=profile(colonist);
        p.priorities[work]=p.priorities[work]>=4?0:p.priorities[work]+1;
    }
    BuildItem[] items(BuildCategory requested) {
        ArrayList<BuildItem> matches=new ArrayList<>();
        for(BuildItem item:buildItems) if(item.category==requested) matches.add(item);
        return matches.toArray(new BuildItem[matches.size()]);
    }
}
