package net.halbear.Terrain4J.EngineCore.Main.Scene;

import net.halbear.Terrain4J.EngineCore.Audio.AudioManager;
import net.halbear.Terrain4J.EngineCore.Audio.AudioSource;
import net.halbear.Terrain4J.EngineCore.Audio.SoundBuffer;
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.ModelData;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

import java.util.ArrayList;
import java.util.List;

public class JackOfAllTradesActor extends Actor {

    private ModelData Particle;
    private List<ParticleActor> particleActors = new ArrayList<>();
    private String SOUND_ID_BUFFER;
    private String SOUND_ID_PLAYER;
    private AudioSource playerAudioSource;

    public JackOfAllTradesActor(String ID, String ModelID, Vector3f Position, ModelData Particle){
        super(ID,ModelID,Position);
        SOUND_ID_BUFFER = ID + "_AUDIO_BUFFER";
        SOUND_ID_PLAYER = ID+"_AUDIO_PLAYER";
        this.Particle = Particle;
    }
    public JackOfAllTradesActor SetScale(float scale){
        super.SetScale(scale);
        return this;
    }
    public JackOfAllTradesActor SetAudio(EngineInstance engineInstance, String AudioFile){
        AudioManager audioManager = engineInstance.audioManager();
        SoundBuffer buffer = new SoundBuffer(AudioFile);
        audioManager.AddSoundBuffer(SOUND_ID_BUFFER, buffer);
        playerAudioSource = new AudioSource(true,false);
        playerAudioSource.SetPosition(new Vector3f(GetPosition()));
        playerAudioSource.SetBuffer(buffer.GetBufferID());
        audioManager.AddSoundSource(SOUND_ID_PLAYER,playerAudioSource);
        PlayAudio();
        return this;
    }
    public JackOfAllTradesActor PlayAudio(){
        playerAudioSource.Play();
        return this;
    }

    public void tick(Scene scene){
        List<Integer> ParticlesToDelete = new ArrayList<>();
        if((int)(Math.random() * 20) == 0){
            ParticleActor particle = new ParticleActor("particle" + particleActors.size(), Particle.ID(), new Vector3f(GetPosition()),new Vector3f(1,1,1),0,new Vector3f(-1f + (float)Math.random() * 2,-1f + (float)Math.random() * 2,-1f + (float)Math.random() * 2),0.0005f,5000);
            particleActors.add(particle);
            scene.AddActor(particle);
        }
        for(int i = 0; i < particleActors.size(); i++){
            particleActors.get(i).TickUpdate();
            if(particleActors.get(i).TicksLeft() <= 0){
                scene.RemoveActor(particleActors.get(i));
                ParticlesToDelete.add(i);
            }
        }
        for(int i = ParticlesToDelete.size() - 1; i >= 0; i--){
            particleActors.remove((int)ParticlesToDelete.get(i));
        }
    }

}
