package pw.yumc.Residence.commandmain;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.PluginHelper.commands.DefaultCommand;
import cn.citycraft.PluginHelper.commands.HandlerSubCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.commandsub.CommandArea;
import pw.yumc.Residence.commandsub.CommandBank;
import pw.yumc.Residence.commandsub.CommandCheck;
import pw.yumc.Residence.commandsub.CommandCheckSelf;
import pw.yumc.Residence.commandsub.CommandClearFlags;
import pw.yumc.Residence.commandsub.CommandCompass;
import pw.yumc.Residence.commandsub.CommandConfirm;
import pw.yumc.Residence.commandsub.CommandCreate;
import pw.yumc.Residence.commandsub.CommandCurrent;
import pw.yumc.Residence.commandsub.CommandDefault;
import pw.yumc.Residence.commandsub.CommandGive;
import pw.yumc.Residence.commandsub.CommandGset;
import pw.yumc.Residence.commandsub.CommandInfo;
import pw.yumc.Residence.commandsub.CommandKick;
import pw.yumc.Residence.commandsub.CommandLease;
import pw.yumc.Residence.commandsub.CommandLimits;
import pw.yumc.Residence.commandsub.CommandList;
import pw.yumc.Residence.commandsub.CommandListAll;
import pw.yumc.Residence.commandsub.CommandListAllHidden;
import pw.yumc.Residence.commandsub.CommandListHidden;
import pw.yumc.Residence.commandsub.CommandLists;
import pw.yumc.Residence.commandsub.CommandLset;
import pw.yumc.Residence.commandsub.CommandMarket;
import pw.yumc.Residence.commandsub.CommandMaterial;
import pw.yumc.Residence.commandsub.CommandMessage;
import pw.yumc.Residence.commandsub.CommandMirror;
import pw.yumc.Residence.commandsub.CommandPset;
import pw.yumc.Residence.commandsub.CommandRemove;
import pw.yumc.Residence.commandsub.CommandRemoveAll;
import pw.yumc.Residence.commandsub.CommandRename;
import pw.yumc.Residence.commandsub.CommandRenameArea;
import pw.yumc.Residence.commandsub.CommandSelect;
import pw.yumc.Residence.commandsub.CommandServer;
import pw.yumc.Residence.commandsub.CommandSet;
import pw.yumc.Residence.commandsub.CommandSetOwner;
import pw.yumc.Residence.commandsub.CommandSubList;
import pw.yumc.Residence.commandsub.CommandSubZone;
import pw.yumc.Residence.commandsub.CommandTool;
import pw.yumc.Residence.commandsub.CommandTp;
import pw.yumc.Residence.commandsub.CommandTpSet;
import pw.yumc.Residence.commandsub.CommandUnStuck;
import pw.yumc.Residence.commandsub.CommandVersion;

public class CommandRes extends BaseCommand implements DefaultCommand {
    HandlerSubCommand hdsubcmd;

    ResidenceMain plugin;

