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

package com.bekvon.bukkit.residence.permissions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

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

import com.bekvon.bukkit.residence.protection.CuboidArea;
import com.bekvon.bukkit.residence.protection.FlagPermissions;
import com.bekvon.bukkit.residence.protection.FlagPermissions.FlagState;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 *
 *         changed by inori 03/17/2012 line 91:limits MaxHeight changed to 255
 */
public class PermissionGroup {
    protected boolean buyIgnoreLimits;
    protected boolean canBuy;
    protected boolean cancreate;
    protected boolean canSell;
    protected double costperarea;
    protected Map<String, Boolean> creatorDefaultFlags;
    protected String defaultEnterMessage;
    protected String defaultLeaveMessage;
    protected FlagPermissions flagPerms;
    protected Map<String, Map<String, Boolean>> groupDefaultFlags;
    protected String groupname;
    protected boolean itemListAccess;
    protected boolean kick;
    protected int leaseGiveTime;
    protected int maxHeight;
    protected int maxLeaseTime;
    protected int maxPhysical;
    protected int maxRentables;
    protected int maxRents;
    protected boolean messageperms;
    protected int minHeight;
    protected double renewcostperarea;
    protected Map<String, Boolean> residenceDefaultFlags;
    protected int resmax;
    protected boolean selectCommandAccess;
    protected int subzonedepth;
    protected boolean tpaccess;
    protected boolean unstuck;
    protected int xmax;
    protected int ymax;
    protected int zmax;

    public PermissionGroup(final String name) {
        flagPerms = new FlagPermissions();
        creatorDefaultFlags = new HashMap<String, Boolean>();
        residenceDefaultFlags = new HashMap<String, Boolean>();
        groupDefaultFlags = new HashMap<String, Map<String, Boolean>>();
        groupname = name;
    }

    public PermissionGroup(final String name, final ConfigurationSection node) {
        this(name);
        this.parseGroup(node);
    }

    public PermissionGroup(final String name, final ConfigurationSection node, final FlagPermissions parentFlagPerms) {
        this(name, node);
        flagPerms.setParent(parentFlagPerms);
    }

    public boolean buyLandIgnoreLimits() {
        return buyIgnoreLimits;
    }

    public boolean canBuyLand() {
        return canBuy;
    }

    public boolean canCreateResidences() {
        return cancreate;
    }

    public boolean canSellLand() {
        return canSell;
    }

    public boolean canSetEnterLeaveMessages() {
        return messageperms;
    }

    public double getCostPerBlock() {
        return costperarea;
    }

    public Set<Entry<String, Boolean>> getDefaultCreatorFlags() {
        return creatorDefaultFlags.entrySet();
    }

    public String getDefaultEnterMessage() {
        return defaultEnterMessage;
    }

    public Set<Entry<String, Map<String, Boolean>>> getDefaultGroupFlags() {
        return groupDefaultFlags.entrySet();
    }

    public String getDefaultLeaveMessage() {
        return defaultLeaveMessage;
    }

    public Set<Entry<String, Boolean>> getDefaultResidenceFlags() {
        return residenceDefaultFlags.entrySet();
    }

    public int getLeaseGiveTime() {
        return leaseGiveTime;
    }

    public double getLeaseRenewCost() {
        return renewcostperarea;
    }

    public int getMaxHeight() {
        return maxHeight;
    }

    public int getMaxLeaseTime() {
        return maxLeaseTime;
    }

    public int getMaxPhysicalPerResidence() {
        return maxPhysical;
    }

    public int getMaxRentables() {
        return maxRentables;
    }

    public int getMaxRents() {
        return maxRents;
    }

    public int getMaxSubzoneDepth() {
        return subzonedepth;
    }

    public int getMaxX() {
        return xmax;
    }

    public int getMaxY() {
        return ymax;
    }

    public int getMaxZ() {
        return zmax;
    }

    public int getMaxZones() {
        return resmax;
    }

    public int getMinHeight() {
        return minHeight;
    }

