package pw.yumc.Residence.gui;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;

import cn.citycraft.PluginHelper.config.FileConfig;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.listeners.ResidenceGUIListener;

public class FlagUtil {
    public static FileConfig conf;
    final static LinkedHashMap<String, List<String>> description = new LinkedHashMap<>();
    private final FlagData flagData = new FlagData();
    private final ResidenceMain plugin;
    protected ItemStack guitrue, guifalse, guiremove;

    public static FileConfig getConfig() {
        return conf;
    }

    public static void open(final ClaimedResidence area, final Player player, final String target, final boolean resadmin) {
        final SetFlag flag = new SetFlag(area, player, resadmin);
        if (StringUtils.isEmpty(target)) {
            flag.recalculateResidence(area);
        } else {
            flag.setTargePlayer(target);
            flag.recalculatePlayer(area);
        }
        ResidenceGUIListener.GUI.put(player.getName(), flag);
        player.openInventory(flag.getInventory());
        player.sendMessage("§a已打开领地 " + area.getName() + " 的管理界面!");
    }

    public FlagUtil(final ResidenceMain plugin) {
        this.plugin = plugin;
        conf = new FileConfig(plugin, "gui.yml");
        guitrue = new ItemStack(conf.getInt("GUI.setFtue.id", 1), 1, (short) conf.getInt("GUI.setFtue.data", 0));
        guifalse = new ItemStack(conf.getInt("GUI.setFalse.id", 1), 1, (short) conf.getInt("GUI.setFalse.data", 0));
        guiremove = new ItemStack(conf.getInt("GUI.setRemove.id", 1), 1, (short) conf.getInt("GUI.setRemove.data", 0));
    }

    public FlagData getFlagData() {
        return this.flagData;
    }

    public ItemStack getGuiFalse() {
        return guifalse;
    }

    public ItemStack getGuiRemove() {
        return guiremove;
    }

    public ItemStack getGuiTrue() {
        return guitrue;
    }

    public void load() {
        final Set<String> allFlags = plugin.getConfig().getConfigurationSection("Global.FlagPermission").getKeys(false);
        for (final String oneFlag : allFlags) {
            if (conf.contains("FlagGUI." + oneFlag)) {
                if (conf.contains("FlagGUI." + oneFlag + ".id")) {
                    if (conf.contains("FlagGUI." + oneFlag + ".data")) {
                        final int id = conf.getInt("FlagGUI." + oneFlag + ".id");
                        final int data = conf.getInt("FlagGUI." + oneFlag + ".data");
                        Material Mat = Material.getMaterial(id);
                        if (Mat == null) {
                            Mat = Material.STONE;
                        }
                        final ItemStack item = new ItemStack(Mat, 1, (short) data);
                        this.flagData.addFlagButton(oneFlag.toLowerCase(), item);
                    }
                }
                if (conf.contains("FlagGUI." + oneFlag + ".des")) {
                    description.put(oneFlag, conf.getStringList("FlagGUI." + oneFlag + ".des"));
                }
            }
        }
    }

}
