#include "FloatGesture.hpp"

#include "../../../../render/Renderer.hpp"
#include "../../../../desktop/state/FocusState.hpp"
#include "../../../../desktop/view/Window.hpp"
#include "../../../../layout/LayoutManager.hpp"
#include "../../../../layout/target/WindowTarget.hpp"

constexpr const float MAX_DISTANCE = 250.F;

//
static Vector2D lerpVal(const Vector2D& from, const Vector2D& to, const float& t) {
    return Vector2D{
        from.x + ((to.x - from.x) * t),
        from.y + ((to.y - from.y) * t),
    };
}

CFloatTrackpadGesture::CFloatTrackpadGesture(const std::string_view& data) {
    std::string lc = std::string{data};
    std::ranges::transform(lc, lc.begin(), ::tolower);

    if (lc.starts_with("float"))
        m_mode = FLOAT_MODE_FLOAT;
    else if (lc.starts_with("tile"))
        m_mode = FLOAT_MODE_TILE;
    else
        m_mode = FLOAT_MODE_TOGGLE;
}

void CFloatTrackpadGesture::begin(const ITrackpadGesture::STrackpadGestureBegin& e) {
    ITrackpadGesture::begin(e);

    m_window = Desktop::focusState()->window();

    if (!m_window)
        return;

    if ((m_window->m_isFloating && m_mode == FLOAT_MODE_FLOAT) || (!m_window->m_isFloating && m_mode == FLOAT_MODE_TILE)) {
        m_window.reset();
        return;
    }

    g_layoutManager->changeFloatingMode(m_window->layoutTarget());

    m_posFrom  = m_window->positionAnimation()->begun();
    m_sizeFrom = m_window->sizeAnimation()->begun();

    m_posTo  = m_window->position(Desktop::View::IGeometric::GEOMETRIC_GOAL);
    m_sizeTo = m_window->size(Desktop::View::IGeometric::GEOMETRIC_GOAL);

    m_lastDelta = 0.F;
}

void CFloatTrackpadGesture::update(const ITrackpadGesture::STrackpadGestureUpdate& e) {
    if (!m_window)
        return;

    g_pHyprRenderer->damageWindow(m_window.lock());

    m_lastDelta += distance(e);

    const auto FADEPERCENT = std::clamp(m_lastDelta / MAX_DISTANCE, 0.F, 1.F);

    m_window->positionAnimation()->setValueAndWarp(lerpVal(m_posFrom, m_posTo, FADEPERCENT));
    m_window->sizeAnimation()->setValueAndWarp(lerpVal(m_sizeFrom, m_sizeTo, FADEPERCENT));

    g_pDecorationPositioner->onWindowUpdate(m_window.lock());

    g_pHyprRenderer->damageWindow(m_window.lock());
}

void CFloatTrackpadGesture::end(const ITrackpadGesture::STrackpadGestureEnd& e) {
    if (!m_window)
        return;

    const auto COMPLETION = std::clamp(m_lastDelta / MAX_DISTANCE, 0.F, 1.F);

    if (COMPLETION < 0.2F) {
        // revert the animation
        g_pHyprRenderer->damageWindow(m_window.lock());
        g_layoutManager->changeFloatingMode(m_window->layoutTarget());
        return;
    }

    m_window->move(m_posTo);
    m_window->resize(m_sizeTo);

    // the gesture warps m_realSize->goal() around during update(), which races the deferred
    // sendWindowSize() queued when the size animation began and can leave the client configured to
    // an intermediate size. force a configure to the final size so the client matches its box.
    m_window->sendWindowSize(true);
}
