#include <stdlib.h>
#include <inttypes.h>

/* Taken from https://github.com/djpohly/dwl/issues/466 */
#define COLOR(hex)    { ((hex >> 24) & 0xFF) / 255.0f, \
                        ((hex >> 16) & 0xFF) / 255.0f, \
                        ((hex >> 8) & 0xFF) / 255.0f, \
                        (hex & 0xFF) / 255.0f }
/* appearance */
static const int sloppyfocus               = 0;  /* focus follows mouse */
static const int bypass_surface_visibility = 0;  /* 1 means idle inhibitors will disable idle tracking even if it's surface isn't visible  */
static const unsigned int borderpx         = 3;  /* border pixel of windows */
static const int smartgaps                 = 0;  /* 1 means no outer gap when there is only one window */
static const int monoclegaps               = 0;  /* 1 means outer gaps in monocle layout */
static const unsigned int gappih           = 6; /* horiz inner gap between windows */
static const unsigned int gappiv           = 6; /* vert inner gap between windows */
static const unsigned int gappoh           = 0; /* horiz outer gap between windows and screen edge */
static const unsigned int gappov           = 0; /* vert outer gap between windows and screen edge */
static const int showbar                   = 0; /* 0 means no bar */
static const int topbar                    = 1; /* 0 means bottom bar */
static const char *fonts[]                 = { "BigBlueTermPlus Nerd Font:size=14" };
static const float rootcolor[]             = COLOR(0x000000ff);
static const int trayspacing               = 4; /* Spacing between icons in system tray */
static const int traymargins               = 4; /* System tray inner margins */
/* This conforms to the xdg-protocol. Set the alpha to zero to restore the old behavior */
static const float fullscreen_bg[]         = {0.1f, 0.1f, 0.1f, 1.0f}; /* You can also use glsl colors */
static uint32_t colors[][3]                = {
	/*               fg          bg          border    */
	[SchemeNorm] = { 0xbbbbbbff, 0x222222ff, 0x444444ff },
	[SchemeSel]  = { 0xeeeeeeff, 0x005577ff, 0x005577ff },
	[SchemeUrg]  = { 0,          0,          0x770000ff },
};

/* tagging - TAGCOUNT must be no greater than 31 */
static char *tags[] = {
	"  ",
	"  ",
	"  ",
	"  ",
	"  ",
	"  ",
	"  ",
	"  ",
	"  "
};

static const unsigned int ulinepad = 5;    /* horizontal padding between the underline and tag */
static const unsigned int ulinestroke  = 2;    /* thickness / height of the underline */
static const unsigned int ulinevoffset = 0;    /* how far above the bottom of the bar the line should appear */
static const int ulineall      = 0;    /* 1 to show underline on all tags, 0 for just the active ones */

/* logging */
static int log_level = WLR_ERROR;

/* NOTE: ALWAYS keep a rule declared even if you don't use rules (e.g leave at least one example) */
static const Rule rules[] = {
	/* app_id             title       tags mask     isfloating  isterm  noswallow  monitor scratchkey */
	/* examples: */
	{ "Gimp_EXAMPLE",     NULL,       0,            1,          0,      0,         -1,     0 },
	{ "firefox_EXAMPLE",  NULL,       1 << 8,       0,          0,      0,         -1,     0 },
	{ "Alacritty",        NULL,       0,            0,          1,      1,         -1,     0 },
	{ "st",               NULL,       0,            0,          1,      1,         -1,     0 },
	{ "mpv",              NULL,       0,            0,          0,      0,         -1,     0 },
	{ NULL,               "spterm",   0,            1,          1,      0,         -1,    's' },
	{ NULL,               "spfm",     0,            1,          1,      0,         -1,    'f' },
	{ NULL,               "pcmanfm",  0,            1,          0,      0,         -1,    'p' },
	{ NULL,               "spflip",   0,            1,          0,      0,         -1,    'h' },
	{ NULL,               "spmpd",    0,            1,          0,      0,         -1,    'm' },
	{ NULL,               "spnews",   0,            0,          0,      0,         -1,    'm' },
};

const char *spterm[]  = {"s", "alacritty", "--title", "spterm", NULL };
const char *spfm[]    = {"f", "alacritty", "--title", "spfm", "-e", "zsh", "-ic", "lf", NULL };
const char *sppcman[] = {"p", "pcmanfm", NULL };
const char *spflip[]  = {"h", "alacritty", "--title", "spflip", "-e", "hf", NULL };
const char *spmpd[]   = {"m", "alacritty", "--title", "spmpd",  "-e", "ncmpc", NULL };
const char *spnews[]  = {"n", "alacritty", "--title", "spnews", "-e", "nb", NULL };

