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

// dvxDlg.h  -- Modal dialogs for DVX GUI
//
// Provides pre-built modal dialog boxes (message box, file dialog) that
// block the caller and run their own event loop via dvxUpdate() until the
// user dismisses them. Modal dialogs set ctx->modalWindow to prevent input
// from reaching other windows during the dialog's lifetime.
//
// The flag encoding uses separate bit fields for button configuration
// (low nibble) and icon type (high nibble) so they can be OR'd together
// in a single int32_t argument, matching the Win16 MessageBox() convention.
#ifndef DVX_DLG_H
#define DVX_DLG_H

#include "dvxApp.h"

// ============================================================
// Message box button flags (low nibble)
// ============================================================

#define MB_OK            0x0000
#define MB_OKCANCEL      0x0001
#define MB_YESNO         0x0002
#define MB_YESNOCANCEL   0x0003
#define MB_RETRYCANCEL   0x0004

#define MB_BUTTON_MASK   0x000F  // isolates the button-set nibble

// ============================================================
// Message box icon flags (high nibble, OR with button flags)
// ============================================================

#define MB_ICONINFO      0x0010
#define MB_ICONWARNING   0x0020
#define MB_ICONERROR     0x0030
#define MB_ICONQUESTION  0x0040

#define MB_ICON_MASK     0x00F0  // isolates the icon-type nibble

// ============================================================
// Message box return values
// ============================================================

#define ID_OK       1
#define ID_CANCEL   2
#define ID_YES      3
#define ID_NO       4
#define ID_RETRY    5

// Display a modal message box with the specified button and icon combination.
// Blocks the caller by running dvxUpdate() in a loop until a button is
// pressed or the dialog is closed. Returns the ID_xxx value of the button
// that was pressed. The dialog window is automatically destroyed on return.
int32_t dvxMessageBox(AppContextT *ctx, const char *title, const char *message, int32_t flags);

// Convenience wrapper: MB_OK | MB_ICONERROR.  Title defaults to "Error"
// when NULL.  Returns ID_OK.
int32_t dvxErrorBox(AppContextT *ctx, const char *title, const char *message);

// Convenience wrapper: MB_OK | MB_ICONINFO.  Title defaults to NULL-permitted.
int32_t dvxInfoBox(AppContextT *ctx, const char *title, const char *message);

// ============================================================
// File dialog flags
// ============================================================

#define FD_OPEN          0x0000    // Open file (default)
#define FD_SAVE          0x0001    // Save file

// ============================================================
// File dialog filter
// ============================================================
//
// Filters are displayed in a dropdown at the bottom of the file dialog.
// Pattern matching is case-insensitive and supports semicolon-separated
// lists of glob patterns (e.g. "*.bmp;*.png"). A filename matches if any
// pattern in the list matches.

// File filter for the file dialog.  Pattern is extracted from inside
// parentheses in the label.  Example: "Text Files (*.txt)"
// Multiple extensions: "Images (*.bmp;*.png;*.jpg;*.gif)"
typedef struct {
    const char *label;      // e.g. "Text Files (*.txt)" -- pattern extracted from parens
} FileFilterT;

// Display a modal file open/save dialog. The dialog shows a directory
// listing with navigation (parent directory, drive letters on DOS), a
// filename text input, and an optional filter dropdown. Blocks the caller
// via dvxUpdate() loop. Returns true if the user selected a file (path
// written to outPath), false if cancelled or closed. initialDir may be
// NULL to start in the current working directory.
bool dvxFileDialog(AppContextT *ctx, const char *title, int32_t flags, const char *initialDir, const FileFilterT *filters, int32_t filterCount, char *outPath, int32_t outPathSize);

// Display a modal input box with a prompt label, text input field,
// and OK/Cancel buttons. Blocks the caller via dvxUpdate() loop.
// Returns true if the user clicked OK (text written to outBuf),
// false if cancelled or closed. defaultText may be NULL.
bool dvxInputBox(AppContextT *ctx, const char *title, const char *prompt, const char *defaultText, char *outBuf, int32_t outBufSize);

// Display a modal integer input box with a spinner (up/down arrows).
// Returns true if the user clicked OK, false if cancelled.
bool dvxIntInputBox(AppContextT *ctx, const char *title, const char *prompt, int32_t defaultVal, int32_t minVal, int32_t maxVal, int32_t step, int32_t *outVal);

// ============================================================
// Save prompt helper
// ============================================================
//
// Common "Save changes?" dialog for apps with unsaved data.
// Returns:
//   DVX_SAVE_YES    -- user wants to save (caller should save, then proceed)
//   DVX_SAVE_NO     -- user wants to discard (caller should proceed without saving)
//   DVX_SAVE_CANCEL -- user cancelled (caller should abort the operation)

#define DVX_SAVE_YES     1
#define DVX_SAVE_NO      2
#define DVX_SAVE_CANCEL  3

int32_t dvxPromptSave(AppContextT *ctx, const char *title);

// ============================================================
// Choice dialog
// ============================================================
//
// Display a modal dialog with a listbox of choices. The user picks
// one item and clicks OK, or cancels. Returns true if a choice was
// made (selected index written to outIdx), false if cancelled.
// defaultIdx is the initially highlighted item (-1 for none).

bool dvxChoiceDialog(AppContextT *ctx, const char *title, const char *prompt, const char **items, int32_t itemCount, int32_t defaultIdx, int32_t *outIdx);

#endif // DVX_DLG_H
