module config;

import st;
import patches;
import deimos.X11.X : KeySym, Mod1Mask;

// Appearance
__gshared string font = "TamzenForPowerline:pixelsize=16:style=regular:antialias=false:hinting=false";

static if (isPatchEnabled!"FONT2_PATCH") {
    __gshared string[] font2 = [];
}

static if (isPatchEnabled!"BACKGROUND_IMAGE_PATCH") {
    __gshared string bgfile = "/path/to/image.ff";
    __gshared int pseudotransparency = 0;
}

static if (isPatchEnabled!"RELATIVEBORDER_PATCH") {
    __gshared int borderperc = 20;
} else {
    extern(C) __gshared int borderpx = 2;
}

static if (isPatchEnabled!"OPENURLONCLICK_PATCH") {
    __gshared uint url_opener_modkey = ShiftMask; // Require Shift to be held
    __gshared string url_opener = "xdg-open";
}

// Shell settings
__gshared string shell = "/bin/sh";
extern(C) __gshared char*[2] _shell_array = [ cast(char*)"/bin/sh", null ];
extern(C) __gshared char** shell_ptr;
extern(C) __gshared char* utmp = null;
extern(C) __gshared char* scroll = null;
extern(C) __gshared char* stty_args = cast(char*)"stty raw pass8 nl -echo -iexten -cstopb 38400";

// Identification sequence
static if (isPatchEnabled!"SIXEL_PATCH") {
    extern(C) __gshared char* vtiden = cast(char*)"\033[?62;4c";
    __gshared const int sixelbyteorder = 0; // LSBFirst
} else {
    extern(C) __gshared char* vtiden = cast(char*)"\033[?6c";
}

// Kerning / character bounding-box multipliers
__gshared float cwscale = 1.0;
extern(C) __gshared float chscale = 1.0;

// Word delimiters
extern(C) __gshared wchar* worddelimiters = cast(wchar*)" ";

static if (isPatchEnabled!"KEYBOARDSELECT_PATCH" && isPatchEnabled!"REFLOW_PATCH") {
    extern(C) __gshared wchar* kbds_sdelim = cast(wchar*)"!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~ ";
    extern(C) __gshared wchar* kbds_ldelim = cast(wchar*)" ";
}

// Selection timeouts (in milliseconds)
extern(C) __gshared uint doubleclicktimeout = 300;
extern(C) __gshared uint tripleclicktimeout = 600;

// Alt screens
extern(C) __gshared int allowaltscreen = 1;

// Window operations
extern(C) __gshared int allowwindowops = 0;

// Draw latency range in ms
extern(C) __gshared double minlatency = 2;
extern(C) __gshared double maxlatency = 33;

// Tab spaces
extern(C) __gshared uint tabspaces = 8;

// Terminal name
extern(C) __gshared char* termname = cast(char*)"st-256color";

// Colors
extern(C) __gshared uint defaultfg = 259;
extern(C) __gshared uint defaultbg = 258;
extern(C) __gshared uint defaultcs = 256;
extern(C) __gshared uint defaultrcs = 257;
extern(C) __gshared uint defaultattr = 11;
extern(C) __gshared uint mousefg = 7;
extern(C) __gshared uint mousebg = 0;

static if (isPatchEnabled!"ALPHA_PATCH") {
    extern(C) __gshared float alpha = 0.8;
    static if (isPatchEnabled!"ALPHA_FOCUS_HIGHLIGHT_PATCH") {
        extern(C) __gshared float alphaUnfocused = 0.6;
    }
    static if (isPatchEnabled!"ALPHA_GRADIENT_PATCH") {
        extern(C) __gshared float grad_alpha = 0.54; // alpha value that'll change
        extern(C) __gshared float stat_alpha = 0.46; // constant alpha value that'll get added to grad_alpha
    }
}

static if (isPatchEnabled!"BOXDRAW_PATCH") {
    extern(C) __gshared const int boxdraw = 1;
    extern(C) __gshared const int boxdraw_bold = 1;
    extern(C) __gshared const int boxdraw_braille = 1;
}