/* layout(s) */
static const Layout layouts[] = {
	/* symbol     arrange function */
	{ "[]=",      tile },
	{ "[M]",      monocle },
	{ "><>",      NULL },    /* no layout function means floating behavior */
};

/* monitors */
/* (x=-1, y=-1) is reserved as an "autoconfigure" monitor position indicator
 * WARNING: negative values other than (-1, -1) cause problems with Xwayland clients
 * https://gitlab.freedesktop.org/xorg/xserver/-/issues/899
*/
/* NOTE: ALWAYS add a fallback rule, even if you are completely sure it won't be used */
static const MonitorRule monrules[] = {
	/* name       mfact  nmaster scale layout       rotate/reflect                x    y */
	/* example of a HiDPI laptop monitor:
	{ "eDP-1",    0.5f,  1,      2,    &layouts[0], WL_OUTPUT_TRANSFORM_NORMAL,   -1,  -1 },
	*/
	/* defaults */
	{ "DP-3",       0.5f, 1,      1,    &layouts[0], WL_OUTPUT_TRANSFORM_NORMAL,   0,     0 },
	{ "HDMI-A-3",   0.5f, 1,      1,    &layouts[0], WL_OUTPUT_TRANSFORM_NORMAL,   0,     0 },
	{ "DP-1",       0.5f, 1,      1,    &layouts[0], WL_OUTPUT_TRANSFORM_NORMAL,   1920,  0 },
};

/* keyboard */
static const struct xkb_rule_names xkb_rules = {
	/* can specify fields: rules, model, layout, variant, options */
	/* example:
	.options = "ctrl:nocaps",
	*/
	.layout = "us",
	.variant = "intl",
	.options = NULL,
};

static const int repeat_rate = 100;
static const int repeat_delay = 200;

/* Trackpad */
static const int tap_to_click = 1;
static const int tap_and_drag = 1;
static const int drag_lock = 1;
static const int natural_scrolling = 0;
static const int disable_while_typing = 1;
static const int left_handed = 0;
static const int middle_button_emulation = 0;
/* You can choose between:
LIBINPUT_CONFIG_SCROLL_NO_SCROLL
LIBINPUT_CONFIG_SCROLL_2FG
LIBINPUT_CONFIG_SCROLL_EDGE
LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN
*/
static const enum libinput_config_scroll_method scroll_method = LIBINPUT_CONFIG_SCROLL_2FG;

/* You can choose between:
LIBINPUT_CONFIG_CLICK_METHOD_NONE
LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS
LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER
*/
static const enum libinput_config_click_method click_method = LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;

/* You can choose between:
LIBINPUT_CONFIG_SEND_EVENTS_ENABLED
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED
LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE
*/
static const uint32_t send_events_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;

/* You can choose between:
LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT
LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE
*/
static const enum libinput_config_accel_profile accel_profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
static const double accel_speed = 0.0;

/* You can choose between:
LIBINPUT_CONFIG_TAP_MAP_LRM -- 1/2/3 finger tap maps to left/right/middle
LIBINPUT_CONFIG_TAP_MAP_LMR -- 1/2/3 finger tap maps to left/middle/right
*/
static const enum libinput_config_tap_button_map button_map = LIBINPUT_CONFIG_TAP_MAP_LRM;

/* If you want to use the windows key for MODKEY, use WLR_MODIFIER_LOGO */
#define MODKEY	WLR_MODIFIER_LOGO
#define META	WLR_MODIFIER_ALT
#define CTRL	WLR_MODIFIER_CTRL
#define SHIFT	WLR_MODIFIER_SHIFT

#define TAGKEYS(KEY,SKEY,TAG) \
	{ MODKEY,             KEY,            view,            {.ui = 1 << TAG} }, \
	{ MODKEY|META|CTRL,   KEY,            toggleview,      {.ui = 1 << TAG} }, \
	{ MODKEY|SHIFT,       SKEY,           tag,             {.ui = 1 << TAG} }, \
	{ MODKEY|META,        KEY,            toggletag, {.ui = 1 << TAG} }

/* helper for spawning shell commands in the pre dwm-5.0 fashion */
#define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }

