package org.black_ixx.bossshop.command;

import java.util.ArrayList;

import org.black_ixx.bossshop.nbt.NBT;
import org.black_ixx.bossshop.nbt.NBTEditManager;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class CommandNBT extends ACommandBase{

    private final ArrayList<String> mSubCommand=new ArrayList<>();
    
    public CommandNBT(CommandExc exector){
        super(exector);
        this.mSubCommand.add("add");
        this.mSubCommand.add("clear");
        this.mSubCommand.add("help");
    }

    @Override
    public boolean execute(CommandSender pSender,String[] pArgs){
        String label="help";
        if(pArgs.length>0) label=pArgs[0];
        if(label.equalsIgnoreCase("add")){
            if(!pSender.hasPermission(this.mPluginName+".nbt.add"))
                return noPermit(pSender);
            if(!(pSender instanceof Player))
                return this.consoleNotAllow(pSender);
            return this.add((Player)pSender,pArgs);
        }else if(label.equalsIgnoreCase("clear")){
            if(!pSender.hasPermission(this.mPluginName+".nbt.clear"))
                return noPermit(pSender);
            return this.clear(pSender,pArgs);
        }else if(label.equalsIgnoreCase("help")){
            if(!pSender.hasPermission(this.mPluginName+".nbt.help"))
                return noPermit(pSender);
            return help(pSender);
        }else return unKnowChildCommand(pSender,label);
    }

    @Override
    public ArrayList<String> getHelp(CommandSender sender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(pAll&&sender.hasPermission(this.mPluginName+".nbt.help")){
            list.add(this.mMainCmdLabel+" nbt [help]");
            list.add(this.C("Help.NBTHelp"));
        }
        if(sender.hasPermission(this.mPluginName+".nbt.add")){
            list.add(this.mMainCmdLabel+" nbt add");
            list.add(this.C("Help.NBTAdd"));
        }
        if(sender.hasPermission(this.mPluginName+".nbt.clear")){
            list.add(this.mMainCmdLabel+" nbt clear");
            list.add(this.C("Help.NBTClear"));
        }
        return list;
    }
    
    private boolean add(Player pPlayer,String args[]){
        if(args.length!=1) return this.help(pPlayer);
        ItemStack item=pPlayer.getItemInHand();
        if(item==null||item.getType()==Material.AIR){
            this.send(pPlayer,this.C("Cmd.YouShouldTakeItemInHand"));
            return true;
        }
        NBTEditManager NBTMan=this.mPlugin.getManager(NBTEditManager.class);
        NBT tNBT=NBTMan.getNBTByItem(item);
        if(tNBT!=null){
            this.send(pPlayer,this.C("Cmd.NBTHasExistInStockAndItsID")+tNBT.mLabel);
            return true;
        }
        String label=NBTMan.addItemNBTToStock(item,false);
        if(label==null) this.send(pPlayer,this.C("Cmd.ItemMayDonotHaveNBT"));
        else this.send(pPlayer,this.C("Cmd.NBTHaveAddAndItsID")+label);
        return true;
    }
    
    private boolean clear(CommandSender sender,String args[]){
        if(args.length!=1) return this.help(sender);
        int count=this.mPlugin.getManager(NBTEditManager.class).clearNBT();
        return this.send(sender,this.C("Cmd.ClearedNoUsefulNBT").replace("%numb%",count+""));
    }
    
    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String[] pArgs){
        return this.mSubCommand;
    }

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

}
