#include "CHyprDropShadowDecoration.hpp"

#include <algorithm>
#include "../../Compositor.hpp"
#include "../../config/ConfigValue.hpp"
#include "../pass/ShadowPassElement.hpp"
#include "../Renderer.hpp"
#include "../pass/RectPassElement.hpp"
#include "../pass/TextureMatteElement.hpp"
#include "../../state/MonitorState.hpp"

CHyprDropShadowDecoration::CHyprDropShadowDecoration(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow), m_window(pWindow) {
    ;
}

eDecorationType CHyprDropShadowDecoration::getDecorationType() {
    return DECORATION_SHADOW;
}

SDecorationPositioningInfo CHyprDropShadowDecoration::getPositioningInfo() {
    SDecorationPositioningInfo info;
    info.policy         = DECORATION_POSITION_ABSOLUTE;
    info.desiredExtents = m_extents;
    info.edges          = DECORATION_EDGE_BOTTOM | DECORATION_EDGE_LEFT | DECORATION_EDGE_RIGHT | DECORATION_EDGE_TOP;

    m_reportedExtents = m_extents;
    return info;
}

void CHyprDropShadowDecoration::onPositioningReply(const SDecorationPositioningReply& reply) {
    updateWindow(m_window.lock());
}

uint64_t CHyprDropShadowDecoration::getDecorationFlags() {
    return DECORATION_NON_SOLID;
}

std::string CHyprDropShadowDecoration::getDisplayName() {
    return "Drop Shadow";
}

void CHyprDropShadowDecoration::damageEntire() {
    static auto PSHADOWS = CConfigValue<Config::INTEGER>("decoration:shadow:enabled");

    if (*PSHADOWS != 1)
        return; // disabled

    const auto PWINDOW = m_window.lock();
    const auto pos     = PWINDOW->position(Desktop::View::IGeometric::GEOMETRIC_CURRENT);
    const auto size    = PWINDOW->size(Desktop::View::IGeometric::GEOMETRIC_CURRENT);

    CBox       shadowBox = {pos.x - m_extents.topLeft.x, pos.y - m_extents.topLeft.y, pos.x + size.x + m_extents.bottomRight.x, pos.y + size.y + m_extents.bottomRight.y};

    const auto PWORKSPACE  = PWINDOW->m_workspace;
    const auto applyOffset = [&](CBox& b) {
        if (PWORKSPACE && PWORKSPACE->m_renderOffset->isBeingAnimated() && !PWINDOW->m_pinned)
            b.translate(PWORKSPACE->m_renderOffset->value());
        b.translate(PWINDOW->m_floatingOffset);
    };

    applyOffset(shadowBox);

    CRegion shadowRegion(shadowBox);

    for (auto const& m : State::monitorState()->monitors()) {
        if (!g_pHyprRenderer->shouldRenderWindow(PWINDOW, m)) {
            const CRegion monitorRegion({m->m_position, m->m_size});
            shadowRegion.subtract(monitorRegion);
        }
    }

    g_pHyprRenderer->damageRegion(shadowRegion);
}

void CHyprDropShadowDecoration::updateWindow(PHLWINDOW pWindow) {
    const auto PWINDOW = m_window.lock();

    m_lastWindowPos  = PWINDOW->position(Desktop::View::IGeometric::GEOMETRIC_CURRENT);
    m_lastWindowSize = PWINDOW->size(Desktop::View::IGeometric::GEOMETRIC_CURRENT);

    m_lastWindowBox          = {m_lastWindowPos.x, m_lastWindowPos.y, m_lastWindowSize.x, m_lastWindowSize.y};
    m_lastWindowBoxWithDecos = g_pDecorationPositioner->getBoxWithIncludedDecos(pWindow);
}

void CHyprDropShadowDecoration::draw(PHLMONITOR pMonitor, float const& a) {
    CShadowPassElement::SShadowData data;
    data.deco = this;
    data.a    = a;
    g_pHyprRenderer->addPassElement(makeUnique<CShadowPassElement>(data));
}

