package org.black_ixx.bossshop.command;

import java.util.ArrayList;

import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.BSShopManager;
import org.bukkit.command.CommandSender;

public class CommandReload extends ACommandBase{

    private final ArrayList<String> mSubCommand=new ArrayList<>();
    
    public CommandReload(CommandExc exector){
        super(exector);
        this.mSubCommand.add("all");
        this.mSubCommand.add("config");
        this.mSubCommand.add("help");
        this.mSubCommand.add("lang");
        this.mSubCommand.add("shop");
    }

    @Override
    public boolean execute(CommandSender pSender,String[] pArgs){
        String cmdLabel="all";
        if(pArgs.length>0) cmdLabel=pArgs[0];
        if(cmdLabel.equalsIgnoreCase("all")){
            if(!pSender.hasPermission(this.mPluginName+".reload.all"))
                return noPermit(pSender);
            this.mPlugin.reloadPlugin();
            return send(pSender,this.C("Cmd.PluginReloaded"));
        }else if(cmdLabel.equalsIgnoreCase("config")){
            if(!pSender.hasPermission(this.mPluginName+".reload.config"))
                return noPermit(pSender);
            this.mPlugin.reloadConfig();
            return send(pSender,this.C("Cmd.ConfigReloacded"));
        }else if(cmdLabel.equalsIgnoreCase("help")){
            if(!pSender.hasPermission(this.mPluginName+".reload.help"))
                return noPermit(pSender);
            return help(pSender);
        }else if(cmdLabel.equalsIgnoreCase("lang")){
            if(!pSender.hasPermission(this.mPluginName+".reload.lang"))
                return noPermit(pSender);
            this.mPlugin.getLangManager().reloadConfig();
            return send(pSender,this.C("Cmd.LangReloacded"));
        }if(cmdLabel.equalsIgnoreCase("shop")){
            if(!pSender.hasPermission(this.mPluginName+".reload.shop"))
                return noPermit(pSender);
            return this.reloadShop(pSender,pArgs);
        }else return unKnowChildCommand(pSender,cmdLabel);
    }

    @Override
    public ArrayList<String> getHelp(CommandSender sender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(pAll&&sender.hasPermission(this.mPluginName+".reload.help")){
            list.add(this.mMainCmdLabel+" reload help");
            list.add(this.C("Help.ReloadHelp"));
        }
        if(sender.hasPermission(this.mPluginName+".reload.all")){
            list.add(this.mMainCmdLabel+" reload [all]");
            list.add(this.C("Help.Reload"));
        }
        if(sender.hasPermission(this.mPluginName+".reload.config")){
            list.add(this.mMainCmdLabel+" reload config");
            list.add(this.C("Help.ReloadConfig"));
        }
        if(sender.hasPermission(this.mPluginName+".reload.lang")){
            list.add(this.mMainCmdLabel+" reload lang");
            list.add(this.C("Help.ReloadLang"));
        }
        if(sender.hasPermission(this.mPluginName+".reload.shop")){
            list.add(this.mMainCmdLabel+" reload shop ["+C("Word.ShopName")+"]");
            list.add(this.C("Help.ReloadShop"));
        }
        return list;
    }

    private boolean reloadShop(CommandSender pSender,String[] pArgs){
        BSShopManager shopMan=this.mPlugin.getManager(BSShopManager.class);
        if(pArgs.length==1){
            shopMan.reloadConfig();
            return send(pSender,C("Cmd.AlreadyReloadShop").replace("%shop%",C("Word.All")));
        }else if(pArgs.length==2){
            BSShop tShop=shopMan.getShop(pArgs[1]);
            if(tShop==null)
                return send(pSender,C("Cmd.NoShopFound")+"["+pArgs[1]+"]");
            if(!tShop.reloadShop())
                shopMan.removeShop(tShop);
            tShop.updateAllInventory();
            return send(pSender,C("Cmd.AlreadyReloadShop").replace("%shop%",tShop.getShopName()));
        }else return help(pSender);
    }
    
    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String[] pArgs){
        return this.mSubCommand;
    }

    @Override
    public int getMaxArgumentLength(){
        return 1;
    }
    
}
