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.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import cc.util.bossshop.FBukkit;


public class CommandOpen extends ACommandBase{
    
    public CommandOpen(CommandExc exector){
        super(exector);
    }

    @Override
    public boolean execute(CommandSender pSender,String[] args){
        BSShopManager shopMan=this.mPlugin.getManager(BSShopManager.class);
        if(args.length==1){
            if(args[0].equalsIgnoreCase("help")){
                if(!pSender.hasPermission(this.mPluginName+".open.help"))
                    return noPermit(pSender);
                return help(pSender);
            }
            if(!pSender.hasPermission(this.mPluginName+".open.command"))
                return this.noPermit(pSender);
            if(!(pSender instanceof Player))
                return this.consoleNotAllow(pSender);
            Player tPlayer=(Player)pSender;
            BSShop tshop=shopMan.getShop(args[0]);
            if(tshop==null) return this.send(pSender,this.C("Cmd.NoShopFound")+" ["+args[0]+"]");
            this.openShop(tPlayer,tshop);
        }else if(args.length==2){
            Player tPlayer=Bukkit.getPlayer(args[0]);
            if(tPlayer==null)
                return this.send(pSender,this.C("Cmd.NoPlayerFound")+" ["+args[0]+"]");
            boolean self=pSender.getName().equalsIgnoreCase(args[0]);
            if(!self&&!pSender.hasPermission("BossShop.open.other"))
                return this.noPermit(pSender);
            if(!pSender.hasPermission(this.mPluginName+".open.command"))
                return this.noPermit(pSender);
            BSShop tshop=shopMan.getShop(args[1]);
            if(tshop==null)
                return this.send(pSender,this.C("Cmd.NoShopFound")+" ["+args[1]+"]");
            this.openShop(tPlayer,tshop);
            if(!self) this.send(pSender,this.C("Cmd.OpenShopForPlayer").replace("%player%",tPlayer.getName().replace("%shop%",tshop.getShopName())));
        }
        else return unKnowChildCommand(pSender,null);
        return true;
    }

    @Override
    public ArrayList<String> getHelp(CommandSender sender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(pAll&&sender.hasPermission(this.mPluginName+".open.help")){
            list.add(this.mMainCmdLabel+" open [help]");
            list.add(this.C("Help.HelpOpen"));
        }
        if(sender.hasPermission(this.mPluginName+".open.command")){
            list.add(this.mMainCmdLabel+" [open] ["+C("Word.ShopName")+"]");
            list.add(this.C("Help.OpenShop"));
        }
        if(sender.hasPermission(this.mPluginName+".open.other")){
            list.add(this.mMainCmdLabel+" open <"+C("Word.PlayerName")+"> <"+C("Word.ShopName")+">");
            list.add(this.C("Help.OpenOtherShop"));
        }
        return list;
    }
    
    private void openShop(final Player pPlayer,final BSShop pShop){
        int tDelay=this.mPlugin.getConfigManager().getShopOpenDelay();
        final BSShopManager shopMan=this.mPlugin.getManager(BSShopManager.class);
        if(tDelay<=0){
            shopMan.openShop(pPlayer,pShop);
        }
        else Bukkit.getScheduler().scheduleSyncDelayedTask(this.mPlugin,new Runnable(){

            @Override
            public void run(){
                shopMan.openShop(pPlayer,pShop);
            }
        },tDelay);
        
    }
    
    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String[] pArgs){
        ArrayList<String> mayCmd=new ArrayList<>();
        switch(pArgs.length){
        case 1:
            mayCmd.addAll(this.mPlugin.getManager(BSShopManager.class).getShops().keySet());
            mayCmd.addAll(FBukkit.getOnlinePlayersName());
            return mayCmd;
        case 2:
            mayCmd.addAll(this.mPlugin.getManager(BSShopManager.class).getShops().keySet());
            return mayCmd;
        }
        return super.getSubCommand(pSender, pArgs);
    }

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

}
