package me.km.snuviscript;

import java.util.Arrays;
import java.util.HashSet;
import me.hammerle.snuviscript.code.ISnuviLogger;
import me.hammerle.snuviscript.code.ISnuviScheduler;
import me.hammerle.snuviscript.code.Script;
import me.hammerle.snuviscript.code.ScriptManager;
import me.km.events.CustomEventCaller;

public class Scripts {
    private final ScriptManager manager;
    private final ISnuviLogger logger;
    private CustomEventCaller cec = null;

    private final HashSet<String> scriptCommands = new HashSet<>();

    private final Limits entityLimits = new Limits();

    public Scripts(ISnuviLogger logger, ISnuviScheduler scheduler) {
        this.logger = logger;
        manager = new ScriptManager(logger, scheduler);
    }

    public void setCustomEventCaller(CustomEventCaller cec) {
        this.cec = cec;
    }

    public ScriptManager getScriptManager() {
        return manager;
    }

    public ISnuviLogger getLogger() {
        return logger;
    }

    public Limits getEntityLimits() {
        return entityLimits;
    }

    public boolean isRegisteredScriptCommand(String s) {
        return scriptCommands.contains(s);
    }

    public void registerScriptCommand(String s) {
        if(s.equals("script")) {
            return;
        }
        scriptCommands.add(s);
    }

    public void unregisterScriptCommand(String s) {
        scriptCommands.remove(s);
    }

    public void clearScriptCommands() {
        scriptCommands.clear();
    }

    public Script startScript(String name, String... names) {
        Arrays.setAll(names, i -> "scripts/" + names[i] + ".txt");
        return manager.startScript(sc -> cec.removeScriptData(sc), name == null ? names[0] : name, names);
    }
}