    public boolean hasFlagAccess(final String flag) {
        return flagPerms.has(flag, false);
    }

    public boolean hasKickAccess() {
        return kick;
    }

    public boolean hasTpAccess() {
        return tpaccess;
    }

    public boolean hasUnstuckAccess() {
        return unstuck;
    }

    public boolean inLimits(final CuboidArea area) {
        if (area.getXSize() > xmax || area.getYSize() > ymax || area.getZSize() > zmax) {
            return false;
        }
        return true;
    }

    public boolean itemListAccess() {
        return itemListAccess;
    }

    public void printLimits(final Player player) {
        final ResidenceMain plugin = ResidenceMain.getInstance();
        player.sendMessage(ChatColor.GRAY + "---------------------------");
        player.sendMessage(ChatColor.YELLOW + "权限 组:" + ChatColor.DARK_AQUA + " " + plugin.getPermissionManager().getPermissionsGroup(player));
        player.sendMessage(ChatColor.YELLOW + "领地 组:" + ChatColor.DARK_AQUA + " " + groupname);
        player.sendMessage(ChatColor.YELLOW + "领地 管理:" + ChatColor.DARK_AQUA + " " + plugin.getPermissionManager().isResidenceAdmin(player));
        player.sendMessage(ChatColor.YELLOW + "允许创建领地:" + ChatColor.DARK_AQUA + " " + cancreate);
        player.sendMessage(ChatColor.YELLOW + "最大领地个数:" + ChatColor.DARK_AQUA + " " + resmax);
        player.sendMessage(ChatColor.YELLOW + "X轴最大长度:" + ChatColor.DARK_AQUA + " " + xmax);
        player.sendMessage(ChatColor.YELLOW + "Z轴最大长度:" + ChatColor.DARK_AQUA + " " + zmax);
        player.sendMessage(ChatColor.YELLOW + "Y轴最大高度:" + ChatColor.DARK_AQUA + " " + ymax);
        player.sendMessage(ChatColor.YELLOW + "高度限制:" + ChatColor.DARK_AQUA + " " + minHeight + " - " + maxHeight);
        player.sendMessage(ChatColor.YELLOW + "最大子领地深度:" + ChatColor.DARK_AQUA + " " + subzonedepth);
        player.sendMessage(ChatColor.YELLOW + "是否允许设置进出消息:" + ChatColor.DARK_AQUA + " " + messageperms);
        player.sendMessage(ChatColor.YELLOW + "你所拥有的领地:" + ChatColor.DARK_AQUA + " " + plugin.getResidenceManager().getOwnedZoneCount(player.getName()));
        if (plugin.getEconomyManager() != null) {
            player.sendMessage(ChatColor.YELLOW + "每个方块需要金钱:" + ChatColor.DARK_AQUA + " " + costperarea);
        }
        player.sendMessage(ChatColor.YELLOW + "Flag 权限:" + ChatColor.DARK_AQUA + " " + flagPerms.listFlags());
        if (plugin.getConfigManager().useLeases()) {
            player.sendMessage(ChatColor.YELLOW + "最大租赁日:" + ChatColor.DARK_AQUA + " " + maxLeaseTime);
            player.sendMessage(ChatColor.YELLOW + "续租日期:" + ChatColor.DARK_AQUA + " " + leaseGiveTime);
            player.sendMessage(ChatColor.YELLOW + "续租方块需要金钱:" + ChatColor.DARK_AQUA + " " + renewcostperarea);
        }
        player.sendMessage(ChatColor.GRAY + "---------------------------");
    }

    public boolean selectCommandAccess() {
        return selectCommandAccess;
    }

