package org.black_ixx.bossshop.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.BSShopHolder;
import org.black_ixx.bossshop.core.BSShopManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

public class BossShopAPI{

    private BossShop mPlugin;
    private List<BossShopAddon> enabled_addons;

    public BossShopAPI(BossShop plugin){
        this.mPlugin=plugin;
    }

    // For Single Shop
    public boolean isValidShop(Inventory i){
        return (i.getHolder() instanceof BSShopHolder&&this.mPlugin.getManager(BSShopManager.class).getShop(i.getName())!=null);
    }

    public BSShop getShop(String name){
        return this.mPlugin.getManager(BSShopManager.class).getShop(name.toLowerCase());
    }

    public void openShop(Player p,String name){
        BSShop shop=getShop(name);
        if(shop==null){
            this.mPlugin.warn("尝试打开不存在的商店["+name+"]");
            return;
        }
        openShop(p,shop);
    }

    public void openShop(Player p,BSShop shop){
        this.mPlugin.getManager(BSShopManager.class).openShop(p,shop);
    }

    // Get Managers
    public BSShopManager getShopHandler(){
        return this.mPlugin.getManager(BSShopManager.class);
    }

    // Modify Shop/Shops
    public void addItemToShop(ItemStack menu_item,BSBuy shop_item,BSShop shop){
        shop.addShopItem(shop_item,menu_item);
    }

    public void finishedAddingItemsToShop(BSShop shop){
        shop.finishedAddingItems();
    }

    // Get Shop Items
    public HashMap<BSShop,List<BSBuy>> getAllShopItems(){
        HashMap<BSShop,List<BSBuy>> all=new HashMap<BSShop,List<BSBuy>>();
        for(BSShop shop : this.mPlugin.getManager(BSShopManager.class).getShops().values()){
            List<BSBuy> items=new ArrayList<BSBuy>();
            for(Integer pos : shop.getItems().keySet()){
                BSBuy buy=shop.getItems().get(pos);
                if(pos==null||buy==null) continue;
                items.add(buy);
            }
            all.put(shop,items);
        }
        return all;
    }

    public HashMap<BSShop,List<BSBuy>> getAllShopItems(String config_option){
        HashMap<BSShop,List<BSBuy>> all=new HashMap<BSShop,List<BSBuy>>();
        for(BSShop shop : this.mPlugin.getManager(BSShopManager.class).getShops().values()){
            List<BSBuy> items=new ArrayList<BSBuy>();
            for(Integer pos : shop.getItems().keySet()){
                BSBuy buy=shop.getItems().get(pos);
                if(pos==null||buy==null) continue;
                if(buy.getConfigurationSection().getBoolean(config_option)==false&&buy.getConfigurationSection().getInt(config_option)==0){
                    continue;
                }
                items.add(buy);
            }
            all.put(shop,items);
        }
        return all;
    }

    // Addon API
    protected void addEnabledAddon(BossShopAddon addon){
        Plugin addonplugin=Bukkit.getPluginManager().getPlugin(addon.getAddonName());
        if(addonplugin==null){ return; }
        if(enabled_addons==null){
            enabled_addons=new ArrayList<BossShopAddon>();
        }
        if(enabled_addons.contains(addon)){ return; }
        enabled_addons.add(addon);
    }

    public List<BossShopAddon> getEnabledAddons(){
        return enabled_addons;
    }

}
