/*
 * To change this template, choose Tools | Templates and open the template in the editor.
 */
package pw.yumc.Residence.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

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

import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.utils.ActionBar;
import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class ConfigManager {
    private boolean enforceAreaInsideArea;
    protected boolean actionBar;
    protected boolean adminOps;
    protected boolean adminsOnly;
    protected boolean allowEmptyResidences;
    protected int autoSaveInt;
    protected ChatColor chatColor;
    protected boolean chatEnable;
    protected List<Integer> customBothClick;
    protected List<Integer> customContainers;
    protected List<Integer> customRightClick;
    protected String defaultGroup;
    protected String economySystem;
    protected boolean enableDebug;
    protected boolean enableEconomy;
    protected boolean enableLeaseMoneyAccount;
    protected boolean enableRentSystem;
    protected boolean flagsInherit;
    protected FlagPermissions globalCreatorDefaults;
    protected Map<String, FlagPermissions> globalGroupDefaults;
    protected FlagPermissions globalResidenceDefaults;
    protected int infoToolId;
    protected String language;
    protected boolean leaseAutoRenew;
    protected int leaseCheckInterval;
    protected boolean legacyperms;
    protected int minMoveUpdate;
    protected String multiworldPlugin;
    protected String namefix;
    protected ResidenceMain plugin;
    protected boolean preventBuildInRent;
    protected int rentCheckInterval;
    protected int selectionToolId;
    protected boolean showIntervalMessages;
    protected boolean spoutEnable;
    protected boolean stopOnSaveError;
    protected boolean useLeases;

    public ConfigManager(final ResidenceMain plugin) {
        this.plugin = plugin;
        globalCreatorDefaults = new FlagPermissions();
        globalResidenceDefaults = new FlagPermissions();
        globalGroupDefaults = new HashMap<String, FlagPermissions>();
        this.load(plugin.getConfig());
    }

    public boolean allowAdminsOnly() {
        return adminsOnly;
    }

    public boolean allowEmptyResidences() {
        return allowEmptyResidences;
    }

    public boolean autoRenewLeases() {
        return leaseAutoRenew;
    }

    public boolean chatEnabled() {
        return chatEnable;
    }

    public boolean debugEnabled() {
        return enableDebug;
    }

    public boolean enabledRentSystem() {
        return enableRentSystem && enableEconomy();
    }

    public boolean enableEconomy() {
        return enableEconomy && plugin.getEconomyManager() != null;
    }

    public boolean enableLeaseMoneyAccount() {
        return enableLeaseMoneyAccount;
    }

    public boolean enableSpout() {
        return spoutEnable;
    }

    public boolean flagsInherit() {
        return flagsInherit;
    }

    public int getAutoSaveInterval() {
        return autoSaveInt;
    }

    public ChatColor getChatColor() {
        return chatColor;
    }

    public List<Integer> getCustomBothClick() {
        return customBothClick;
    }

    public List<Integer> getCustomContainers() {
        return customContainers;
    }

    public List<Integer> getCustomRightClick() {
        return customRightClick;
    }

    public String getDefaultGroup() {
        return defaultGroup;
    }

    public String getEconomySystem() {
        return economySystem;
    }

    public boolean getEnforceAreaInsideArea() {
        return enforceAreaInsideArea;
    }

    public FlagPermissions getGlobalCreatorDefaultFlags() {
        return globalCreatorDefaults;
    }

    public Map<String, FlagPermissions> getGlobalGroupDefaultFlags() {
        return globalGroupDefaults;
    }

    public FlagPermissions getGlobalResidenceDefaultFlags() {
        return globalResidenceDefaults;
    }

    public int getInfoToolID() {
        return infoToolId;
    }

    public String getLanguage() {
        return language;
    }

    public int getLeaseCheckInterval() {
        return leaseCheckInterval;
    }

    public int getMinMoveUpdateInterval() {
        return minMoveUpdate;
    }

    public String getMultiworldPlugin() {
        return multiworldPlugin;
    }

    public boolean getOpsAreAdmins() {
        return adminOps;
    }

    public int getRentCheckInterval() {
        return rentCheckInterval;
    }

    public String getResidenceNameRegex() {
        return namefix;
    }

    public int getSelectionTooldID() {
        return selectionToolId;
    }

    public boolean preventRentModify() {
        return preventBuildInRent;
    }

    public boolean showIntervalMessages() {
        return showIntervalMessages;
    }

    public boolean stopOnSaveError() {
        return stopOnSaveError;
    }

    public boolean useActionBar() {
        return actionBar;
    }

    public boolean useLeases() {
        return useLeases;
    }

    public boolean useLegacyPermissions() {
        return legacyperms;
    }

    @SuppressWarnings("deprecation")
    private void load(final FileConfiguration config) {
        try {
            if (!config.isConfigurationSection("Global")) {
                plugin.getLogger().warning("配置文件主键丢失 恢复默认配置文件!");
                plugin.getConfig().addDefaults(new FileConfig(plugin));
            }
            defaultGroup = config.getString("Global.DefaultGroup", "default").toLowerCase();
            adminsOnly = config.getBoolean("Global.AdminOnlyCommands", false);
            useLeases = config.getBoolean("Global.UseLeaseSystem", false);
            leaseAutoRenew = config.getBoolean("Global.LeaseAutoRenew", true);
            enableEconomy = config.getBoolean("Global.EnableEconomy", false);
            economySystem = config.getString("Global.EconomySystem", "iConomy");
            infoToolId = config.getInt("Global.InfoToolId", Material.STRING.getId());
            selectionToolId = config.getInt("Global.SelectionToolId", Material.WOOD_AXE.getId());
            adminOps = config.getBoolean("Global.AdminOPs", true);
            multiworldPlugin = config.getString("Global.MultiWorldPlugin");
            enableRentSystem = config.getBoolean("Global.EnableRentSystem", false);
            rentCheckInterval = config.getInt("Global.RentCheckInterval", 10);
            leaseCheckInterval = config.getInt("Global.LeaseCheckInterval", 10);
            autoSaveInt = config.getInt("Global.SaveInterval", 10);
            flagsInherit = config.getBoolean("Global.ResidenceFlagsInherit", false);
            minMoveUpdate = config.getInt("Global.MoveCheckInterval", 500);
            chatEnable = config.getBoolean("Global.ResidenceChatEnable", true);
            actionBar = config.getBoolean("Global.UseActionBar", true);
            enforceAreaInsideArea = config.getBoolean("Global.EnforceAreaInsideArea", false);
            language = config.getString("Global.Language", "English");
            globalCreatorDefaults = FlagPermissions.parseFromConfigNode("CreatorDefault", config.getConfigurationSection("Global"));
            globalResidenceDefaults = FlagPermissions.parseFromConfigNode("ResidenceDefault", config.getConfigurationSection("Global"));
            preventBuildInRent = config.getBoolean("Global.PreventRentModify", true);
            stopOnSaveError = config.getBoolean("Global.StopOnSaveFault", true);
            legacyperms = config.getBoolean("Global.LegacyPermissions", false);
            namefix = config.getString("Global.ResidenceNameRegex", "[^A-Za-z0-9\\u4e00-\\u9fa5\\-\\_]");// "[^a-zA-Z0-9\\-\\_]"
            showIntervalMessages = config.getBoolean("Global.ShowIntervalMessages", false);
            spoutEnable = config.getBoolean("Global.EnableSpout", false);
            enableLeaseMoneyAccount = config.getBoolean("Global.EnableLeaseMoneyAccount", true);
            enableDebug = config.getBoolean("Global.EnableDebug", false);
            customContainers = config.getIntegerList("Global.CustomContainers");
            customBothClick = config.getIntegerList("Global.CustomBothClick");
            customRightClick = config.getIntegerList("Global.CustomRightClick");
        } catch (final Exception e) {
            throw new RuntimeException("领地配置文件载入错误...", e);
        }
        if (actionBar && plugin.isGt1_8()) {
            actionBar = ActionBar.init();
        } else {
            actionBar = false;
        }
        final ConfigurationSection node = config.getConfigurationSection("Global.GroupDefault");
        if (node != null) {
            final Set<String> keys = node.getConfigurationSection(defaultGroup).getKeys(false);
            if (keys != null) {
                for (final String key : keys) {
                    globalGroupDefaults.put(key, FlagPermissions.parseFromConfigNode(key, config.getConfigurationSection("Global.GroupDefault")));
                }
            }
        }
        try {
            chatColor = ChatColor.valueOf(config.getString("Global.ResidenceChatColor", "DARK_PURPLE"));
        } catch (final Exception ex) {
            chatColor = ChatColor.DARK_PURPLE;
        }
    }
}