    private void parseGroup(final ConfigurationSection limits) {
        if (limits == null) {
            return;
        }
        cancreate = limits.getBoolean("Residence.CanCreate", false);
        resmax = limits.getInt("Residence.MaxResidences", 0);
        maxPhysical = limits.getInt("Residence.MaxAreasPerResidence", 2);
        xmax = limits.getInt("Residence.MaxEastWest", 0);
        ymax = limits.getInt("Residence.MaxUpDown", 0);
        zmax = limits.getInt("Residence.MaxNorthSouth", 0);
        minHeight = limits.getInt("Residence.MinHeight", 0);
        maxHeight = limits.getInt("Residence.MaxHeight", 255);
        tpaccess = limits.getBoolean("Residence.CanTeleport", false);
        subzonedepth = limits.getInt("Residence.SubzoneDepth", 0);
        messageperms = limits.getBoolean("Messaging.CanChange", false);
        defaultEnterMessage = limits.getString("Messaging.DefaultEnter", null);
        defaultLeaveMessage = limits.getString("Messaging.DefaultLeave", null);
        maxLeaseTime = limits.getInt("Lease.MaxDays", 16);
        leaseGiveTime = limits.getInt("Lease.RenewIncrement", 14);
        maxRents = limits.getInt("Rent.MaxRents", 0);
        maxRentables = limits.getInt("Rent.MaxRentables", 0);
        renewcostperarea = limits.getDouble("Economy.RenewCost", 0.02D);
        canBuy = limits.getBoolean("Economy.CanBuy", false);
        canSell = limits.getBoolean("Economy.CanSell", false);
        buyIgnoreLimits = limits.getBoolean("Economy.IgnoreLimits", false);
        costperarea = limits.getDouble("Economy.BuyCost", 0);
        unstuck = limits.getBoolean("Residence.Unstuck", false);
        kick = limits.getBoolean("Residence.Kick", false);
        selectCommandAccess = limits.getBoolean("Residence.SelectCommandAccess", true);
        itemListAccess = limits.getBoolean("Residence.ItemListAccess", true);
        ConfigurationSection node = limits.getConfigurationSection("Flags.Permission");
        Set<String> flags = null;
        if (node != null) {
            flags = node.getKeys(false);
        }
        if (flags != null) {
            final Iterator<String> flagit = flags.iterator();
            while (flagit.hasNext()) {
                final String flagname = flagit.next();
                final boolean access = limits.getBoolean("Flags.Permission." + flagname, false);
                flagPerms.setFlag(flagname, access ? FlagState.TRUE : FlagState.FALSE);
            }
        }
        node = limits.getConfigurationSection("Flags.CreatorDefault");
        if (node != null) {
            flags = node.getKeys(false);
        }
        if (flags != null) {
            final Iterator<String> flagit = flags.iterator();
            while (flagit.hasNext()) {
                final String flagname = flagit.next();
                final boolean access = limits.getBoolean("Flags.CreatorDefault." + flagname, false);
                creatorDefaultFlags.put(flagname, access);
            }

        }
        node = limits.getConfigurationSection("Flags.Default");
        if (node != null) {
            flags = node.getKeys(false);
        }
        if (flags != null) {
            final Iterator<String> flagit = flags.iterator();
            while (flagit.hasNext()) {
                final String flagname = flagit.next();
                final boolean access = limits.getBoolean("Flags.Default." + flagname, false);
                residenceDefaultFlags.put(flagname, access);
            }
        }
        node = limits.getConfigurationSection("Flags.GroupDefault");
        Set<String> groupDef = null;
        if (node != null) {
            groupDef = node.getKeys(false);
        }
        if (groupDef != null) {
            final Iterator<String> groupit = groupDef.iterator();
            while (groupit.hasNext()) {
                final String name = groupit.next();
                final Map<String, Boolean> gflags = new HashMap<String, Boolean>();
                flags = limits.getConfigurationSection("Flags.GroupDefault." + name).getKeys(false);
                final Iterator<String> flagit = flags.iterator();
                while (flagit.hasNext()) {
                    final String flagname = flagit.next();
                    final boolean access = limits.getBoolean("Flags.GroupDefault." + name + "." + flagname, false);
                    gflags.put(flagname, access);
                }
                groupDefaultFlags.put(name, gflags);
            }
        }
    }

}
