package org.black_ixx.bossshop.managers;

import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.lottery.LotteryManager;
import org.black_ixx.bossshop.mail.MailManager;
import org.black_ixx.bossshop.sale.SaleManager;

import cc.util.bossshop.filemanager.AConfigManager;

public class ConfigManager extends AConfigManager{

    private boolean signs;
    private boolean ic_hide;
    private boolean transaction_log;
    private boolean allow_unsafe_enchantments;
    private boolean debug;
    private String mainshop;
    private int mCoolTime=5;
    private boolean mAutoClearNBT=true;
    private int mHideItemFlag=63;
    private int mShopOpenDelay=0;

    private BossShop plugin;

    public ConfigManager(BossShop pplugin){
        super(pplugin,"1.4");
        this.reloadConfig();
    }

    @Override
    public boolean reloadConfig(){
        if(!super.reloadConfig()) return false;
        this.checkUpdate();
        this.addDefaults();
        this.saveConfig();
        this.signs=this.mConfig.getBoolean("signs.enabled")||this.mConfig.getBoolean("EnableSigns");
        this.mainshop=this.mConfig.getString("MainShop","Menu");
        this.ic_hide=this.mConfig.getBoolean("HideItemsPlayersDoNotHavePermissionsFor",false);
        this.transaction_log=this.mConfig.getBoolean("EnableTransactionLog",false);
        this.allow_unsafe_enchantments=this.mConfig.getBoolean("AllowUnsafeEnchantments",false);
        this.mCoolTime=this.mConfig.getInt("CoolTime",5);
        this.debug=this.mConfig.getBoolean("debug",false);
        this.mAutoClearNBT=this.mConfig.getBoolean("AutoClearNBT",true);
        this.mHideItemFlag=this.mConfig.getInt("HideItemFlag",63);
        this.mShopOpenDelay=this.mConfig.getInt("OpenShopDelay",0);
        this.mShopOpenDelay/=50;
        return true;
    }

    protected boolean checkUpdate(){
        String tVersion=this.mConfig.getString(SEC_CFG_VERSION,"1.0");
        if(tVersion.compareToIgnoreCase(this.mVersion)>=0) return false;
        if(tVersion.compareToIgnoreCase("1.0")==0){// 1.0-->1.1
            tVersion="1.1";
            Object obj=this.mConfig.get("SaleSystem.OutDateItemNumbLimit");
            if(obj!=null) this.mConfig.set("MailSystem.MaxSize",obj);
            this.mConfig.set("SaleSystem.OutDateItemNumbLimit",null);
            obj=this.mConfig.get("SaleSystem.AutoClearNBT");
            if(obj!=null) this.mConfig.set("AutoClearNBT",true);
            this.mConfig.set("SaleSystem.AutoClearNBT",null);
        }
        if(tVersion.compareToIgnoreCase("1.1")==0){// 1.1-->1.2
            tVersion="1.2";
            int coolTime=this.mConfig.getInt("CoolTime",1);
            this.mConfig.set("CoolTime",coolTime*1000);
        }
        if(tVersion.compareToIgnoreCase("1.2")==0){// 1.2-->1.3
            tVersion="1.3";
            int item=this.mConfig.getInt("BindMenuItem");
            this.mConfig.set("BindMenuItem",null);
            this.mConfig.set("OpenMainShop.BindItem",item);
        }
        if(tVersion.compareToIgnoreCase("1.3")==0){// 1.3-->1.4
            tVersion="1.4";
            this.mConfig.set("AutoClearNBT",null);
            this.mConfig.set("DisableUpdateNotifications",null);
        }
        this.mConfig.set(SEC_CFG_VERSION,this.mVersion);
        return true;
    }

