#include "DamageRing.hpp"

#include <hyprutils/memory/Casts.hpp>

using namespace Monitor;
using namespace Hyprutils::Memory;

void CDamageRing::setSize(const Vector2D& size_) {
    if (size_ == m_size)
        return;

    m_size = size_;

    damageEntire();
}

bool CDamageRing::damage(const CBox& box) {
    if (m_size.x <= 0 || m_size.y <= 0 || box.w <= 0 || box.h <= 0)
        return false;

    return damage(CRegion{box});
}

bool CDamageRing::damage(const CRegion& rg) {
    if (m_size.x <= 0 || m_size.y <= 0)
        return false;

    CRegion clipped = rg.copy().intersect(CBox{{}, m_size});
    if (clipped.empty())
        return false;

    m_current.add(clipped);
    return true;
}

void CDamageRing::damageEntire() {
    damage(CBox{{}, m_size});
}

void CDamageRing::rotate() {
    m_previousIdx = (m_previousIdx + DAMAGE_RING_PREVIOUS_LEN - 1) % DAMAGE_RING_PREVIOUS_LEN;

    m_previous[m_previousIdx] = m_current;
    m_current.clear();
}

CRegion CDamageRing::getBufferDamage(int age) {
    if (m_size.x <= 0 || m_size.y <= 0)
        return {};

    if (age <= 0 || age > DAMAGE_RING_PREVIOUS_LEN + 1)
        return CBox{{}, m_size};

    CRegion damage = m_current;

    for (int i = 0; i < age - 1; ++i) {
        int j = (m_previousIdx + i) % DAMAGE_RING_PREVIOUS_LEN;
        damage.add(m_previous.at(j));
    }

    // don't return a ludicrous amount of rects
    if (pixman_region32_n_rects(damage.pixman()) > DAMAGE_RING_MAX_RECTS) {
        const auto EXTENTS = damage.getExtents();

        // but only when the bounding box is not much bigger than the rects it replaces.
        // scattered damage would otherwise shade most of the screen to save a few draws.
        double rectsArea = 0;
        damage.forEachRect([&rectsArea](const auto& RECT) { rectsArea += sc<double>(RECT.x2 - RECT.x1) * (RECT.y2 - RECT.y1); });

        if (EXTENTS.w * EXTENTS.h <= rectsArea * DAMAGE_RING_EXTENTS_FACTOR)
            return EXTENTS;
    }

    return damage;
}

bool CDamageRing::hasChanged() {
    return !m_current.empty();
}
