package com.yaohun.demonskills.skills.skillsapi;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.component.*;
import com.yaohun.demonskills.component.damage.DamageComponent;
import com.yaohun.demonskills.component.damage.DamageContinuedComponent;
import com.yaohun.demonskills.component.particles.CircleParticlesComponent;
import com.yaohun.demonskills.component.particles.ParticlesComponent;
import com.yaohun.demonskills.component.projectile.ProjectileVectorComponent;
import com.yaohun.demonskills.core.Skill;
import com.yaohun.demonskills.data.PlayerData;
import com.yaohun.demonskills.manage.PlayerManager;
import com.yaohun.demonskills.target.NearestEnemiesSelector;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.Vector;
import org.serverct.ersha.jd.AttributeAPI;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Skill_ZhongLiYaZhi {

    private static final String skillName = "重力压制";
    private static final double cooldownTime = 65;
    private static final double cdReducePerLevel = 3.0;     // 每级减少1.2秒冷却
    private static final double minCooldown = 30.0;         // 最低冷却限制

    private static final Map<Integer, Integer> skillMagicMap = new HashMap<>();
    static {
        skillMagicMap.put(1, 1);
        skillMagicMap.put(2, 1);
        skillMagicMap.put(3, 2);
        skillMagicMap.put(4, 2);
        skillMagicMap.put(5, 3);
        skillMagicMap.put(6, 3);
        skillMagicMap.put(7, 4);
        skillMagicMap.put(8, 4);
    }

    public static Skill getSkillItemStack(int skillLevel){
        double realCooldown = Math.max(cooldownTime - cdReducePerLevel * (skillLevel - 1), minCooldown);
        int realNeedMagic = skillMagicMap.getOrDefault(skillLevel, 0);
        return new Skill(skillName, realNeedMagic, realCooldown, 2.0,new ArrayList<>());
    }

    public static Skill getSkillData(Player player) {
        String playerName = player.getName();
        PlayerManager playerManager = SkillsMain.getPlayerManager();
        PlayerData playerData = playerManager.getPlayerData(playerName);
        int skillLevel = playerData.getSkillLevel(skillName);
        double realCooldown = Math.max(cooldownTime - cdReducePerLevel * (skillLevel - 1), minCooldown);
        int realNeedMagic = skillMagicMap.getOrDefault(skillLevel, 0);
        double damageValue = AttributeAPI.getAttrData(player).getAttributeRandomValue("物理伤害");
        double damage = 100 + (damageValue * 1.3) * skillLevel;
        Skill skill = new Skill( // 技能 ID
                skillName,   // 技能名称
                realNeedMagic,      // 魔力需求
                realCooldown,      // 冷却时间
                2.0,
                Arrays.asList(
                        // 玩家向前冲刺 力度0.3  高度0.8
                        new ProjectileVectorComponent(new Vector(0,0.5,0),0,0.5,0),
                        // 播放声音
                        new SoundComponent("ENTITY_BAT_TAKEOFF",1.0f,1.0f),
                        // 播放  跟随粒子每次出现6个 范围2  播放次数 15次
                        new ParticlesComponent(Particle.CRIT,6,2,30),
                        // 延迟 16L
                        new DelayComponent(12L,Arrays.asList(
                                // 添加防摔CD  // 播放爆炸音效
                                new CdTimeComponent("anti_fall",500),
                                new SoundComponent("ENTITY_GENERIC_EXPLODE",1.0f,1.2f),
                                new ParticlesComponent(Particle.LAVA,8,1,2),
                                // 生成脚下一圈粒子
                                new CircleParticlesComponent(Particle.SPELL_WITCH,2,2,36,3),
                                new CircleParticlesComponent(Particle.SPELL_WITCH,2,3,36,3),
                                new CircleParticlesComponent(Particle.SPELL_WITCH,2,4,36,3),
                                // 对周围5格范围内生物造成减速2秒
                                new DamageContinuedComponent(damage,new NearestEnemiesSelector(5),5,4,5),
                                new PotionEffectComponent(PotionEffectType.SLOW,20,4,new NearestEnemiesSelector(5)),
                                new ParticlesComponent(Particle.PORTAL,6,2.0,60,new NearestEnemiesSelector(5))
                        ))
                )
        );
        playerData.setCooldownData(skillName, realCooldown);
        return skill;
    }
}