    public void addDefaults(){
        super.addDefaults();
        this.mConfig.addDefault("EnableSigns",true,"启用贴牌创建BossShop商店入口");
        this.mConfig.addDefault("LogPrefix","[BossShop]","用于日志输出前缀,基本不用");
        this.mConfig.addDefault("MsgPrefix","&c[&7&l商店&c]&b","前台聊天以及后台大部分输出的前缀");
        this.mConfig.addDefault("MainShop","Menu","BossShop主商店");
        this.mConfig.addDefault("ItemNameLang","zh_CN","物品名字翻译语言,用于寄售物品和邮件 时候使用","当前支持zh_CN,zh_TW,en_US,其中en_US翻译不需要依赖文件,且翻译准确","更多翻译支持请到.minecraft/assets/objects下找到对应的翻译文件,更改名字格式然后复制到插件目录就可以了","此项配置的原版物品名字需要靠ItemName_xx_XX文件,mod物品名字会自动获取","部分mod或mod物品可能存在翻译问题,比如怪物蛋(但是英文是肯定对的)","如果整个mod不能翻译,可能是该mod里没有对应的语言文件","此项配置需要重载整个插件才能使原版翻译生效","可以在手上拿着物品时输入/BS name查看手上的物品的翻译");
        this.mConfig.addDefault("OpenMainShop.BindItem",347,"打开BossShop主商店的物品");
        this.mConfig.addDefault("OpenMainShop.SneakNeed",true,"使用物品打开商店时是否需要潜行");
        this.mConfig.addDefault("OpenShopDelay",0,"命令打开商店的延迟","用来兼容例如时钟命令等插件");
        this.mConfig.addDefault("HideItemsPlayersDoNotHavePermissionsFor",false,"隐藏玩家没有权限购买的物品");
        this.mConfig.addDefault("EnableTransactionLog",false,"启用交易记录");
        this.mConfig.addDefault("AllowUnsafeEnchantments",false,"允许不安全的附魔");
        this.mConfig.addDefault("MultiplierGroups.Enabled",false);
        this.mConfig.addDefault("MultiplierGroups.List",new String[]{"Permission.Node:<type>:<multiplier>","BossShop.PriceMultiplier.Points1:points:0.75","BossShop.PriceMultiplier.Points1:points:0.5","BossShop.PriceMultiplier.Money1:money:0.75","BossShop.PriceMultiplier.Money2:money:0.5","BossShop.PriceMultiplier.MoneyNegative:money:2.0","BossShop.PriceMultiplier.Exp:exp:0.8",});
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_ShopBaseName,"SYSTEM_SALE","寄售系统主配置");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_ShopSize,54,"寄售商店大小","必须为9的整数,不超过81,而且商店最后两格需要留空用来制作导航按钮","一旦设置过大小并创建了寄售商店后,就不要改这个值了,除非同时修改商店文件,不然导航功能会失效");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_ShopMaxNumb,2,"寄售商店最大数量");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_MoneyPoundage,5,"金币收入手续费,百分比0-99");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_PointsPoundage,5,"点券收入手续费,百分比0-99");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_MoneyItem,9,"金币寄售默认显示的物品,只支持数值,不支持12:3的类型");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_PointsItem,10,"点券寄售默认显示的物品,只支持数值,不支持12:3的类型");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_SaleTime,86400,"默认寄售时长(秒),这里是一天");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_UnsaleCost,1000,"下架需要花费的金币数量,默认没有下架权限","权限BossShop.unsale.user: 下架自己物品的权限","权限BossShop.unsale.admin: 下架所有人物品的权限,并且不要支付下架花费");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_UnsaleCount,3,"每天的下架数量限制,拥有BossShop.unsale.admin的使人可以无视该权限","下架记录保存在buyRecord.yml文件中");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_PerOneSaleLimit,3,"个人允许最多同时寄售物品数量的上限,如果玩家有BossShop.sale.unlimited权限可以无限寄售");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_SaleItemName,"&2寄售: %bs_sale_item_sigle_count% %bs_sale_item_name%","设置寄售物品的显示格式","如果一行中有空格的话需要用单引号把整句括起来","可用的变量,SaleItemLore中也可以使用这些变量","%bs_sale_owner_name%        寄售者的名字","%bs_sale_item_id%           寄售物品的编号","%bs_sale_item_sigle_count%  寄售物品单份数量","%bs_sale_item_part_count%   寄售物品份数","%bs_sale_item_type%         寄售物品类型,[金币,点券,物品],如果要修改这几个请转到语言列表的Word节点","%bs_sale_item_name%         寄售物品名字,[金币,点券,物品英文名],前两项同上,最后一项没办法,获取不到中文显示用的名字","%bs_sale_price%             寄售物品价格,这个只显示数字","%bs_sale_price_type%        寄售物品类型,[金币,点券],要修改同上上");
        this.mConfig.addDefault(SaleManager.CFG_MAIN+"."+SaleManager.CFG_SaleItemLore,new String[]{"&3寄售者: %bs_sale_owner_name%","&2寄售编号: %bs_sale_item_id%","&c价格: %bs_sale_price% %bs_sale_price_type%"},"设置寄售物品的lore格式,时间限制和数量限制为默认lore,如果需要修改请转到语言文件","lore最好加上颜色,不然默认是斜体紫色,丑!","lore默认显示在物品属性下面,过期信息,数量限制等信息的上面");
        this.mConfig.addDefault(MailManager.CFG_MAIN+"."+MailManager.SEC_MaxSize,10,"邮箱大小,超过此大小的物品将不会保存");
        this.mConfig.addDefault(MailManager.CFG_MAIN+"."+MailManager.SEC_SendCost,1000,"发送邮件的花费(金币)");
        this.mConfig.addDefault(LotteryManager.CFG_MAIN+"."+LotteryManager.CFG_ReflashBaseTicks,5,"基础Ticks,多少Ticks快速刷新一次背包");
        this.mConfig.addDefault(LotteryManager.CFG_MAIN+"."+LotteryManager.CFG_ReflashLowMultiple,4,"预览抽奖时,过了多少倍ReflashBaseTicks后刷新一次背包");
        this.mConfig.addDefault(LotteryManager.CFG_MAIN+"."+LotteryManager.CFG_RefalshFastCount,20,"进行抽奖时,刷新背包多少次");
        this.mConfig.addDefault(LotteryManager.CFG_MAIN+"."+LotteryManager.CFG_ShowPrecent,34,"抽奖以及未抽奖时,物品显示比例,最高50,交替显示(50的话就只有两个显示样子了)");
        this.mConfig.addDefault("AutoClear.NBTExpiredDays",7,"NBT过期天数,超过该天数未被引用而且是自动创建的NBT会被清理");
        this.mConfig.addDefault("HideItemFlag",63,"隐藏寄售物品的攻击效果等属性"," 1|0.HIDE_ENCHANTS           附魔"," 2|1.HIDE_ATTRIBUTES         前缀"," 4|2.HIDE_UNBREAKABLE        不可破坏","  8|3.HIDE_DESTROYS,          受损度","16|4.HIDE_PLACED_ON,         放置","32|5.HIDE_POTION_EFFECTS     药水效果","如何隐藏物品的状态??如果想隐藏相应的属性,只要把前面的数字加起来就可以了,如果要全部隐藏,就是63(默认)","注意此项设置只在1.8.3及其以后的版本有效,低版本的只有设置0和非零来开启全部或关闭全部显示,不能选择关闭什么","更低版本的可能不支持关闭","上面的表也可能存在错误就是前面的数字对应的不是这个属性的隐藏");
        this.mConfig.addDefault("PointsPlugin","auto-detect","可选[PlayerPoints,PointsAPI,EnjinMinecraftPlugin,CommandPoints]","如果是[auto-detect],将会自动选择");
        this.mConfig.addDefault("CoolTime",200,"玩家点击商品的间隔(毫秒),无论是否购买成功,都会重复点击会重新计时");
    }

    public boolean signsEnabled(){
        return signs;
    }

    public String getMainShop(){
        return mainshop;
    }

    public boolean hideNoPermitItem(){
        return ic_hide;
    }

    public boolean transactionLogEnabled(){
        return transaction_log;
    }

    public boolean allowUnsafeEnchantments(){
        return allow_unsafe_enchantments;
    }

    public boolean debug(){
        return this.debug;
    }

    public int getCoolTime(){
        return this.mCoolTime;
    }

    public boolean isAutoClearNBT(){
        return this.mAutoClearNBT;
    }

    public int getHideItemFlag(){
        return this.mHideItemFlag;
    }
    
    /**
     * 商店打开延迟,单位为tick
     */
    public int getShopOpenDelay(){
        return this.mShopOpenDelay<0?0:this.mShopOpenDelay;
    }
    
}
