package com.yaohun.demonskills.manage;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.config.Config;
import com.yaohun.demonskills.config.MessageUtil;
import com.yaohun.demonskills.core.Skill;
import com.yaohun.demonskills.data.PlayerData;
import com.yaohun.demonskills.data.SkillData;
import com.yaohun.demonskills.skills.*;
import com.yaohun.demonskills.skills.skillsapi.Skill_BaoHuoLongJi;
import com.yaohun.demonskills.skills.skillsapi.Skill_ZhongLiYaZhi;
import com.yaohun.demonskills.skills.skillsapi.Skill_左翻滚;
import com.yaohun.demonskills.skills.youling.*;
import com.yaohun.demonskills.util.CDTimeAPI;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.serverct.ersha.jd.AttributeAPI;

import java.util.*;

public class SkillManager {

    public static HashMap<String, SkillData> skillDataMap = new HashMap<>();

    public static void init() {
        FileConfiguration config = SkillsMain.inst().getConfig();
        ConfigurationSection section = config.getConfigurationSection("SkillStone");
        if(section == null) return;
        for (String key : section.getKeys(false)) {
            skillDataMap.put(key,new SkillData(key,section));
        }
        Bukkit.getConsoleSender().sendMessage("§d[技能系统] §7载入技能: §6"+skillDataMap.size()+"个");
    }

    public static String getRandomStone(){
        // 获取skillDataMap的键集
        Set<String> keys = skillDataMap.keySet();
        // 如果键集为空，返回null或者抛出异常（根据需求）
        if (keys.isEmpty()) return null;

        // 创建Random对象
        Random rand = new Random();
        // 随机选择一个键
        int index = rand.nextInt(keys.size());
        // 将Set转换为列表，获取随机索引的元素
        return (String) keys.toArray()[index];

    }

    public static boolean isSkillDataExit(String skillName){
        if(skillDataMap.containsKey(skillName)){
            return true;
        }
        return false;
    }

    public static void castSkill(Player caster, Skill skill) {
        UUID uuid = caster.getUniqueId();
        String cooldownKey = "skill_"+skill.name;
        String playerName = caster.getName();
        PlayerData playerData = SkillsMain.getPlayerManager().getPlayerData(playerName);
        // 首先判断冷却
        if(CDTimeAPI.isCD(uuid,cooldownKey)){
            double time = (double) CDTimeAPI.getCD(uuid, cooldownKey) / 1000L;
            String message = Config.langData.getMessage("castSkillCooldown");
            message = message.replace("{name}",skill.name);
            message = message.replace("{cooldown}",String.format("%.1f",time));
            MessageUtil.sendMessage(caster, message,Sound.ENTITY_VILLAGER_NO);
            return;
        }
        if(CDTimeAPI.isCD(uuid,"coreSkillCd")){return;}
        /*// 然后是魔法值消耗
        int nowMagic = playerData.getMagicNow();
        int needMagic = skill.needMagic;
        if(!playerData.hasMagic(needMagic)){
            SkillsMain.sendMessage(caster,"魔法值不足，还需要§e["+(needMagic - nowMagic)+"]§a才能触发该技能...", Sound.ENTITY_VILLAGER_NO);
            return;
        }*/
        // 扣除消耗并执行效果
        double coolingReduction = AttributeAPI.getAttrData(caster).getAttributeValue("冷却缩减:") / 100;
        if(coolingReduction >= 0.5){
            coolingReduction = 0.5;
        }
        CDTimeAPI.setPlayerCD(uuid,"coreSkillCd", (long) (skill.coreCooldown * 1000L));
        long newCoolDownTime = (long) (skill.cooldownTime * 1000L * (1.0 - coolingReduction));
        CDTimeAPI.setPlayerCD(uuid,cooldownKey,newCoolDownTime);
        // playerData.takeMagicNow(needMagic); // 扣除魔法值
        skill.cast(caster);
        String message = Config.langData.getMessage("castSkillMessage");
        message = message.replace("{name}",skill.name);
        message = message.replace("{player}",playerName);
        // 给玩家 caster 周围10格范围内所有玩家发送这条消息
        double radius = 10.0;
        Location center = caster.getLocation();
        for (Player nearbyPlayer : Bukkit.getOnlinePlayers()) {
            if (nearbyPlayer.getWorld().equals(center.getWorld()) &&
                    nearbyPlayer.getLocation().distance(center) <= radius) {
                nearbyPlayer.sendMessage(message);
            }
        }
    }

    public static Skill getNameConvertSkill(Player player,String skillName) {
        if(skillName.equalsIgnoreCase("狱炎怒吼")){
            return Skill_狱炎怒吼.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("爆火龙疾")){
            return Skill_BaoHuoLongJi.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("重力压制")){
            return Skill_ZhongLiYaZhi.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("赤焰旋风斩")){
            return Skill_赤焰旋风斩.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("斩空陨击")){
            return Skill_斩空陨击.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("崩地裂刺")){
            return Skill_崩地裂刺.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("牵魂法阵")){
            return Skill_牵魂法阵.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("逐风三式")){
            return Skill_逐风三式.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("破空闪击")){
            return Skill_破空闪击.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("苍锋剑气")){
            return Skill_苍锋剑气.getSkillData(player);
        } else if(skillName.equalsIgnoreCase("剑舞八荒")){
            return Skill_剑舞八荒.getSkillData(player);
        } else if (skillName.equalsIgnoreCase("逐影破空斩")) {
            return Skill_逐影破空斩.getSkillData(player);
        }else if (skillName.equalsIgnoreCase("幻刃屠龙舞")) {
            return Skill_幻刃屠龙舞.getSkillData(player);
        }
        return null;
    }

    public static Skill getGuiSkillStack(String skillName,int skillLevel) {
        if (skillName.equalsIgnoreCase("狱炎怒吼")) {
            return Skill_狱炎怒吼.getSkillItemStack(skillLevel);
        } else if (skillName.equalsIgnoreCase("爆火龙疾")) {
            return Skill_BaoHuoLongJi.getSkillItemStack(skillLevel);
        } else if (skillName.equalsIgnoreCase("重力压制")) {
            return Skill_ZhongLiYaZhi.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("赤焰旋风斩")) {
            return Skill_赤焰旋风斩.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("斩空陨击")) {
            return Skill_斩空陨击.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("崩地裂刺")) {
            return Skill_崩地裂刺.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("牵魂法阵")) {
            return Skill_牵魂法阵.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("逐风三式")) {
            return Skill_逐风三式.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("破空闪击")) {
            return Skill_破空闪击.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("苍锋剑气")) {
            return Skill_苍锋剑气.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("剑舞八荒")) {
            return Skill_剑舞八荒.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("逐影破空斩")) {
            return Skill_逐影破空斩.getSkillItemStack(skillLevel);
        }else if (skillName.equalsIgnoreCase("幻刃屠龙舞")) {
            return Skill_幻刃屠龙舞.getSkillItemStack(skillLevel);
        }
        return null;
    }
}