bool CHyprDropShadowDecoration::canRender(PHLMONITOR pMonitor) {
    static auto PSHADOWS = CConfigValue<Config::INTEGER>("decoration:shadow:enabled");
    if (*PSHADOWS != 1)
        return false; // disabled

    const auto PWINDOW = m_window.lock();

    if (!validMapped(PWINDOW))
        return false;

    {
        static constexpr auto HAS_ALPHA = [](const auto& c) { return c.a > 0.001f; };
        if (std::none_of(PWINDOW->m_realShadowColor.m_colors.begin(), PWINDOW->m_realShadowColor.m_colors.end(), HAS_ALPHA)) {
            if (!PWINDOW->m_shadowFadeAnimationProgress->isBeingAnimated())
                return false;
            if (std::none_of(PWINDOW->m_realShadowColorPrevious.m_colors.begin(), PWINDOW->m_realShadowColorPrevious.m_colors.end(), HAS_ALPHA))
                return false;
        }
    }

    if (!PWINDOW->m_ruleApplicator->decorate().valueOrDefault())
        return false;

    if (PWINDOW->m_ruleApplicator->noShadow().valueOrDefault())
        return false;

    return true;
}

SShadowRenderData CHyprDropShadowDecoration::getRenderData(PHLMONITOR pMonitor, float const& a) {
    if (!canRender(pMonitor))
        return {};

    const auto  PWINDOW = m_window.lock();

    static auto PSHADOWSIZE   = CConfigValue<Config::INTEGER>("decoration:shadow:range");
    static auto PSHADOWSCALE  = CConfigValue<Config::FLOAT>("decoration:shadow:scale");
    static auto PSHADOWOFFSET = CConfigValue<Config::VEC2>("decoration:shadow:offset");

    const auto  BORDERSIZE       = PWINDOW->getRealBorderSize();
    const auto  ROUNDINGBASE     = PWINDOW->rounding();
    const auto  ROUNDINGPOWER    = PWINDOW->roundingPower();
    const auto  CORRECTIONOFFSET = (BORDERSIZE * (M_SQRT2 - 1) * std::max(2.0 - ROUNDINGPOWER, 0.0));
    const auto  ROUNDING         = ROUNDINGBASE > 0 ? (ROUNDINGBASE + BORDERSIZE) - CORRECTIONOFFSET : 0;
    const auto  PWORKSPACE       = PWINDOW->m_workspace;
    const auto  WORKSPACEOFFSET  = PWORKSPACE && !PWINDOW->m_pinned ? PWORKSPACE->m_renderOffset->value() : Vector2D();

    // draw the shadow
    CBox fullBox = m_lastWindowBoxWithDecos;
    fullBox.translate(-pMonitor->m_position + WORKSPACEOFFSET);
    fullBox.x -= *PSHADOWSIZE;
    fullBox.y -= *PSHADOWSIZE;
    fullBox.w += 2 * *PSHADOWSIZE;
    fullBox.h += 2 * *PSHADOWSIZE;

    const float SHADOWSCALE = std::clamp(*PSHADOWSCALE, 0.f, 1.f);

    // scale the box in relation to the center of the box
    fullBox.scaleFromCenter(SHADOWSCALE).translate({(*PSHADOWOFFSET).x, (*PSHADOWOFFSET).y});

    updateWindow(PWINDOW);
    m_lastWindowPos += WORKSPACEOFFSET;
    m_extents = {
        .topLeft =
            {
                m_lastWindowPos.x - fullBox.x - pMonitor->m_position.x + 2,
                m_lastWindowPos.y - fullBox.y - pMonitor->m_position.y + 2,
            },
        .bottomRight =
            {
                fullBox.x + fullBox.width + pMonitor->m_position.x - m_lastWindowPos.x - m_lastWindowSize.x + 2,
                fullBox.y + fullBox.height + pMonitor->m_position.y - m_lastWindowPos.y - m_lastWindowSize.y + 2,
            },
    };

    fullBox.translate(PWINDOW->m_floatingOffset);

    if (fullBox.width < 1 || fullBox.height < 1)
        return {}; // don't draw invisible shadows

    g_pHyprRenderer->m_renderData.currentWindow = m_window;

    fullBox.scale(pMonitor->m_scale).round();

    return {
        .valid         = true,
        .fullBox       = fullBox,
        .rounding      = ROUNDING,
        .roundingPower = ROUNDINGPOWER,
        .size          = *PSHADOWSIZE,
    };
}

