package space.animefu.starfallhaven;

import android.content.Context;
import android.content.SharedPreferences;

/** Persistent first-run field guide advanced only by real colony-console actions. */
public final class TutorialController {
    public enum Step { SELECT, ORDERS, DRAFT, ARCHITECT, PLACE, WORK, PAN_ZOOM, DONE }
    private static final String PREFS="starfall_settings",KEY="tutorial_v2_step";
    private final SharedPreferences prefs;
    private Step step;

    public TutorialController(Context context){
        prefs=context.getSharedPreferences(PREFS,Context.MODE_PRIVATE);
        int i=prefs.getInt(KEY,0);
        step=Step.values()[Math.max(0,Math.min(i,Step.DONE.ordinal()))];
    }
    public Step step(){return step;}
    public boolean visible(){return step!=Step.DONE;}
    public String title(){return "HAVEN CONSOLE  "+(step.ordinal()+1)+" / 7";}
    public String instruction(){switch(step){
        case SELECT:return "Tap a settler card. Inspect health, focus, needs, traits and skills.";
        case ORDERS:return "Open ORDERS. Choose MOVE or ATTACK for direct commands.";
        case DRAFT:return "Tap DRAFT to place this settler under direct control.";
        case ARCHITECT:return "Open ARCHITECT. Browse structures, furniture, zones and production.";
        case PLACE:return "Choose a blueprint, then tap clear meadow to designate it.";
        case WORK:return "Open WORK. Tap a priority cell to cycle 0–4.";
        case PAN_ZOOM:return "Drag to pan; pinch to zoom. Pause or set ×2/×4 speed above.";
        default:return "";
    }}
    public void onColonistSelected(){advanceIf(Step.SELECT);}
    public void onOrder(){if(step==Step.ORDERS)advance();}
    public void onDraft(){if(step==Step.ORDERS)set(Step.DRAFT);advanceIf(Step.DRAFT);}
    public void onArchitectOpened(){if(step==Step.ORDERS)set(Step.ARCHITECT);advanceIf(Step.ARCHITECT);}
    public void onBuildPlaced(){if(step==Step.PLACE)advance();}
    public void onWorkOpened(){if(step==Step.PLACE)set(Step.WORK);}
    public void onWorkPriority(){advanceIf(Step.WORK);}
    public void onPan(){if(step==Step.PAN_ZOOM)advance();}
    public void onZoom(){if(step==Step.PAN_ZOOM)advance();}
    public void onGather(){}
    public void skip(){set(Step.DONE);}
    private void advanceIf(Step expected){if(step==expected)advance();}
    private void advance(){set(Step.values()[step.ordinal()+1]);}
    private void set(Step value){step=value;prefs.edit().putInt(KEY,value.ordinal()).apply();}
}
