package net.cobem.grizzly.habbohotel.misc;

import gnu.trove.map.hash.THashMap;
import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.habbohotel.misc.commands.*;
import net.cobem.grizzly.habbohotel.roleplay.commands.Hit;
import net.cobem.grizzly.habbohotel.roleplay.commands.Taxi;
import net.cobem.grizzly.habbohotel.sessions.Session;

import java.util.Map;


public class ChatCommandParser {
    private THashMap<String, ChatCommand> ChatCommands;

    public ChatCommandParser() {
        ChatCommands = new THashMap<>();

        //Default in Grizzly
        add("help", new Help());
        add("commands", new CommandList());
        add("about", new Help());
        add("ban", new Ban());
        add("coords", new Coords());
        add("summon", new Summon());
        add("destructplugin", new DestructPlugin());
        add("destroyplugin", new DestructPlugin());
        add("deloadplugin", new DestructPlugin());
        add("bot", new CreateBot());
        add("destroybot", new DestroyBot());
        add("togglestatus", new ToggleStatus());
        add("credits", new Credits());
        add("officialstream", new OfficialStream());
        add("badge", new Badge());
        add("override", new Overrider());

        if (Grizzly.roleplayEnabled()) {
            add("taxi", new Taxi());
            add("hit", new Hit());
            add("punch", new Hit());
        }
    }

    public void add(String Name, ChatCommand Command) //2Lazy4Lyfe
    {
        ChatCommands.put(Name, Command);
    }

    public boolean parse(Session mSession, String Command) {
        String CommandName = splitCommand(Command, true)[0];

        if (!ChatCommands.containsKey(CommandName.toLowerCase())) {
            return false;
        }

        if (mSession.getHabbo().getRank() < ChatCommands.get(CommandName).rank()) {
            return false;
        }

        ChatCommands.get(CommandName).execute(mSession, splitCommand(Command, false));
        return true;
    }

    public Map<String, ChatCommand> getList() {
        return ChatCommands;
    }

    private String[] splitCommand(String Command, boolean Start) {
        String[] Split = Command.split(" ");

        if (Start) {
            return Split;
        } else {
            try {
                String Arguments = Command.replace(Split[0], "").substring(1);

                return Arguments.split(" ");
            } catch (Exception Ex) {
                return null;
            }
        }
    }
}
