package me.km.snuviscript;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import me.hammerle.snuviscript.code.Script;
import me.km.permissions.Command;
import net.minecraft.command.ICommandSource;

public class CommandScript extends Command {
    private final Scripts scripts;

    public CommandScript(Scripts scripts) {
        this.scripts = scripts;
    }

    @Override
    public String getName() {
        return "script";
    }

    @Override
    public Iterable<String> getAliases() {
        ArrayList<String> list = new ArrayList<>();
        list.add("s");
        return list;
    }

    @Override
    public void execute(ICommandSource cs, String[] arg) {
        if(arg.length >= 1) {
            switch (arg[0].toLowerCase()) {
                case "s":
                case "start": {
                    if(arg.length >= 2) {
                        String[] pars = Arrays.copyOfRange(arg, 1, arg.length);
                        scripts.startScript(null, pars);
                        return;
                    }
                    break;
                }
                case "see": {
                    Collection<Script> scs = scripts.getScriptManager().getScripts();
                    if(scs.isEmpty()) {
                        sendMessage(cs, "No scripts are active.");
                        return;
                    }
                    sendMessage(cs, "Active scripts:");
                    scs.forEach(
                            sc -> sendListMessage(cs, String.valueOf(sc.getId()), sc.getName()));
                    return;
                }
                case "t":
                case "term": {
                    if(arg.length >= 2) {
                        try {
                            if(arg[1].equals("all")) {
                                scripts.getScriptManager().removeScripts();
                                sendMessage(cs, "All active scripts were terminated.");
                                return;
                            }
                            int id = Integer.parseInt(arg[1]);
                            Script sc = scripts.getScriptManager().getScript(id);
                            if(sc != null) {
                                scripts.getScriptManager().removeScript(sc);
                                sendMessage(cs,
                                        String.format("Script '%s' was terminated.", sc.getName()));
                            } else {
                                sendMessage(cs, String.format("Script id '%d' is not valid.", id));
                            }
                        } catch (NumberFormatException ex) {
                            sendMessage(cs, String.format("'%s' is not a valid id.", arg[1]));
                        } catch (Exception ex) {
                            sendMessage(cs, "An exception on script termination was thrown.");
                            ex.printStackTrace();
                        }
                        return;
                    }
                    break;
                }
            }
        }

        sendMessage(cs, "/script ...");
        sendListMessage(cs, "start <scripts...>", "starts a script");
        sendListMessage(cs, "see", "shows active scripts");
        sendListMessage(cs, "term <id/all>", "terminates a script");
    }
}
