package electrosphere.menu.debug;

import electrosphere.audio.AudioBuffer;
import electrosphere.audio.VirtualAudioSource;
import electrosphere.engine.Globals;
import electrosphere.renderer.ui.imgui.ImGuiWindow;
import electrosphere.renderer.ui.imgui.ImGuiWindow.ImGuiWindowCallback;
import imgui.ImGui;
import imgui.type.ImString;

/**
 * Audio debug menus
 */
public class ImGuiAudio {

    //audio debug menu
    public static ImGuiWindow audioDebugMenu;

    //the audio buffer list filter
    private static ImString bufferListFilter = new ImString();

    /**
     * Create audio debug menu
     */
    public static void createAudioDebugMenu(){
        audioDebugMenu = new ImGuiWindow("Audio");
        audioDebugMenu.setCallback(new ImGuiWindowCallback() {
            @Override
            public void exec() {
                //audio engine details
                ImGui.text("Audio Engine Details");
                ImGui.text("Virtual Audio Source Manager Details");
                ImGui.text("Total number active virtual sources: " + Globals.virtualAudioSourceManager.getSourceQueue().size());
                if(ImGui.collapsingHeader("Engine Configuration")){
                    ImGui.text("Current audio device: " + Globals.audioEngine.getDevice());
                    ImGui.text("Default audio device: " + Globals.audioEngine.getDefaultDevice());
                    ImGui.text("Has HRTF: " + Globals.audioEngine.getHRTFStatus());
                    ImGui.text("Listener location: " + Globals.audioEngine.getListener().getPosition());
                    ImGui.text("Listener eye vector: " + Globals.audioEngine.getListener().getEyeVector());
                    ImGui.text("Listener up vector: " + Globals.audioEngine.getListener().getUpVector());
                }
                //only active children
                if(ImGui.collapsingHeader("Mapped Virtual Sources")){
                    for(VirtualAudioSource source : Globals.virtualAudioSourceManager.getMappedSources()){
                        ImGui.text("Source " + source.getPriority());
                        ImGui.text(" - Position " + source.getPosition());
                        ImGui.text(" - Gain " + source.getGain());
                        ImGui.text(" - Type " + source.getType());
                        ImGui.text(" - Total time played " + source.getTotalTimePlayed());
                        ImGui.text(" - Buffer Lenth " + source.getBufferLength());
                    }
                }
                //all virtual children
                if(ImGui.collapsingHeader("All Virtual Sources")){
                    for(VirtualAudioSource source : Globals.virtualAudioSourceManager.getSourceQueue()){
                        ImGui.text("Position " + source.getPosition());
                    }
                }
                //all buffers loaded into memory and stats about them
                if(ImGui.collapsingHeader("All Loaded Audio Buffers")){
                    if(ImGui.inputText("Filter", bufferListFilter)){
                        
                    }
                    for(AudioBuffer buffer : Globals.assetManager.getAllAudio()){
                        if(buffer.getFilePath().contains(bufferListFilter.get()) && ImGui.collapsingHeader(buffer.getFilePath())){
                            ImGui.text("Length: " + buffer.getLength());
                        }
                    }
                }
            }
        });
        audioDebugMenu.setOpen(false);
        Globals.renderingEngine.getImGuiPipeline().addImGuiWindow(audioDebugMenu);
    }
    
}
