// The MIT License (MIT)
//
// Copyright (C) 2026 Scott Duensing
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// canvas.h  -- Canvas widget API
#ifndef CANVAS_H
#define CANVAS_H

#include "../../../libs/kpunch/libdvx/dvxWgt.h"

typedef struct {
    WidgetT  *(*create)(WidgetT *parent, int32_t w, int32_t h);
    void      (*clear)(WidgetT *w, uint32_t color);
    void      (*setPenColor)(WidgetT *w, uint32_t color);
    void      (*setPenSize)(WidgetT *w, int32_t size);
    void      (*setMouseCallback)(WidgetT *w, void (*cb)(WidgetT *w, int32_t cx, int32_t cy, bool drag));
    void      (*save)(WidgetT *w, const char *path);
    void      (*load)(WidgetT *w, const char *path);
    void      (*drawLine)(WidgetT *w, int32_t x0, int32_t y0, int32_t x1, int32_t y1);
    void      (*drawRect)(WidgetT *w, int32_t x, int32_t y, int32_t width, int32_t height);
    void      (*fillRect)(WidgetT *w, int32_t x, int32_t y, int32_t width, int32_t height);
    void      (*fillCircle)(WidgetT *w, int32_t cx, int32_t cy, int32_t radius);
    void      (*setPixel)(WidgetT *w, int32_t x, int32_t y, uint32_t color);
    uint32_t  (*getPixel)(const WidgetT *w, int32_t x, int32_t y);
    void      (*drawText)(WidgetT *w, int32_t x, int32_t y, const char *text);
} CanvasApiT;

static inline const CanvasApiT *dvxCanvasApi(void) {
    static const CanvasApiT *sApi;
    if (!sApi) { sApi = (const CanvasApiT *)wgtGetApi("canvas"); }
    return sApi;
}

#define wgtCanvas(parent, w, h)                         dvxCanvasApi()->create(parent, w, h)
#define wgtCanvasClear(w, color)                        dvxCanvasApi()->clear(w, color)
#define wgtCanvasSetPenColor(w, color)                  dvxCanvasApi()->setPenColor(w, color)
#define wgtCanvasSetPenSize(w, size)                    dvxCanvasApi()->setPenSize(w, size)
#define wgtCanvasSetMouseCallback(w, cb)                dvxCanvasApi()->setMouseCallback(w, cb)
#define wgtCanvasSave(w, path)                          dvxCanvasApi()->save(w, path)
#define wgtCanvasLoad(w, path)                          dvxCanvasApi()->load(w, path)
#define wgtCanvasDrawLine(w, x0, y0, x1, y1)            dvxCanvasApi()->drawLine(w, x0, y0, x1, y1)
#define wgtCanvasDrawRect(w, x, y, width, height)       dvxCanvasApi()->drawRect(w, x, y, width, height)
#define wgtCanvasFillRect(w, x, y, width, height)       dvxCanvasApi()->fillRect(w, x, y, width, height)
#define wgtCanvasFillCircle(w, cx, cy, radius)          dvxCanvasApi()->fillCircle(w, cx, cy, radius)
#define wgtCanvasSetPixel(w, x, y, color)               dvxCanvasApi()->setPixel(w, x, y, color)
#define wgtCanvasGetPixel(w, x, y)                      dvxCanvasApi()->getPixel(w, x, y)
#define wgtCanvasDrawText(w, x, y, text)                dvxCanvasApi()->drawText(w, x, y, text)

#endif // CANVAS_H