void CHyprDropShadowDecoration::reposition() {
    if (m_extents != m_reportedExtents)
        g_pDecorationPositioner->repositionDeco(this);

    g_pHyprRenderer->m_renderData.currentWindow.reset();
}

// TODO remove
void CHyprDropShadowDecoration::render(PHLMONITOR pMonitor, float const& a) {
    auto data = getRenderData(pMonitor, a);
    if (!data.valid)
        return;

    const auto PWINDOW = m_window.lock();

    g_pHyprRenderer->disableScissor();

    auto       grad     = PWINDOW->m_realShadowColor;
    const bool ANIMATED = PWINDOW->m_shadowFadeAnimationProgress->isBeingAnimated();

    if (PWINDOW->m_shadowAngleAnimationProgress->enabled()) {
        grad.m_angle += PWINDOW->m_shadowAngleAnimationProgress->value() * M_PI * 2;
        grad.m_angle = normalizeAngleRad(grad.m_angle);

        if (ANIMATED)
            PWINDOW->m_realShadowColorPrevious.m_angle = grad.m_angle;
    }

    if (ANIMATED)
        drawShadowInternal(data.fullBox, data.rounding * pMonitor->m_scale, data.roundingPower, data.size * pMonitor->m_scale, PWINDOW->m_realShadowColorPrevious, grad,
                           PWINDOW->m_shadowFadeAnimationProgress->value(), a);
    else
        drawShadowInternal(data.fullBox, data.rounding * pMonitor->m_scale, data.roundingPower, data.size * pMonitor->m_scale, grad, a);

    reposition();
}

eDecorationLayer CHyprDropShadowDecoration::getDecorationLayer() {
    return DECORATION_LAYER_BOTTOM;
}

void CHyprDropShadowDecoration::drawShadowInternal(const CBox& box, int round, float roundingPower, int range, const Config::CGradientValueData& grad, float a) {
    static auto PSHADOWSHARP = CConfigValue<Config::INTEGER>("decoration:shadow:sharp");

    if (box.w < 1 || box.h < 1)
        return;

    g_pHyprRenderer->blend(true);

    if (*PSHADOWSHARP) {
        CHyprColor flatColor = grad.m_colors.empty() ? CHyprColor(0, 0, 0, 0) : grad.m_colors[0];
        flatColor.a *= a;
        g_pHyprRenderer->draw(
            CRectPassElement::SRectData{
                .box           = box,
                .color         = flatColor,
                .round         = round,
                .roundingPower = roundingPower,
            },
            box);
    } else
        g_pHyprRenderer->drawShadow(box, round, roundingPower, range, grad, a);
}

void CHyprDropShadowDecoration::drawShadowInternal(const CBox& box, int round, float roundingPower, int range, const Config::CGradientValueData& grad1,
                                                   const Config::CGradientValueData& grad2, float lerp, float a) {
    static auto PSHADOWSHARP = CConfigValue<Config::INTEGER>("decoration:shadow:sharp");

    if (box.w < 1 || box.h < 1)
        return;

    g_pHyprRenderer->blend(true);

    if (*PSHADOWSHARP) {
        CHyprColor col1 = grad1.m_colors.empty() ? CHyprColor(0, 0, 0, 0) : grad1.m_colors[0];
        CHyprColor col2 = grad2.m_colors.empty() ? col1 : grad2.m_colors[0];
        CHyprColor flatColor =
            CHyprColor(col1.r + (col2.r - col1.r) * lerp, col1.g + (col2.g - col1.g) * lerp, col1.b + (col2.b - col1.b) * lerp, col1.a + (col2.a - col1.a) * lerp);
        flatColor.a *= a;
        g_pHyprRenderer->draw(
            CRectPassElement::SRectData{
                .box           = box,
                .color         = flatColor,
                .round         = round,
                .roundingPower = roundingPower,
            },
            box);
    } else
        g_pHyprRenderer->drawShadow(box, round, roundingPower, range, grad1, grad2, lerp, a);
}
