// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#pragma once

// Edge-fade scroll region for the blur-backdrop modals (smooth scroll + Settings-tab edge fade +
// a thicker, inset, rounded scrollbar). Split out of draw_helpers.h because it depends on
// effects::ScrollFadeShader, whose GL/DX headers must not be pulled into the widely-included base
// header (they clash with SDL's GL headers in some TUs). Include this only from modal .cpp files.

#include "draw_helpers.h"                    // IsCapturingBlurBackdrop, ApplySmoothScroll
#include "../effects/scroll_fade_shader.h"
#include "../effects/low_spec.h"
#include "imgui.h"
#include <algorithm>
#include <string>

namespace dragonx {
namespace ui {
namespace material {

// Scroll region with smooth scrolling + edge fade + a thicker, inset, rounded scrollbar. Uses a
// bordered/rounded OUTER frame whose padding insets the scrollbar, holding an INNER scrolling child.
// `fade` must persist across frames (one instance per scroll region); `pad` is the inner content
// inset. Returns the inner draw list; pair with EndFadeScrollChild().
inline ImDrawList* BeginFadeScrollChild(const char* id, effects::ScrollFadeShader& fade,
                                        const ImVec2& pad, float dp)
{
    ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 6.0f * dp);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(6.0f * dp, 6.0f * dp));
    ImGui::BeginChild(id, ImVec2(0, std::max(120.0f * dp, ImGui::GetContentRegionAvail().y)), true,
                      ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
    ImGui::PopStyleVar(2);  // ChildRounding + outer WindowPadding (captured by the outer child)
    ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarSize, 11.0f * dp);
    ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarRounding, 5.5f * dp);
    ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, pad);
    std::string innerId = std::string(id) + "_i";
    ImGui::BeginChild(innerId.c_str(), ImVec2(0, 0), false, ImGuiWindowFlags_NoScrollWithMouse);
    ImGui::PopStyleVar();   // inner WindowPadding — captured by this child; don't cascade to tooltips
    ApplySmoothScroll();
    ImDrawList* dl = ImGui::GetWindowDrawList();
    const float fadeH = 22.0f * dp;
    if (!effects::isLowSpecMode() && !IsCapturingBlurBackdrop() && fade.init()) {
        ImVec2 cMin = ImGui::GetWindowPos();
        ImVec2 cMax(cMin.x + ImGui::GetWindowSize().x, cMin.y + ImGui::GetWindowSize().y);
        float sY = ImGui::GetScrollY(), sMax = ImGui::GetScrollMaxY();
        fade.fadeTopY       = cMin.y;
        fade.fadeBottomY    = cMax.y;
        fade.fadeZoneTop    = (sY > 1.0f) ? fadeH : 0.0f;
        fade.fadeZoneBottom = (sMax > 0.0f && sY < sMax - 1.0f) ? fadeH : 0.0f;
        fade.addBind(dl);
    }
    return dl;
}

inline void EndFadeScrollChild(effects::ScrollFadeShader& fade, ImDrawList* dl)
{
    if (!effects::isLowSpecMode() && !IsCapturingBlurBackdrop() && fade.ready)
        effects::ScrollFadeShader::addUnbind(dl);
    ImGui::EndChild();       // inner
    ImGui::PopStyleVar(2);   // ScrollbarSize + ScrollbarRounding
    ImGui::EndChild();       // outer
}

} // namespace material
} // namespace ui
} // namespace dragonx
