// 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.

// dataCtrl.h -- VB3-style Data control for database binding

#ifndef DATACTRL_H
#define DATACTRL_H

#include "dvxWgt.h"

typedef struct {
    WidgetT    *(*create)(WidgetT *parent);
    void        (*refresh)(WidgetT *w);
    void        (*moveFirst)(WidgetT *w);
    void        (*movePrev)(WidgetT *w);
    void        (*moveNext)(WidgetT *w);
    void        (*moveLast)(WidgetT *w);
    const char *(*getField)(WidgetT *w, const char *colName);
    void        (*setField)(WidgetT *w, const char *colName, const char *value);
    void        (*updateRow)(WidgetT *w);
    void        (*update)(WidgetT *w);
    void        (*addNew)(WidgetT *w);
    void        (*delete)(WidgetT *w);
    void        (*setMasterValue)(WidgetT *w, const char *val);
    int32_t     (*getRowCount)(WidgetT *w);
    int32_t     (*getColCount)(WidgetT *w);
    const char *(*getColName)(WidgetT *w, int32_t col);
    const char *(*getCellText)(WidgetT *w, int32_t row, int32_t col);
    void        (*setCurrentRow)(WidgetT *w, int32_t row);
} DataCtrlApiT;

static inline const DataCtrlApiT *dvxDataCtrlApi(void) {
    return (const DataCtrlApiT *)wgtGetApi("data");
}

#define wgtDataCtrl(parent)                dvxDataCtrlApi()->create(parent)
#define wgtDataCtrlRefresh(w)              dvxDataCtrlApi()->refresh(w)
#define wgtDataCtrlMoveFirst(w)            dvxDataCtrlApi()->moveFirst(w)
#define wgtDataCtrlMovePrev(w)             dvxDataCtrlApi()->movePrev(w)
#define wgtDataCtrlMoveNext(w)             dvxDataCtrlApi()->moveNext(w)
#define wgtDataCtrlMoveLast(w)             dvxDataCtrlApi()->moveLast(w)
#define wgtDataCtrlGetField(w, col)        dvxDataCtrlApi()->getField(w, col)
#define wgtDataCtrlSetField(w, col, v)     dvxDataCtrlApi()->setField(w, col, v)
#define wgtDataCtrlUpdateRow(w)            dvxDataCtrlApi()->updateRow(w)
#define wgtDataCtrlUpdate(w)               dvxDataCtrlApi()->update(w)
#define wgtDataCtrlAddNew(w)               dvxDataCtrlApi()->addNew(w)
#define wgtDataCtrlDelete(w)               dvxDataCtrlApi()->delete(w)
#define wgtDataCtrlSetMasterValue(w, val)  dvxDataCtrlApi()->setMasterValue(w, val)
#define wgtDataCtrlGetRowCount(w)          dvxDataCtrlApi()->getRowCount(w)
#define wgtDataCtrlGetColCount(w)          dvxDataCtrlApi()->getColCount(w)
#define wgtDataCtrlGetColName(w, col)      dvxDataCtrlApi()->getColName(w, col)
#define wgtDataCtrlGetCellText(w, r, c)    dvxDataCtrlApi()->getCellText(w, r, c)
#define wgtDataCtrlSetCurrentRow(w, row)   dvxDataCtrlApi()->setCurrentRow(w, row)

#endif // DATACTRL_H
