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

// listView.h  -- ListView widget API
//
// Include this header in app/shell code that uses the ListView widget.
// Column types (ListViewColT, ListViewAlignE, ListViewSortE) are
// defined in dvxWgt.h so widget DXEs can reference them.
#ifndef LISTVIEW_H
#define LISTVIEW_H

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

typedef struct {
    WidgetT *(*create)(WidgetT *parent);
    void     (*setColumns)(WidgetT *w, const ListViewColT *cols, int32_t count);
    void     (*setData)(WidgetT *w, const char **cellData, int32_t rowCount);
    int32_t  (*getSelected)(const WidgetT *w);
    void     (*setSelected)(WidgetT *w, int32_t idx);
    void     (*setSort)(WidgetT *w, int32_t col, ListViewSortE dir);
    void     (*setHeaderClickCallback)(WidgetT *w, void (*cb)(WidgetT *w, int32_t col, ListViewSortE dir));
    void     (*setMultiSelect)(WidgetT *w, bool multi);
    bool     (*isItemSelected)(const WidgetT *w, int32_t idx);
    void     (*setItemSelected)(WidgetT *w, int32_t idx, bool selected);
    void     (*selectAll)(WidgetT *w);
    void     (*clearSelection)(WidgetT *w);
    void     (*setReorderable)(WidgetT *w, bool reorderable);
    void     (*addItem)(WidgetT *w, const char *text);
    void     (*removeRow)(WidgetT *w, int32_t row);
    void     (*clear)(WidgetT *w);
    const char *(*getCell)(const WidgetT *w, int32_t row, int32_t col);
    void     (*setCell)(WidgetT *w, int32_t row, int32_t col, const char *text);
    int32_t  (*getRowCount)(const WidgetT *w);
} ListViewApiT;

static inline const ListViewApiT *dvxListViewApi(void) {
    static const ListViewApiT *sApi;
    if (!sApi) { sApi = (const ListViewApiT *)wgtGetApi("listview"); }
    return sApi;
}

#define wgtListView(parent)                              dvxListViewApi()->create(parent)
#define wgtListViewSetColumns(w, cols, count)             dvxListViewApi()->setColumns(w, cols, count)
#define wgtListViewSetData(w, cellData, rowCount)         dvxListViewApi()->setData(w, cellData, rowCount)
#define wgtListViewGetSelected(w)                        dvxListViewApi()->getSelected(w)
#define wgtListViewSetSelected(w, idx)                   dvxListViewApi()->setSelected(w, idx)
#define wgtListViewSetSort(w, col, dir)                  dvxListViewApi()->setSort(w, col, dir)
#define wgtListViewSetHeaderClickCallback(w, cb)         dvxListViewApi()->setHeaderClickCallback(w, cb)
#define wgtListViewSetMultiSelect(w, multi)              dvxListViewApi()->setMultiSelect(w, multi)
#define wgtListViewIsItemSelected(w, idx)                dvxListViewApi()->isItemSelected(w, idx)
#define wgtListViewSetItemSelected(w, idx, selected)     dvxListViewApi()->setItemSelected(w, idx, selected)
#define wgtListViewSelectAll(w)                          dvxListViewApi()->selectAll(w)
#define wgtListViewClearSelection(w)                     dvxListViewApi()->clearSelection(w)
#define wgtListViewSetReorderable(w, reorderable)        dvxListViewApi()->setReorderable(w, reorderable)
#define wgtListViewAddItem(w, text)                      dvxListViewApi()->addItem(w, text)
#define wgtListViewRemoveRow(w, row)                     dvxListViewApi()->removeRow(w, row)
#define wgtListViewClear(w)                              dvxListViewApi()->clear(w)
#define wgtListViewGetCell(w, row, col)                  dvxListViewApi()->getCell(w, row, col)
#define wgtListViewSetCell(w, row, col, text)            dvxListViewApi()->setCell(w, row, col, text)
#define wgtListViewGetRowCount(w)                        dvxListViewApi()->getRowCount(w)

#endif // LISTVIEW_H
