package com.yuyu.srdisassemble.config;

import com.yuyu.srdisassemble.data.JdbcSqlClass;
import com.yuyu.srdisassemble.pojo.CanDisItem;
import com.yuyu.srdisassemble.pojo.DisProduct;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author 峰。
 * @version 1.0
 * @project SrDisassemble
 * @date 2024/6/11 16:11:01
 * @description 配置文件相关
 */
public class ConfigManager {
    private Logger logger;
    private HashMap<String, CanDisItem> disProductHashMap;
    private Plugin plugin;
    private String username;
    private String password;
    private String driver;
    private String url;
    public ConfigManager(Plugin plugin) {
        this.plugin = plugin;
        this.logger = plugin.getLogger();
        this.loadDataBase();

        plugin.saveResource("disProduct.yml",false);
        plugin.saveResource("disToItem.yml",false);
        plugin.saveResource("ItemDetail.yml",false);

        File disProduct = new File(plugin.getDataFolder(), "disProduct.yml");
        File disToItem = new File(plugin.getDataFolder(), "disToItem.yml");
        File ItemDetail = new File(plugin.getDataFolder(), "ItemDetail.yml");

        FileConfiguration disProductConfig = YamlConfiguration.loadConfiguration(disProduct);
        FileConfiguration disToItemProduct = YamlConfiguration.loadConfiguration(disToItem);
        FileConfiguration itemDetailProduct = YamlConfiguration.loadConfiguration(ItemDetail);
        this.disProductHashMap = new HashMap<>();

        //先读取可分解的产物
        List<String> disProductConfigStringList = disProductConfig.getStringList("SrDisassemble");
        for (String disProductConfigString : disProductConfigStringList) {
            HashMap<String,DisProduct> disProductHashMap = new HashMap<>();
            //循环读取每个需要分解的产物
            int anInt = disToItemProduct.getInt("SrDisassemble." + disProductConfigString + ".disTime");
            String tagetIndex = disToItemProduct.getString("SrDisassemble." + disProductConfigString + ".index");
            CanDisItem canDisItem = new CanDisItem(disProductConfigString, anInt,disProductConfigString,tagetIndex);
            List<String> stringList = disToItemProduct.getStringList("SrDisassemble." + disProductConfigString +".disProduct");
            for (String product : stringList) {
                Double proMax = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMax");
                Double proMin = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMin");
                String index = itemDetailProduct.getString("SrDisassemble."+disProductConfigString+"."+product+".index");
                DisProduct disPojo = new DisProduct(product, index, proMax, proMin);
                logger.info(ChatColor.GREEN+disPojo.toString());
                disProductHashMap.put(product,disPojo);
            }
            canDisItem.setDisProductHashMap(disProductHashMap);

            this.disProductHashMap.put(disProductConfigString,canDisItem);
        }

    }

    public void loadDataBase(){
        plugin.saveResource("datasource.yml",false);

        File datasource = new File(plugin.getDataFolder(), "datasource.yml");

        FileConfiguration datasourceConfig = YamlConfiguration.loadConfiguration(datasource);

        JdbcSqlClass.setUser(datasourceConfig.getString("database.sql.username"));
        JdbcSqlClass.setPassword(datasourceConfig.getString("database.sql.password"));
        JdbcSqlClass.setDriver(datasourceConfig.getString("database.sql.driver"));
        JdbcSqlClass.setUrl(datasourceConfig.getString("database.sql.url"));

        logger.info(ChatColor.GREEN+"username:"+JdbcSqlClass.getUser()+"\npassword:"+JdbcSqlClass.getPassword()
        +"\ndriver:"+JdbcSqlClass.getDriver()+"\nurl:"+JdbcSqlClass.getUrl());

        JdbcSqlClass.initTable();
        JdbcSqlClass.setChartseUtf8();

        //获取到datasource.yml的信息后建立数据库连接
//        SqlData.initTable();
    }


    public void reload() {
        //重载前先清零
        disProductHashMap.clear();
        loadDataBase();

        plugin.saveResource("disProduct.yml",false);
        plugin.saveResource("disToItem.yml",false);
        plugin.saveResource("ItemDetail.yml",false);

        File disProduct = new File(plugin.getDataFolder(), "disProduct.yml");
        File disToItem = new File(plugin.getDataFolder(), "disToItem.yml");
        File ItemDetail = new File(plugin.getDataFolder(), "ItemDetail.yml");

        FileConfiguration disProductConfig = YamlConfiguration.loadConfiguration(disProduct);
        FileConfiguration disToItemProduct = YamlConfiguration.loadConfiguration(disToItem);
        FileConfiguration itemDetailProduct = YamlConfiguration.loadConfiguration(ItemDetail);
        this.disProductHashMap = new HashMap<>();

        //先读取可分解的产物
        List<String> disProductConfigStringList = disProductConfig.getStringList("SrDisassemble");
        for (String disProductConfigString : disProductConfigStringList) {
            HashMap<String,DisProduct> disProductHashMap = new HashMap<>();
            //循环读取每个需要分解的产物
            int anInt = disToItemProduct.getInt("SrDisassemble." + disProductConfigString + ".disTime");
            String tagetIndex = disToItemProduct.getString("SrDisassemble." + disProductConfigString + ".index");
            CanDisItem canDisItem = new CanDisItem(disProductConfigString, anInt,disProductConfigString,tagetIndex);
            List<String> stringList = disToItemProduct.getStringList("SrDisassemble." + disProductConfigString +".disProduct");
            for (String product : stringList) {
                Double proMax = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMax");
                Double proMin = itemDetailProduct.getDouble("SrDisassemble."+disProductConfigString+"."+product+".probabilityMin");
                String index = itemDetailProduct.getString("SrDisassemble."+disProductConfigString+"."+product+".index");
                DisProduct disPojo = new DisProduct(product, index, proMax, proMin);
                logger.info(ChatColor.GREEN+disPojo.toString());
                disProductHashMap.put(product,disPojo);
            }
            canDisItem.setDisProductHashMap(disProductHashMap);

            this.disProductHashMap.put(disProductConfigString,canDisItem);
        }
    }

    public HashMap<String, CanDisItem> getDisProductHashMap() {
        return disProductHashMap;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getDriver() {
        return driver;
    }

    public String getUrl() {
        return url;
    }
}
