// curve_popup.cpp — see curve_popup.h. Pure arithmetic; no LICE/VST3/REAPER includes.

#include "core/instrument/ui/curve_popup.h"

#include <algorithm>

namespace reasampler::instrument::ui {

namespace {
int clampDim(int want, int lo, int hi, int windowDim) {
    const int clamped = (std::max)(lo, (std::min)(hi, want));
    return (std::min)(clamped, (std::max)(0, windowDim));
}
}  // namespace

CurvePopupLayout computeCurvePopup(int w, int h) {
    CurvePopupLayout out;
    const int sheetW = clampDim((w * 60) / 100, kCurvePopupMinW, kCurvePopupMaxW, w);
    const int sheetH = clampDim((h * 55) / 100, kCurvePopupMinH, kCurvePopupMaxH, h);
    const int left = (w - sheetW) / 2;
    const int top = (h - sheetH) / 2;
    out.sheet = Rect::ltrb(left, top, left + sheetW, top + sheetH);

    const int titleBottom = out.sheet.y + kCurvePopupTitleH;
    const int closeTop = out.sheet.y + (kCurvePopupTitleH - kCurvePopupCloseSize) / 2;
    out.close = Rect::ltrb(out.sheet.right() - kCurvePopupPad - kCurvePopupCloseSize, closeTop,
                     out.sheet.right() - kCurvePopupPad, closeTop + kCurvePopupCloseSize);
    out.title = Rect::ltrb(out.sheet.x + kCurvePopupPad, out.sheet.y,
                     out.close.x - kCurvePopupPad, titleBottom);

    out.curveBox = Rect::ltrb(out.sheet.x + kCurvePopupPad, titleBottom + 2,
                        out.sheet.right() - kCurvePopupPad,
                        out.sheet.bottom() - kCurvePopupPad);
    return out;
}

bool popupOutsideSheet(const CurvePopupLayout& layout, int x, int y) {
    return !contains(layout.sheet, x, y);
}

}  // namespace reasampler::instrument::ui