// ASCII printable characters for font width measurement
__gshared immutable string ascii_printable =
    " !\"#$%&'()*+,-./0123456789:;<=>?" ~
    "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" ~
    "`abcdefghijklmnopqrstuvwxyz{|}~";

// Cursor settings
__gshared int cursorstyle = 1;
__gshared int cursorshape = 2;
extern(C) __gshared uint cursorthickness = 2;
extern(C) __gshared Rune stcursor = 0x2603; /* snowman (U+2603) */

// Window settings
extern(C) __gshared int bellvolume = 0;

// Timing settings
extern(C) __gshared uint blinktimeout = 800;

// Import X11 key symbols and modifiers
import deimos.X11.keysym;
import deimos.X11.X : ControlMask, ShiftMask;

// Import needed functions
import st : sendbreak, toggleprinter, printscreen, printsel, kscrollup, kscrolldown;
import x : clipcopy, clippaste, selpaste;

// Keyboard shortcuts
static if (isPatchEnabled!"SCROLLBACK_PATCH") {
    extern(C) __gshared const(Shortcut)[] shortcuts = [
        // Clipboard shortcuts
        Shortcut(TERMMOD, XK_C, &clipcopy, Arg(0), 0),          // Ctrl+Shift+C - copy
        Shortcut(TERMMOD, XK_V, &clippaste, Arg(0), 0),         // Ctrl+Shift+V - paste from clipboard
        // Scrollback support
        Shortcut(ShiftMask, XK_Prior, &kscrollup, Arg(-1), 0),     // Shift+PageUp
        Shortcut(ShiftMask, XK_Next, &kscrolldown, Arg(-1), 0),    // Shift+PageDown
    ];
} else {
    extern(C) __gshared const(Shortcut)[] shortcuts = [
        // Clipboard shortcuts
        Shortcut(TERMMOD, XK_C, &clipcopy, Arg(0), 0),          // Ctrl+Shift+C - copy
        Shortcut(TERMMOD, XK_V, &clippaste, Arg(0), 0),         // Ctrl+Shift+V - paste from clipboard
    ];
}

// Key modifier constants
enum uint XK_ANY_MOD = uint.max;
enum uint XK_NO_MOD = 0;
enum uint TERMMOD = ControlMask | ShiftMask;

// Key mappings - essential keys for basic functionality
extern(C) __gshared const(Key)[] key = [
    // Backspace - standard DEL (0x7F) for Linux terminals
    { XK_BackSpace,     XK_NO_MOD,      cast(char*)"\177",          0,    0},
    { XK_BackSpace,     Mod1Mask,       cast(char*)"\033\177",      0,    0},
    // Delete
    { XK_Delete,        XK_ANY_MOD,     cast(char*)"\033[P",       -1,    0},
    { XK_Delete,        XK_ANY_MOD,     cast(char*)"\033[3~",      +1,    0},
    // Arrow keys
    { XK_Up,            XK_NO_MOD,      cast(char*)"\033OA",        0,   -1},
    { XK_Up,            XK_NO_MOD,      cast(char*)"\033[A",        0,   +1},
    { XK_Down,          XK_NO_MOD,      cast(char*)"\033OB",        0,   -1},
    { XK_Down,          XK_NO_MOD,      cast(char*)"\033[B",        0,   +1},
    { XK_Left,          XK_NO_MOD,      cast(char*)"\033OD",        0,   -1},
    { XK_Left,          XK_NO_MOD,      cast(char*)"\033[D",        0,   +1},
    { XK_Right,         XK_NO_MOD,      cast(char*)"\033OC",        0,   -1},
    { XK_Right,         XK_NO_MOD,      cast(char*)"\033[C",        0,   +1},
    // Home/End
    { XK_Home,          XK_ANY_MOD,     cast(char*)"\033[H",        0,   -1},
    { XK_Home,          XK_ANY_MOD,     cast(char*)"\033[1~",       0,   +1},
    { XK_End,           XK_ANY_MOD,     cast(char*)"\033[4~",       0,    0},
    // Page Up/Down
    { XK_Prior,         XK_ANY_MOD,     cast(char*)"\033[5~",       0,    0},
    { XK_Next,          XK_ANY_MOD,     cast(char*)"\033[6~",       0,    0},
];

