#include "crDevGraphics.h"

#include <imgui.h>

#include "crApp.h"
#include "crGraphics.h"
#include "crGraphicsUniforms.h"
#include "crPostProcess.h"
#include "crShadowMap.h"

void crDevGraphics::Init( crApp* app )
{
    _app = app;
}
void crDevGraphics::Cleanup()
{
    _app = nullptr;
}

void crDevGraphics::TryRenderPanel()
{
    if( enabled == false )
        return;

    crGraphicsUniforms* uniforms = _app->graphics->Uniforms();

    ImGui::PushStyleVar( ImGuiStyleVar_FramePadding, ImVec2( 4.0f, 0.0f ) );
    ImGui::PushStyleVar( ImGuiStyleVar_ItemSpacing, ImVec2( 8.0f, 2.0f ) );

    ImGui::SetNextWindowPos( ImVec2( 10.0f, 70.0f ), ImGuiCond_FirstUseEver );
    ImGui::SetNextWindowSize( ImVec2( 480.0f, 0.0f ), ImGuiCond_FirstUseEver );
    ImGui::SetNextWindowCollapsed( true, ImGuiCond_FirstUseEver );

    if( ImGui::Begin( "Graphics", nullptr, ImGuiWindowFlags_NoSavedSettings ) )
    {
        ImGui::SeparatorText( "Render Scale" );
        crPostProcess* post        = _app->graphics->PostProcess();
        float          renderScale = post->RenderScale();
        bool           applyScale  = false;
        if( ImGui::RadioButton( "1.0", renderScale == 1.0f ) )
        {
            renderScale = 1.0f;
            applyScale  = true;
        }
        ImGui::SameLine();
        if( ImGui::RadioButton( "0.75", renderScale == 0.75f ) )
        {
            renderScale = 0.75f;
            applyScale  = true;
        }
        ImGui::SameLine();
        if( ImGui::RadioButton( "0.5", renderScale == 0.5f ) )
        {
            renderScale = 0.5f;
            applyScale  = true;
        }
        if( applyScale )
            post->SetRenderScale( renderScale );

        {
            const bool supported = _app->IsVSyncSupported();

            ImGui::BeginDisabled( supported == false );   // shown but inert where the platform ignores it
            bool vsync = _app->IsVSyncEnabled();
            if( ImGui::Checkbox( "vsync", &vsync ) )
                _app->SetVSync( vsync );
            ImGui::EndDisabled();

            if( supported == false )
            {
                ImGui::SameLine();
                ImGui::TextDisabled( "(browser paces the loop — use target fps)" );
            }
        }

        ImGui::SeparatorText( "Directional Light" );
        ImGui::DragFloat3( "Dir", &uniforms->lightDir.x, 0.01f, -1.0f, 1.0f, "%.2f" );
        ImGui::ColorEdit3( "Color##light", &uniforms->lightColor.r, ImGuiColorEditFlags_NoInputs );
        ImGui::SliderFloat( "Intensity", &uniforms->lightColor.a, 0.0f, 2.0f, "%.2f" );

        ImGui::SeparatorText( "Ambient (Hemisphere)" );
        ImGui::ColorEdit3( "Sky##ambient", &uniforms->ambientSky.r, ImGuiColorEditFlags_NoInputs );
        ImGui::SliderFloat( "Strength##sky", &uniforms->ambientSky.a, 0.0f, 1.0f, "%.2f" );
        ImGui::ColorEdit3( "Ground##ambient", &uniforms->ambientGround.r, ImGuiColorEditFlags_NoInputs );
        ImGui::SliderFloat( "Strength##ground", &uniforms->ambientGround.a, 0.0f, 1.0f, "%.2f" );

        ImGui::SeparatorText( "Specular" );
        ImGui::SliderFloat( "Shininess", &uniforms->specShininess, 1.0f, 128.0f, "%.0f" );
        ImGui::SliderFloat( "Strength##spec", &uniforms->specStrength, 0.0f, 1.0f, "%.2f" );

        ImGui::SeparatorText( "Shadow" );
        ImGui::SliderFloat( "Bias", &uniforms->shadowBias, 0.0f, 0.1f, "%.3f m" );
        ImGui::SliderFloat( "Strength##shadow", &uniforms->shadowStrength, 0.0f, 1.0f, "%.2f" );
        ImGui::SliderFloat( "Normal Offset", &uniforms->shadowNormalOffset, 0.0f, 8.0f, "%.1f texel" );
        ImGui::SliderFloat( "Darkness", &uniforms->shadowDarkness, 0.0f, 1.0f, "%.2f" );

        ImGui::SeparatorText( "Shadow Cascade" );   // CPU-side (crShadowMap), not UBO values
        crShadowMap* shadowMap = _app->graphics->ShadowMap();
        ImGui::SliderFloat( "Split 1", &shadowMap->splitDistances[ 0 ], 1.0f, 200.0f, "%.0f m" );
        ImGui::SliderFloat( "Split 2", &shadowMap->splitDistances[ 1 ], 1.0f, 200.0f, "%.0f m" );
        ImGui::SliderFloat( "Max Distance", &shadowMap->maxDistance, 10.0f, 200.0f, "%.0f m" );
    }
    ImGui::End();

    ImGui::PopStyleVar( 2 );
}
