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

package com.bekvon.bukkit.residence.protection;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import pw.yumc.Residence.ResidenceMain;

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

    private final Map<String, Map<String, FlagPermissions>> lists;
    ResidenceMain plugin;

    public PermissionListManager(final ResidenceMain plugin) {
        this.plugin = plugin;
        lists = Collections.synchronizedMap(new HashMap<String, Map<String, FlagPermissions>>());
    }

    @SuppressWarnings("unchecked")
    public static PermissionListManager load(final ResidenceMain plugin, final Map<String, Object> root) {
        final PermissionListManager p = new PermissionListManager(plugin);
        if (root != null) {
            for (final Entry<String, Object> players : root.entrySet()) {
                try {
                    final Map<String, Object> value = (Map<String, Object>) players.getValue();
                    final Map<String, FlagPermissions> loadedMap = Collections.synchronizedMap(new HashMap<String, FlagPermissions>());
                    for (final Entry<String, Object> list : value.entrySet()) {
                        loadedMap.put(list.getKey(), FlagPermissions.load((Map<String, Object>) list.getValue()));
                    }
                    p.lists.put(players.getKey(), loadedMap);
                } catch (final Exception ex) {
                    plugin.getLogger().warning(" - 玩家: " + players.getKey() + " 的权限列表载入失败...");
                }
            }
        }
        return p;
    }

    public void applyListToResidence(final Player player, final String listname, final String areaname, final boolean resadmin) {
        final FlagPermissions list = this.getList(player.getName(), listname);
        if (list == null) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidList"));
            return;
        }
        final ClaimedResidence res = plugin.getResidenceManager().getByName(areaname);
        if (res == null) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidResidence"));
            return;
        }
        res.getPermissions().applyTemplate(player, list, resadmin);
    }

    public FlagPermissions getList(final String player, final String listname) {
        final Map<String, FlagPermissions> get = lists.get(player);
        if (get == null) {
            return null;
        }
        return get.get(listname);
    }

    public void makeList(final Player player, final String listname) {
        Map<String, FlagPermissions> get = lists.get(player.getName());
        if (get == null) {
            get = new HashMap<String, FlagPermissions>();
            lists.put(player.getName(), get);
        }
        FlagPermissions perms = get.get(listname);
        if (perms == null) {
            perms = new FlagPermissions();
            get.put(listname, perms);
            player.sendMessage(ChatColor.GREEN + plugin.getLanguage().getPhrase("ListCreate", listname));
        } else {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ListExists"));
        }
    }

    public void printList(final Player player, final String listname) {
        final FlagPermissions list = this.getList(player.getName(), listname);
        if (list == null) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidList"));
            return;
        }
        player.sendMessage(ChatColor.LIGHT_PURPLE + "------Permission Template------");
        player.sendMessage(plugin.getLanguage().getPhrase("Name") + ": " + ChatColor.GREEN + listname);
        list.printFlags(player);
    }

    public void printLists(final Player player) {
        final StringBuilder sbuild = new StringBuilder();
        final Map<String, FlagPermissions> get = lists.get(player.getName());
        sbuild.append(ChatColor.YELLOW + plugin.getLanguage().getPhrase("Lists") + ":" + ChatColor.DARK_AQUA + " ");
        if (get != null) {
            for (final Entry<String, FlagPermissions> thislist : get.entrySet()) {
                sbuild.append(thislist.getKey()).append(" ");
            }
        }
        player.sendMessage(sbuild.toString());
    }

    public void removeList(final Player player, final String listname) {
        final Map<String, FlagPermissions> get = lists.get(player.getName());
        if (get == null) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidList"));
            return;
        }
        final FlagPermissions list = get.get(listname);
        if (list == null) {
            player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("InvalidList"));
            return;
        }
        get.remove(listname);
        player.sendMessage(ChatColor.RED + plugin.getLanguage().getPhrase("ListRemoved"));
    }

    public Map<String, Object> save() {
        final Map<String, Object> root = new LinkedHashMap<String, Object>();
        for (final Entry<String, Map<String, FlagPermissions>> players : lists.entrySet()) {
            final Map<String, Object> saveMap = new LinkedHashMap<String, Object>();
            final Map<String, FlagPermissions> map = players.getValue();
            for (final Entry<String, FlagPermissions> list : map.entrySet()) {
                saveMap.put(list.getKey(), list.getValue().save());
            }
            root.put(players.getKey(), saveMap);
        }
        return root;
    }
}
