package org.black_ixx.bossshop.command;

import java.util.ArrayList;

import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.mail.MailManager;
import org.black_ixx.bossshop.managers.RecordManager;
import org.black_ixx.bossshop.managers.WorthHandler;
import org.black_ixx.bossshop.sale.SaleManager;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class CommandUnsale extends ACommandBase{

    public CommandUnsale(CommandExc exector){
        super(exector);
    }
    
    @Override
    public boolean execute(CommandSender pSender,String[] pArgs){
        if(!pSender.hasPermission(this.mPluginName+".unsale.user"))
            return noPermit(pSender);
        if(pArgs.length!=1) return help(pSender);
        SaleManager tSaleM=this.mPlugin.getManager(SaleManager.class);
        BSBuy tBuy=tSaleM.getSaleItem(pArgs[0]);
        if(tBuy==null)
            return send(pSender,C("Main.NoSaleItemFound")+"["+pArgs[0]+"]");
        boolean op=pSender.hasPermission(this.mPluginName+".unsale.admin");
        if(pSender instanceof Player){
            Player tPlayer=(Player)pSender;
            if(!tBuy.getOwner().equals(tPlayer.getUniqueId())&&!op)
                return send(pSender,C("Main.YouCannotUnsaleOther"));
            if(!op){
                RecordManager recordMan=this.mPlugin.getManager(RecordManager.class);
                int tCount=recordMan.getUnsaleRecord(tPlayer.getUniqueId());
                int tMaxCount=tSaleM.getUnsaleCount();
                if(tCount>=tMaxCount)
                    return send(pSender,C("Main.YouTodayCannotUnsaleMore"));
                int tCost=tSaleM.getUnSaleCost();
                WorthHandler wHandler=this.mPlugin.getManager(WorthHandler.class);
                if(tCost>0){
                    if(!wHandler.hasMoney(tPlayer,tCost))
                        return send(pSender,C("Main.YouNeed%%MoneyToUnsale").replace("%numb%",tCost+""));
                    wHandler.takeMoney(tPlayer,tCost);
                }
                recordMan.addUnsaleRecord(tPlayer.getUniqueId());
            }
        }
        tBuy.getShop().removeItem(tBuy.getName(),true);
        this.mPlugin.getManager(MailManager.class).addMail(tBuy,C("Main.FromHandUnsale"));
        return send(pSender,C("Main.SuccessUnsaleItem")+"["+tBuy.getName()+"]");
    }

    @Override
    public ArrayList<String> getHelp(CommandSender pSender,boolean pAll){
        ArrayList<String> list=new ArrayList<>();
        if(pSender.hasPermission(this.mPluginName+".unsale.user")||pSender.hasPermission(this.mPluginName+".unsale.admin")){
            list.add(this.mMainCmdLabel+" unsale <"+C("Word.SaleID")+">");
            list.add(C("Help.Unsale"));
        }
        return list;
    }
    
    @Override
    public ArrayList<String> getSubCommand(CommandSender pSender,String[] pArgs){
        ArrayList<String> allSale=new ArrayList<>();
        if(pSender.hasPermission(this.mPluginName+".unsale.admin")){
            allSale.addAll(this.mPlugin.getManager(SaleManager.class).getSaleItemsName());
        }else if(pSender.hasPermission(this.mPluginName+".unsale.user")&&pSender instanceof Player){
            allSale.addAll(this.mPlugin.getManager(SaleManager.class).getSaleItemsName((Player)pSender));
        }
        return allSale;
    }

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

}
