#pragma once
#include <wx/print.h>
#include "PrintOptionsDialog.h"
#include "canvas/CanvasState.h"

// wxPrintout that renders the layout using the options chosen in
// PrintOptionsDialog. Pages are computed at OnPreparePrinting time based on
// the chosen scale (fit-to-page is always one page; actual-scale and custom
// percent may tile across multiple pages).
class LayoutPrintout : public wxPrintout {
public:
    LayoutPrintout(const wxString& title,
                   MC::CanvasState* state,
                   const PrintOptionsDialog::Options& opts);

    bool OnPrintPage(int page) override;
    bool HasPage(int page) override;
    void GetPageInfo(int* minPage, int* maxPage,
                     int* pageFrom, int* pageTo) override;
    void OnPreparePrinting() override;

private:
    // Compute layout extent (mm) — same approach as CanvasPanel::ZoomFit.
    void ComputeLayoutBounds(double& minX, double& minY,
                             double& maxX, double& maxY) const;

    // Resolve the effective scale (px-per-mm on the page DC) based on options
    // and the page logical size. Updates m_tilesX/m_tilesY when tiling.
    void ResolveScale(int pageW, int pageH);

    void DrawPageContent(wxDC& dc, int page,
                         int pageW, int pageH);

    MC::CanvasState*               m_state = nullptr;
    PrintOptionsDialog::Options    m_opts;

    // Resolved at OnPreparePrinting:
    double m_layMinX = 0, m_layMinY = 0;
    double m_layMaxX = 0, m_layMaxY = 0;
    double m_scale   = 1.0;   // page-px per layout-mm
    int    m_tilesX  = 1;
    int    m_tilesY  = 1;
    int    m_pageW   = 0;     // logical page width
    int    m_pageH   = 0;     // logical page height

    // Saved level visibility so we can restore after printing all-levels.
    std::vector<std::pair<int,bool>> m_savedLevelVis;
};
