package com.yaohun.demonskills.component.dragoncore;

import com.yaohun.demonskills.SkillsMain;
import com.yaohun.demonskills.api.SkillComponent;
import eos.moe.dragoncore.DragonCore;
import eos.moe.dragoncore.api.CoreAPI;
import eos.moe.dragoncore.network.PacketSender;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Random;
import java.util.UUID;

public class StandAnimationComponent implements SkillComponent {

    private String entityName;
    private String animationKey;
    private long delay;
    private long deleteTime;
    private double add_y = 1.1;

    public StandAnimationComponent(String entityName,String animationKey, long delay, long deleteTime) {
        this.entityName = entityName;
        this.animationKey = animationKey;
        this.delay = delay;
        this.deleteTime = deleteTime;
    }

    public StandAnimationComponent(String entityName,String animationKey, long delay, long deleteTime,double add_y) {
        this.entityName = entityName;
        this.animationKey = animationKey;
        this.delay = delay;
        this.deleteTime = deleteTime;
        this.add_y = add_y;
    }

    @Override
    public void execute(Player caster) {
        Location location = caster.getLocation().clone().add(0,add_y,0);
        // 生成隐身的盔甲架
        ArmorStand armorStand = location.getWorld().spawn(location, ArmorStand.class, stand -> {
            stand.setCustomName(entityName);
            stand.setGravity(false); // 取消重力，不会下落
            stand.setAI(false); // 没有 AI（其实盔甲架本身就没 AI，写了也没影响）
            stand.setMarker(true); // 不占空间、不可推挤，推荐设置
            stand.setCollidable(false); // 禁止与实体碰撞
            stand.setInvulnerable(true); // 不被误伤
        });
        new BukkitRunnable() {
            public void run() {
                PacketSender.setModelEntityAnimation(armorStand,animationKey,0);
            }
        }.runTaskLater(SkillsMain.inst(), delay);
        Bukkit.getScheduler().runTaskLater(SkillsMain.inst(), armorStand::remove, deleteTime);
        new BukkitRunnable() {
            public void run() {
                if(armorStand == null || armorStand.isDead()) {
                    cancel();
                    return;
                }
                armorStand.teleport(caster.getLocation().clone().add(0,add_y,0));
            }
        }.runTaskTimer(SkillsMain.inst(), 2L, 2L);

    }
}
