/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pw.yumc.Residence.itemlist;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

/**
 *
 * @author Administrator
 */
public class ItemList {

    public static enum ListType {
        BLACKLIST,
        WHITELIST,
        IGNORELIST,
        OTHER
    }

    protected List<Material> list;

    protected ListType type;

    public ItemList(final ListType listType) {
        this();
        type = listType;
    }

    protected ItemList() {
        list = new ArrayList<Material>();
    }

    public static ItemList load(final Map<String, Object> map) {
        final ItemList newlist = new ItemList();
        return load(map, newlist);
    }

    public static ItemList readList(final ConfigurationSection node) {
        return ItemList.readList(node, new ItemList());
    }

    @SuppressWarnings("unchecked")
    protected static ItemList load(final Map<String, Object> map, final ItemList newlist) {
        try {
            newlist.type = ListType.valueOf((String) map.get("Type"));
            final List<String> list = (List<String>) map.get("ItemList");
            for (final String item : list) {
                newlist.add(Material.valueOf(item));
            }
        } catch (final Exception ex) {
        }
        return newlist;
    }

    @SuppressWarnings("deprecation")
    protected static ItemList readList(final ConfigurationSection node, final ItemList list) {
        final ListType type = ListType.valueOf(node.getString("Type", "").toUpperCase());
        list.type = type;
        final List<String> items = node.getStringList("Items");
        if (items != null) {
            for (final String item : items) {
                int parse = -1;
                try {
                    parse = Integer.parseInt(item);
                } catch (final Exception ex) {
                }
                if (parse == -1) {
                    try {
                        list.add(Material.valueOf(item.toUpperCase()));
                    } catch (final Exception ex) {
                    }
                } else {
                    try {
                        list.add(Material.getMaterial(parse));
                    } catch (final Exception ex) {
                    }
                }
            }
        }
        return list;
    }

    public void add(final Material mat) {
        if (!list.contains(mat))
            list.add(mat);
    }

    public boolean contains(final Material mat) {
        return list.contains(mat);
    }

    public int getListSize() {
        return list.size();
    }

    public ListType getType() {
        return type;
    }

    public boolean isAllowed(final Material mat) {
        if (type == ListType.BLACKLIST) {
            if (list.contains(mat)) {
                return false;
            }
            return true;
        } else if (type == ListType.WHITELIST) {
            if (list.contains(mat)) {
                return true;
            }
            return false;
        }
        return true;
    }

    public boolean isIgnored(final Material mat) {
        if (type == ListType.IGNORELIST) {
            if (list.contains(mat)) {
                return true;
            }
        }
        return false;
    }

    public boolean isListed(final Material mat) {
        return this.contains(mat);
    }

    public void printList(final Player player) {
        final StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (final Material mat : list) {
            if (!first)
                builder.append(", ");
            else
                builder.append(ChatColor.YELLOW);
            builder.append(mat);
            first = false;
        }
        player.sendMessage(builder.toString());
    }

    public void remove(final Material mat) {
        list.remove(mat);
    }

    public Map<String, Object> save() {
        final Map<String, Object> saveMap = new LinkedHashMap<String, Object>();
        saveMap.put("Type", type.toString());
        final List<String> saveList = new ArrayList<String>();
        for (final Material mat : list) {
            saveList.add(mat.toString());
        }
        saveMap.put("ItemList", saveList);
        return saveMap;
    }

    public Material[] toArray() {
        final Material mats[] = new Material[list.size()];
        int i = 0;
        for (final Material mat : list) {
            mats[i] = mat;
            i++;
        }
        return mats;
    }

    public boolean toggle(final Material mat) {
        if (list.contains(mat)) {
            list.remove(mat);
            return false;
        }
        list.add(mat);
        return true;
    }
}
