package org.black_ixx.bossshop.command;

import java.util.ArrayList;

import org.bukkit.command.CommandSender;


public class CommandHelp extends ACommandBase{

    public CommandHelp(CommandExc exector){
        super(exector);
    }

    @Override
    public boolean execute(CommandSender sender,String[] args){
        if(!sender.hasPermission(this.mPluginName+".help")) return noPermit(sender);
        int page=1;
        if(args.length==1){
            try{
                page=Integer.parseInt(args[0]);
            }catch(NumberFormatException nfexp){
                page=1;
            }
        }
        ArrayList<String> tt=this.getHelp(sender,true);
        int page_numb=tt.size()%10==0?tt.size()/10:tt.size()/10+1;
        if(page<1||page>page_numb) page=1;
        send(sender,"================("+page+"/"+page_numb+")===============");
        for(int i=(page-1)*10;(i<(page*10))&&(i<tt.size());i++){
            send(sender,tt.get(i));
        }
        return true;
    }

    @Override
    public boolean help(CommandSender sender){
        return true;
    }

    @Override
    public ArrayList<String> getHelp(CommandSender sender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(sender.hasPermission(this.mPluginName+".help")){
            for(String label : this.mExector.keySet()){
                ACommandBase cmd=this.mExector.getCommand(label);
                if(cmd==this) continue;
                ArrayList<String> helplist=cmd.getHelp(sender,true);
                if(helplist==null) throw new IllegalArgumentException("命令模块的帮助结果为null,请联系作者完善代码");
                list.addAll(helplist);
            }
        }
        return list;
    }

}