extern(C) __gshared const(KeySym)[] mappedkeys = [
    /* Special keys that need to be handled */
    XK_BackSpace,
    XK_Delete,
    XK_Up,
    XK_Down,
    XK_Left,
    XK_Right,
    XK_Home,
    XK_End,
    XK_Prior,
    XK_Next,
];

// Mouse shortcuts
import x : MouseShortcut;
import deimos.X11.X : Button1, Button2, Button3, Button4, Button5;

static if (isPatchEnabled!"SCROLLBACK_MOUSE_PATCH") {
    static if (isPatchEnabled!"SCROLLBACK_MOUSE_ALTSCREEN_PATCH") {
        extern(C) __gshared const(MouseShortcut)[] mshortcuts = [
            // Clipboard pasting
            MouseShortcut(XK_NO_MOD, Button2, &selpaste, Arg(0), 1, 0),     // Middle-click paste (primary)
            MouseShortcut(ShiftMask, Button2, &clippaste, Arg(0), 1, 0),    // Shift+Middle-click paste (clipboard)
            // Primary screen (normal mode) - scroll without modifier
            MouseShortcut(0, Button4, &kscrollup, Arg(1), 0, x.S_PRI),           // ScrollUp (no modifier)
            MouseShortcut(0, Button5, &kscrolldown, Arg(1), 0, x.S_PRI),         // ScrollDown (no modifier)
            // With Shift for faster scrolling
            MouseShortcut(ShiftMask, Button4, &kscrollup, Arg(2), 0, x.S_PRI),    // Shift+ScrollUp
            MouseShortcut(ShiftMask, Button5, &kscrolldown, Arg(2), 0, x.S_PRI),  // Shift+ScrollDown
        ];
    } else {
        extern(C) __gshared const(MouseShortcut)[] mshortcuts = [
            // Clipboard pasting
            MouseShortcut(XK_NO_MOD, Button2, &selpaste, Arg(0), 1, 0),     // Middle-click paste (primary)
            MouseShortcut(ShiftMask, Button2, &clippaste, Arg(0), 1, 0),    // Shift+Middle-click paste (clipboard)
            MouseShortcut(ShiftMask, Button4, &kscrollup, Arg(2), 0, x.S_PRI),    // Shift+ScrollUp
            MouseShortcut(ShiftMask, Button5, &kscrolldown, Arg(2), 0, x.S_PRI),  // Shift+ScrollDown
        ];
    }
} else {
    extern(C) __gshared const(MouseShortcut)[] mshortcuts = [
        // Clipboard pasting (even without scrollback)
        MouseShortcut(XK_NO_MOD, Button2, &selpaste, Arg(0), 1, 0),     // Middle-click paste (primary)
        MouseShortcut(ShiftMask, Button2, &clippaste, Arg(0), 1, 0),    // Shift+Middle-click paste (clipboard)
    ];
}

// Function to get mouse shortcuts (for x.d to access)
extern(C) const(MouseShortcut)[] getMouseShortcuts() {
    return mshortcuts;
}

// Selection masks
extern(C) __gshared const(uint)[] selmasks = [
    0,              // SEL_REGULAR (placeholder)
    Mod1Mask,       // SEL_RECTANGULAR
];
extern(C) __gshared int selmaskslen = 2;

// Color names (256 standard colors + default fg/bg)
__gshared const(char)*[260] colorname = [
    // 16 basic colors
    "#202020", "#e84f4f", "#b8d68c", "#e2a959",
    "#7dc1cf", "#9b64fb", "#6d878d", "#bbbbbb",
    "#735264", "#d43131", "#578d3b", "#f39713",
    "#4e9fb1", "#7c1ede", "#42717b", "#cccccc",

    // 240 extended colors (6x6x6 color cube + 24 grayscale)
    // These will be calculated programmatically
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,
    null, null, null, null, null, null, null, null,

    // Special colors at specific indices to match C config
    "#dddddd", // [256] cursor color
    "#555555", // [257] reverse cursor
    "#151515", // [258] default background colour  
    "#d5cad5", // [259] default foreground colour
];
