package com.yaohun.demonskills.component;

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

public class SoundComponent implements SkillComponent {

    private final String sound;
    private final float pitch;
    private final float volume;
    private long stopTick = -1;

    public SoundComponent(Sound sound, float pitch, float volume) {
        this.sound = String.valueOf(sound);
        this.pitch = pitch;
        this.volume = volume;
    }

    public SoundComponent(String sound, float pitch, float volume) {
        this.sound = sound;
        this.pitch = pitch;
        this.volume = volume;
    }

    public SoundComponent(String sound, float pitch, float volume,long stopTick) {
        this.sound = sound;
        this.pitch = pitch;
        this.volume = volume;
        this.stopTick = stopTick;
    }

    @Override
    public void execute(Player caster) {
        if (sound.contains("_")) {
            caster.playSound(caster.getLocation(), Sound.valueOf(sound), pitch, volume);
        } else {
            PacketSender.sendPlaySound(caster,sound,pitch,volume,false,0,0,0);
            if(stopTick >= 5) {
                Bukkit.getScheduler().runTaskLater(SkillsMain.inst(), () -> {
                    PacketSender.sendStopSound(caster,sound);
                },stopTick);
            }
        }
    }
}
