package org.black_ixx.bossshop.command;

import java.util.ArrayList;

import org.black_ixx.bossshop.mail.MailManager;
import org.black_ixx.bossshop.managers.WorthHandler;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;


public class CommandMail extends ACommandBase{

    private MailManager mMailMan;
    private final ArrayList<String> mSubCommand=new ArrayList<>();

    public CommandMail(CommandExc exector){
        super(exector);
        this.mSubCommand.add("check");
        this.mSubCommand.add("help");
        this.mSubCommand.add("recive");
        this.mSubCommand.add("send");
    }

    @Override
    public boolean execute(CommandSender pSender,String[] pArgs){
        if(!(pSender instanceof Player))
            return consoleNotAllow(pSender);
        Player tPlayer=(Player)pSender;
        String cmdLabel="help";
        if(pArgs.length>0) cmdLabel=pArgs[0];
        if(cmdLabel.equalsIgnoreCase("check")){
            if(!tPlayer.hasPermission(this.mPluginName+".mail.check"))
                return noPermit(pSender);
            return this.checkMail(tPlayer,pArgs);
        }else if(cmdLabel.equalsIgnoreCase("recive")){
            if(!tPlayer.hasPermission(this.mPluginName+".mail.recive"))
                return noPermit(pSender);
            return this.reciveMial(tPlayer,pArgs);
        }else if(cmdLabel.equalsIgnoreCase("send")){
            if(!tPlayer.hasPermission(this.mPluginName+".mail.send"))
                return noPermit(pSender);
            return this.sendMail(tPlayer,pArgs);
        }
//        else if(cmdLabel.equalsIgnoreCase("undo")){
//            if(!tPlayer.hasPermission("BossShop.mail.undo"))
//                return noPermit(pSender);
//            return this.undoMail(tPlayer,pArgs);
//        }
        else if(cmdLabel.equalsIgnoreCase("help")){
            if(!tPlayer.hasPermission("BossShop.mail.help"))
                return noPermit(pSender);
            return help(pSender);
        }else return unKnowChildCommand(pSender,cmdLabel);
    }
    
    private MailManager getMailManager(){
        if(this.mMailMan==null)
            this.mMailMan=this.mPlugin.getManager(MailManager.class);
        return this.mMailMan;
    }
    
    public boolean checkMail(Player tPlayer,String[] pArgs){
        ArrayList<String> list=this.getMailManager().checkMail(tPlayer.getUniqueId());
        for(String smsg : list) send(tPlayer,smsg);
        return true;
    }
    
    public boolean reciveMial(Player tPlayer,String[] pArgs){
        if(this.getMailManager().getMailCount(tPlayer.getUniqueId())<=0)
            return send(tPlayer,C("Mail.YouHaveNoMail"));
        this.getMailManager().reciveMail(tPlayer);
        return true;
    }
    
    public boolean undoMail(Player tPlayer,String[] pArgs){
        return false;
    }
    
    public boolean sendMail(Player tPlayer,String[] pArgs){
//        if(pArgs.length!=2) return help(tPlayer);
//        ArrayList<Player> tBeSernders=new ArrayList<>();
//        if(pArgs[1].equals("*")){
//            if(!tPlayer.hasPermission("BossShop.mail.send.all")) return noPermit(tPlayer);
//            for(Player sPlayer : FBukkit.getOnlinePlayers()){
//                
//            }
//        }
//        String[] tTargets=pArgs[1].split(",");
        Player tTarget=Bukkit.getPlayer(pArgs[1]);
        if(tTarget==null)
            return send(tPlayer,C("Main.NoPlayerFound")+"["+pArgs[1]+"]");
        if(pArgs[1].equalsIgnoreCase(tPlayer.getName())&&!tPlayer.isOp())
            return send(tPlayer,C("Mail.YouCannotSendMailToYouSelf"));
        MailManager tMailM=this.getMailManager();
        if(tMailM.isMailFull(tTarget.getUniqueId()))
            return send(tPlayer,C("Mail.PlayerMailIsFull").replace("%player%",tTarget.getName()));
        ItemStack tItem=tPlayer.getItemInHand();
        if(tItem==null||tItem.getType()==Material.AIR)
            return send(tPlayer,C("Cmd.YouShouldTakeItemInHand"));
        int tCost=tMailM.getMailSendCost();
        WorthHandler wHandler=this.mPlugin.getManager(WorthHandler.class);
        if(tCost>0){
            if(!wHandler.hasMoney(tPlayer,tCost))
                return send(tPlayer,C("YouNeed%%MoneyToSendMail").replace("%numb%",tCost+""));
            else wHandler.takeMoney(tPlayer,tCost);
        }
        tMailM.addMail(tPlayer,tTarget,tItem);
        tPlayer.setItemInHand(null);
        return send(tPlayer,C("Mail.MailHasSend"));
    }
    
    @Override
    public ArrayList<String> getHelp(CommandSender pSender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(pAll&&pSender.hasPermission("BossShop.mail.help")){
            list.add(this.mMainCmdLabel+" mail [help]");
            list.add(C("Help.MailHelp"));
        }
        if(pSender.hasPermission("BossShop.mail.check")){
            list.add(this.mMainCmdLabel+" mail check");
            list.add(C("Help.MailCheck"));
        }
        if(pSender.hasPermission("BossShop.mail.recive")){
            list.add(this.mMainCmdLabel+" mail recive");
            list.add(C("Help.MailRevice"));
        }
        if(pSender.hasPermission("BossShop.mail.send")){
            list.add(this.mMainCmdLabel+" mail send <"+C("Word.PlayerName")+">");
            list.add(C("Help.MailSend"));
        }
        return list;
    }
    
    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String[] pArgs){
        return this.mSubCommand;
    }

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

}
