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

// textInpt.h  -- Text input widget API
#ifndef TEXTINPT_H
#define TEXTINPT_H

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

// Colorize callback: called for each visible line during paint.
// line: pointer into the buffer (NOT null-terminated).
// lineLen: number of characters in this line.
// colors: output array -- fill colors[0..lineLen-1] with color indices:
//   0 = default, 1 = keyword, 2 = string, 3 = comment,
//   4 = number, 5 = operator, 6 = type/builtin, 7 = reserved
// ctx: user context pointer.
typedef void (*TextColorFnT)(const char *line, int32_t lineLen, uint8_t *colors, void *ctx);

typedef struct {
    WidgetT *(*create)(WidgetT *parent, int32_t maxLen);
    WidgetT *(*password)(WidgetT *parent, int32_t maxLen);
    WidgetT *(*masked)(WidgetT *parent, const char *mask);
    WidgetT *(*textArea)(WidgetT *parent, int32_t maxLen);
    void     (*setColorize)(WidgetT *w, TextColorFnT fn, void *ctx);
    void     (*goToLine)(WidgetT *w, int32_t line);
    void     (*setAutoIndent)(WidgetT *w, bool enable);
    void     (*setShowLineNumbers)(WidgetT *w, bool show);
    void     (*setCaptureTabs)(WidgetT *w, bool capture);
    void     (*setTabWidth)(WidgetT *w, int32_t width);
    void     (*setUseTabChar)(WidgetT *w, bool useChar);
    bool     (*findNext)(WidgetT *w, const char *needle, bool caseSensitive, bool forward);
    int32_t  (*replaceAll)(WidgetT *w, const char *needle, const char *replacement, bool caseSensitive);
    bool     (*replaceSelection)(WidgetT *w, const char *needle, const char *replacement, bool caseSensitive, bool forward);
    void     (*setLineDecorator)(WidgetT *w, uint32_t (*fn)(int32_t, uint32_t *, void *), void *ctx);
    int32_t  (*getCursorLine)(const WidgetT *w);
    void     (*setGutterClick)(WidgetT *w, void (*fn)(WidgetT *, int32_t));
    int32_t  (*getWordAtCursor)(const WidgetT *w, char *buf, int32_t bufSize);
    void     (*setSyntaxColors)(WidgetT *w, const uint32_t *colors, int32_t count);
    void     (*cursorToEnd)(WidgetT *w);
} TextInputApiT;

static inline const TextInputApiT *dvxTextInputApi(void) {
    static const TextInputApiT *sApi;
    if (!sApi) { sApi = (const TextInputApiT *)wgtGetApi("textinput"); }
    return sApi;
}

#define wgtTextInput(parent, maxLen)     dvxTextInputApi()->create(parent, maxLen)
#define wgtPasswordInput(parent, maxLen) dvxTextInputApi()->password(parent, maxLen)
#define wgtMaskedInput(parent, mask)     dvxTextInputApi()->masked(parent, mask)
#define wgtTextArea(parent, maxLen)      dvxTextInputApi()->textArea(parent, maxLen)
#define wgtTextAreaSetColorize(w, fn, ctx) dvxTextInputApi()->setColorize(w, fn, ctx)
#define wgtTextAreaGoToLine(w, line)      dvxTextInputApi()->goToLine(w, line)
#define wgtTextAreaSetAutoIndent(w, en)   dvxTextInputApi()->setAutoIndent(w, en)
#define wgtTextAreaSetShowLineNumbers(w, show) dvxTextInputApi()->setShowLineNumbers(w, show)
#define wgtTextAreaSetCaptureTabs(w, capture) dvxTextInputApi()->setCaptureTabs(w, capture)
#define wgtTextAreaSetTabWidth(w, width)      dvxTextInputApi()->setTabWidth(w, width)
#define wgtTextAreaSetUseTabChar(w, useChar)  dvxTextInputApi()->setUseTabChar(w, useChar)
#define wgtTextAreaFindNext(w, needle, caseSens, fwd) dvxTextInputApi()->findNext(w, needle, caseSens, fwd)
#define wgtTextAreaReplaceAll(w, needle, repl, caseSens) dvxTextInputApi()->replaceAll(w, needle, repl, caseSens)
#define wgtTextAreaReplaceSelection(w, needle, repl, caseSens, fwd) dvxTextInputApi()->replaceSelection(w, needle, repl, caseSens, fwd)
#define wgtTextAreaSetLineDecorator(w, fn, ctx)          dvxTextInputApi()->setLineDecorator(w, fn, ctx)
#define wgtTextAreaGetCursorLine(w)                      dvxTextInputApi()->getCursorLine(w)
#define wgtTextAreaSetGutterClick(w, fn)                 dvxTextInputApi()->setGutterClick(w, fn)
#define wgtTextAreaGetWordAtCursor(w, buf, sz)           dvxTextInputApi()->getWordAtCursor(w, buf, sz)
#define wgtTextAreaSetSyntaxColors(w, colors, count)    dvxTextInputApi()->setSyntaxColors(w, colors, count)
#define wgtTextAreaCursorToEnd(w)                       dvxTextInputApi()->cursorToEnd(w)

#endif // TEXTINPT_H