/* commands */
static const char *termcmd[]     = { "alacritty", NULL };
static const char *menucmd[]     = { "wmenu-run", NULL };
static const char *dmpass_full_cmd[] = { "dmpass", "--full", NULL };
static const char *dmpass_cmd[]      = { "dmpass", NULL };
static const char *dmotp_cmd[]       = { "dmotp", NULL };
static const char *browser_cmd[] = { "firefox", NULL };

static const Key keys[] = {
	/* Note that Shift changes certain key codes: c -> C, 2 -> at, etc. */
	/* modifier              key                    function          argument */
	{ MODKEY,                XKB_KEY_p,             spawn,            {.v = menucmd} },
	{ MODKEY,                XKB_KEY_Return,        spawn,            {.v = termcmd} },
	{ MODKEY,                XKB_KEY_F1,            togglescratch,    {.v = spterm } },
	{ MODKEY,                XKB_KEY_F2,            togglescratch,    {.v = spfm } },
	{ MODKEY|SHIFT,          XKB_KEY_F2,            togglescratch,    {.v = sppcman } },
	{ MODKEY,                XKB_KEY_F3,            spawn,            {.v = browser_cmd } },
	{ MODKEY,                XKB_KEY_F4,            togglescratch,    {.v = spflip } },
	{ MODKEY,                XKB_KEY_F5,            togglescratch,    {.v = spmpd } },
	{ MODKEY,                XKB_KEY_F6,            togglescratch,    {.v = spnews } },
	{ MODKEY|CTRL,           XKB_KEY_p,             spawn,            {.v = dmpass_cmd } },
	{ MODKEY|CTRL|SHIFT,     XKB_KEY_p,             spawn,            {.v = dmpass_full_cmd } },
	{ MODKEY|CTRL,           XKB_KEY_o,             spawn,            {.v = dmotp_cmd } },
	{ MODKEY,                XKB_KEY_b,             togglebar,        {0} },
	{ MODKEY,                XKB_KEY_j,             focusstack,       {.i = +1} },
	{ MODKEY,                XKB_KEY_k,             focusstack,       {.i = -1} },
	{ MODKEY|SHIFT,          XKB_KEY_J,             movestack,        {.i = +1} },
	{ MODKEY|SHIFT,          XKB_KEY_K,             movestack,        {.i = -1} },
	{ MODKEY,                XKB_KEY_i,             incnmaster,       {.i = +1} },
	{ MODKEY,                XKB_KEY_d,             incnmaster,       {.i = -1} },
	{ MODKEY|CTRL,           XKB_KEY_a,             incnmaster,       {.i = +1} },
	{ MODKEY|CTRL,           XKB_KEY_x,             incnmaster,       {.i = -1} },
	{ MODKEY|CTRL,           XKB_KEY_minus,         setmfact,         {.f = -0.025f} },
	{ MODKEY|CTRL,           XKB_KEY_equal,         setmfact,         {.f = +0.025f} },
	{ MODKEY,                XKB_KEY_minus,         incigaps,         {.i = -1 } },
	{ MODKEY,                XKB_KEY_equal,         incigaps,         {.i = +1 } },
	{ MODKEY|SHIFT,          XKB_KEY_plus,          togglegaps,       {0} },
	{ MODKEY|CTRL,           XKB_KEY_h,             shiftview,        { .i = -1 } },
	{ MODKEY|CTRL,           XKB_KEY_l,             shiftview,        { .i = +1 } },
	{ MODKEY|META,           XKB_KEY_h,             shiftboth,        { .i = -1 } },
	{ MODKEY|META,           XKB_KEY_l,             shiftboth,        { .i = +1 } },
	{ MODKEY|SHIFT,          XKB_KEY_Return,        zoom,             {0} },
	{ META,                  XKB_KEY_Escape,        view,             {0} },
	{ MODKEY,                XKB_KEY_q,             killclient,       {0} },
	{ MODKEY,                XKB_KEY_s,             setlayout,        {.v = &layouts[0]} },
	{ MODKEY,                XKB_KEY_m,             setlayout,        {.v = &layouts[1]} },
	{ MODKEY,                XKB_KEY_n,             setlayout,        {.v = &layouts[2]} },
	{ MODKEY|SHIFT,          XKB_KEY_space,         setlayout,        {0} },
	{ MODKEY,                XKB_KEY_space,         togglefloating,   {0} },
	{ MODKEY,                XKB_KEY_f,             togglefullscreen, {0} },
	{ MODKEY,                XKB_KEY_0,             view,             {.ui = ~0} },
	{ MODKEY|SHIFT,          XKB_KEY_parenright,    tag,              {.ui = ~0} },
	{ MODKEY,                XKB_KEY_bracketleft,   focusmon,         {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY,                XKB_KEY_bracketright,  focusmon,         {.i = WLR_DIRECTION_RIGHT} },
	{ MODKEY,                XKB_KEY_h,             focusmon,         {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY,                XKB_KEY_l,             focusmon,         {.i = WLR_DIRECTION_RIGHT} },
	{ MODKEY|SHIFT,          XKB_KEY_H,             tagmon,           {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY|SHIFT,          XKB_KEY_L,             tagmon,           {.i = WLR_DIRECTION_RIGHT} },
	{ MODKEY|SHIFT,          XKB_KEY_bracketleft,   tagmon,           {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY|SHIFT,          XKB_KEY_bracketright,  tagmon,           {.i = WLR_DIRECTION_RIGHT} },
	{ MODKEY|SHIFT,          XKB_KEY_H,             focusmon,         {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY|SHIFT,          XKB_KEY_L,             focusmon,         {.i = WLR_DIRECTION_RIGHT} },
	{ MODKEY|SHIFT,          XKB_KEY_leftmiddlecurlybrace,  focusmon, {.i = WLR_DIRECTION_LEFT} },
	{ MODKEY|SHIFT,          XKB_KEY_rightmiddlecurlybrace, focusmon, {.i = WLR_DIRECTION_RIGHT} },
	TAGKEYS(                 XKB_KEY_1, XKB_KEY_exclam,               0),
	TAGKEYS(                 XKB_KEY_2, XKB_KEY_at,                   1),
	TAGKEYS(                 XKB_KEY_3, XKB_KEY_numbersign,           2),
	TAGKEYS(                 XKB_KEY_4, XKB_KEY_dollar,               3),
	TAGKEYS(                 XKB_KEY_5, XKB_KEY_percent,              4),
	TAGKEYS(                 XKB_KEY_6, XKB_KEY_asciicircum,          5),
	TAGKEYS(                 XKB_KEY_7, XKB_KEY_ampersand,            6),
	TAGKEYS(                 XKB_KEY_8, XKB_KEY_asterisk,             7),
	TAGKEYS(                 XKB_KEY_9, XKB_KEY_parenleft,            8),
	{ MODKEY|SHIFT,          XKB_KEY_E,             quit,             {0} },

	/* Ctrl-Alt-Backspace and Ctrl-Alt-Fx used to be handled by X server */
	{ WLR_MODIFIER_CTRL|WLR_MODIFIER_ALT,XKB_KEY_Terminate_Server, quit, {0} },
	/* Ctrl-Alt-Fx is used to switch to another VT, if you don't know what a VT is
	 * do not remove them.
	 */
#define CHVT(n) { WLR_MODIFIER_CTRL|WLR_MODIFIER_ALT,XKB_KEY_XF86Switch_VT_##n, chvt, {.ui = (n)} }
	CHVT(1), CHVT(2), CHVT(3), CHVT(4), CHVT(5), CHVT(6),
	CHVT(7), CHVT(8), CHVT(9), CHVT(10), CHVT(11), CHVT(12),
};

static const Button buttons[] = {
	{ ClkLtSymbol, 0,      BTN_LEFT,   setlayout,      {.v = &layouts[0]} },
	{ ClkLtSymbol, 0,      BTN_RIGHT,  setlayout,      {.v = &layouts[2]} },
	{ ClkStatus,   0,      BTN_MIDDLE, spawn,          {.v = termcmd} },
	{ ClkClient,   MODKEY, BTN_LEFT,   moveresize,     {.ui = CurMove} },
	{ ClkClient,   MODKEY, BTN_MIDDLE, togglefloating, {0} },
	{ ClkClient,   MODKEY, BTN_RIGHT,  moveresize,     {.ui = CurResize} },
	{ ClkTagBar,   0,      BTN_LEFT,   view,           {0} },
	{ ClkTagBar,   0,      BTN_RIGHT,  toggleview,     {0} },
	{ ClkTagBar,   MODKEY, BTN_LEFT,   tag,            {0} },
	{ ClkTagBar,   MODKEY, BTN_RIGHT,  toggletag,      {0} },
};