package com.yutian.minerwar.manager;

import com.io.yutian.mclive.event.ZhuboAPI;
import com.yutian.minerwar.data.GiftData;
import com.yutian.minerwar.data.LikeData;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class GameManager {
    private File manageFile;
    private FileConfiguration manageConfig;
    private boolean keyInputd = false;
    private int goldOreGoal;
    private int gameGoal;
    private long gameTime;
    private HashMap<String, LikeData> likeDataMap = new HashMap<>();
    private HashMap<String, GiftData> giftDataMap = new HashMap<>();
    private static boolean isRandomBox;
    public GameManager(){
        Bukkit.getConsoleSender().sendMessage("[日志 - 矿工战争] 工具注册:");
        loadRandomBoxPlugin();
        Bukkit.getConsoleSender().sendMessage("[日志 - 矿工战争] 游戏设置:");
        loadSettingsData();
        Bukkit.getConsoleSender().sendMessage("[日志 - 矿工战争] 事件注册:");
        File file = new File("./plugins/游戏设置","礼物设置.yml");
        FileConfiguration gift_yml = YamlConfiguration.loadConfiguration(file);
        ConfigurationSection likeSection = gift_yml.getConfigurationSection("点赞整蛊");
        if (likeSection != null) {
            for (String key : likeSection.getKeys(false)) {
                String effect = likeSection.getString(key+".效果");
                int likeAmount = likeSection.getInt(key+".点赞次数");
                String likesounds = likeSection.getString(key+".声音","wau");
                likeDataMap.put(effect,new LikeData(effect,likeAmount,likesounds));
                String message = "事件: "+effect+"   条件: 点赞x"+likeAmount;
                Bukkit.getConsoleSender().sendMessage(message);
            }
        }
        ConfigurationSection giftSection = gift_yml.getConfigurationSection("礼物设置");
        if(giftSection != null){
            ZhuboAPI.setGameGiftList(new ArrayList<>(giftSection.getKeys(false)));
            for (String giftName : giftSection.getKeys(false)) {
                giftDataMap.put(giftName, new GiftData(giftName, gift_yml));
            }
        }
    }
    public void loadSettingsData(){
        manageFile = new File("./plugins/游戏设置","游戏设置.yml");
        manageConfig = YamlConfiguration.loadConfiguration(manageFile);
        gameGoal = manageConfig.getInt("挑战目标",5);
        Bukkit.getConsoleSender().sendMessage("挑战完成次数: "+gameGoal+"次");
        goldOreGoal = manageConfig.getInt("金矿数量",200);
        Bukkit.getConsoleSender().sendMessage("金矿采集数量: "+goldOreGoal+"个");
        gameTime = manageConfig.getLong("游戏时间定格",5500L);
        Bukkit.getConsoleSender().sendMessage("游戏时间定格: "+gameTime+"刻");
    }

    public void loadRandomBoxPlugin() {
        if(Bukkit.getPluginManager().getPlugin("RandomBox") != null){
            isRandomBox = true;
            Bukkit.getConsoleSender().sendMessage("- 随机盲盒: 已启用");
        } else {
            isRandomBox = false;
            Bukkit.getConsoleSender().sendMessage("- 随机盲盒: 未启用");
        }
    }

    public void saveGameSettings() {
        manageConfig.set("游戏时间定格",gameTime);
        try {
            manageConfig.save(manageFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void setKeyInputd(boolean keyInputd) {
        this.keyInputd = keyInputd;
    }

    public boolean isKeyInputd() {
        return keyInputd;
    }

    public int getGameGoal() {
        return gameGoal;
    }
    public int getGoldOreGoal() {return goldOreGoal;}

    public long getGameTime() {
        return gameTime;
    }

    public void setGameTime(long gameTime) {
        this.gameTime = gameTime;
    }
    public static boolean isIsRandomBox() {
        return isRandomBox;
    }
    // 判断哪个点赞整蛊效果已达到要求可触发
    public HashMap<String, LikeData> getLikeDataMap() {
        return likeDataMap;
    }
    public boolean isGiftDataExit(String giftName){
        for (String gift : this.giftDataMap.keySet()){
            if(gift.equalsIgnoreCase(giftName)){
                return true;
            }
        }
        return false;
    }
    public GiftData getGiftData(String giftName){
        if(this.giftDataMap.get(giftName) == null){
            return null;
        }
        return this.giftDataMap.get(giftName);
    }
    public HashMap<String, GiftData> getGiftDataMap() {
        return giftDataMap;
    }
}