    public CommandRes(final ResidenceMain plugin) {
        super("res", "residence", "resadmin");
        this.plugin = plugin;
        hdsubcmd = new HandlerSubCommand(plugin);

        hdsubcmd.registerCommand(new CommandArea(plugin));
        hdsubcmd.registerCommand(new CommandBank(plugin));
        hdsubcmd.registerCommand(new CommandCheck(plugin));
        hdsubcmd.registerCommand(new CommandCheckSelf(plugin));
        hdsubcmd.registerCommand(new CommandClearFlags(plugin));
        hdsubcmd.registerCommand(new CommandCompass(plugin));
        hdsubcmd.registerCommand(new CommandConfirm(plugin));
        hdsubcmd.registerCommand(new CommandCreate(plugin));
        hdsubcmd.registerCommand(new CommandCurrent(plugin));
        hdsubcmd.registerCommand(new CommandDefault(plugin));
        hdsubcmd.registerCommand(new CommandGive(plugin));
        hdsubcmd.registerCommand(new CommandGset(plugin));
        hdsubcmd.registerCommand(new CommandInfo(plugin));
        hdsubcmd.registerCommand(new CommandKick(plugin));
        hdsubcmd.registerCommand(new CommandLease(plugin));
        hdsubcmd.registerCommand(new CommandLimits(plugin));
        hdsubcmd.registerCommand(new CommandList(plugin));
        hdsubcmd.registerCommand(new CommandListAll(plugin));
        hdsubcmd.registerCommand(new CommandListAllHidden(plugin));
        hdsubcmd.registerCommand(new CommandListHidden(plugin));
        hdsubcmd.registerCommand(new CommandLists(plugin));
        hdsubcmd.registerCommand(new CommandLset(plugin));
        hdsubcmd.registerCommand(new CommandMarket(plugin));
        hdsubcmd.registerCommand(new CommandMaterial(plugin));
        hdsubcmd.registerCommand(new CommandMessage(plugin));
        hdsubcmd.registerCommand(new CommandMirror(plugin));
        hdsubcmd.registerCommand(new CommandPset(plugin));
        hdsubcmd.registerCommand(new CommandRemove(plugin));
        hdsubcmd.registerCommand(new CommandRemoveAll(plugin));
        hdsubcmd.registerCommand(new CommandRename(plugin));
        hdsubcmd.registerCommand(new CommandRenameArea(plugin));
        hdsubcmd.registerCommand(new CommandSelect(plugin));
        hdsubcmd.registerCommand(new CommandServer(plugin));
        hdsubcmd.registerCommand(new CommandSet(plugin));
        hdsubcmd.registerCommand(new CommandSetOwner(plugin));
        hdsubcmd.registerCommand(new CommandSubList(plugin));
        hdsubcmd.registerCommand(new CommandSubZone(plugin));
        hdsubcmd.registerCommand(new CommandTool(plugin));
        hdsubcmd.registerCommand(new CommandTp(plugin));
        hdsubcmd.registerCommand(new CommandTpSet(plugin));
        hdsubcmd.registerCommand(new CommandUnStuck(plugin));
        hdsubcmd.registerCommand(new CommandVersion(plugin));

        hdsubcmd.setDefaultCommand(this);

        plugin.getCommand("residence").setTabCompleter(hdsubcmd);
    }

    @Override
    public void defaultExecute(final CommandSender sender, final Command command, final String label) throws CommandException {
        commandHelp(new String[] { "?" }, true, sender);
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        boolean resadmin = false;
        if (sender instanceof Player) {
            if (label.equalsIgnoreCase("resadmin")) {
                if (plugin.getPermissionManager().isResidenceAdmin((Player) sender)) {
                    resadmin = true;
                } else {
                    sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("NonAdmin"));
                }
            }
        } else {
            resadmin = true;
        }
        if (args.length > 0 && args[args.length - 1].equalsIgnoreCase("?") || args.length > 1 && args[args.length - 2].equals("?")) {
            commandHelp(args, resadmin, sender);
            return;
        }
        if (plugin.getConfigManager().allowAdminsOnly()) {
            if (!resadmin) {
                sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("AdminOnly"));
                return;
            }
        }
        hdsubcmd.onCommand(sender, resadmin ? command : null, label, args);
    }

    private void commandHelp(final String[] args, final boolean resadmin, final CommandSender sender) {
        if (plugin.getHelppages() != null) {
            String helppath = "res";
            for (final String arg : args) {
                if (arg.equalsIgnoreCase("?")) {
                    break;
                }
                helppath = helppath + "." + arg;
            }
            int page = 1;
            if (!args[args.length - 1].equalsIgnoreCase("?")) {
                try {
                    page = Integer.parseInt(args[args.length - 1]);
                } catch (final Exception ex) {
                    sender.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidHelp"));
                }
            }
            if (plugin.getHelppages().containesEntry(helppath)) {
                plugin.getHelppages().printHelp(sender, page, helppath);
            }
        }
    }
}
