-- manual_settings.lua

mainMod = mainMod or "SUPER"

local function bind_exec(keys, cmd, opts)
    hl.bind(keys, hl.dsp.exec_cmd(cmd), opts)
end

-- ##################################################
-- ##  _  __          _                         _  ##
-- ## | |/ /___ _   _| |__   ___   __ _ _ __ __| | ##
-- ## | ' // _ \ | | | '_ \ / _ \ / _` | '__/ _` | ##
-- ## | . \  __/ |_| | |_) | (_) | (_| | | | (_| | ##
-- ## |_|\_\___|\__, |_.__/ \___/ \__,_|_|  \__,_| ##
-- ##           |___/                              ##
-- ##################################################

hl.config({
    input = {
        kb_layout  = "us",
        kb_variant = "",
        kb_model   = "",
        kb_options = "",
        kb_rules   = "",

        numlock_by_default = true,
        follow_mouse       = 1,

        -- ####################################################
        -- ##  _____                _                     _  ##
        -- ## |_   _|__  _   _  ___| |__  _ __   __ _  __| | ##
        -- ##   | |/ _ \| | | |/ __| '_ \| '_ \ / _` |/ _` | ##
        -- ##   | | (_) | |_| | (__| | | | |_) | (_| | (_| | ##
        -- ##   |_|\___/ \__,_|\___|_| |_| .__/ \__,_|\__,_| ##
        -- ##                            |_|                 ##
        -- ####################################################

        touchpad = {
            natural_scroll       = true,
            tap_to_click         = true,
            disable_while_typing = true,
        },

        sensitivity = 0, -- -1.0 - 1.0, 0 means no modification.
    },
})

-- #######################################
-- ## _     _            _              ##
-- ##| |__ | |_   _ _ __(_)_ __   __ _  ##
-- ##| '_ \| | | | | '__| | '_ \ / _` | ##
-- ##| |_) | | |_| | |  | | | | | (_| | ##
-- ##|_.__/|_|\__,_|_|  |_|_| |_|\__, | ##
-- ##                            |___/  ##
-- #######################################

hl.layer_rule({ match = { namespace = "wofi" },            blur = true })
hl.layer_rule({ match = { namespace = "thunar" },          blur = true })
hl.layer_rule({ match = { namespace = "gedit" },           blur = true })
hl.layer_rule({ match = { namespace = "gtk-layer-shell" }, blur = true }) -- for nwg-drawer
hl.layer_rule({ match = { namespace = "catfish" },         blur = true })
hl.layer_rule({ match = { namespace = "nwg-dock" },        blur = true })

-- ##########################################
-- ##                         _ _          ##
-- ##   ___  _ __   __ _  ___(_) |_ _   _  ##
-- ##  / _ \| '_ \ / _` |/ __| | __| | | | ##
-- ## | (_) | |_) | (_| | (__| | |_| |_| | ##
-- ##  \___/| .__/ \__,_|\___|_|\__|\__, | ##
-- ##       |_|                     |___/  ##
-- ##########################################

hl.window_rule({ match = { class = "thunar" },  opacity = "0.85 override 0.85 override" })
hl.window_rule({ match = { class = "gedit" },   opacity = "0.85 override 0.85 override" })
hl.window_rule({ match = { class = "catfish" }, opacity = "0.85 override 0.85 override" })

-- ###########################################
-- ##   __ _             _   _              ##
-- ##  / _| | ___   __ _| |_(_)_ __   __ _  ##
-- ## | |_| |/ _ \ / _` | __| | '_ \ / _` | ##
-- ## |  _| | (_) | (_| | |_| | | | | (_| | ##
-- ## |_| |_|\___/ \__,_|\__|_|_| |_|\__, | ##
-- ##                                |___/  ##
-- ###########################################

-- window rules with evaluation
hl.window_rule({
    name    = "windowrule-1",
    opacity = "0.85 0.85",
    match   = {
        float = true,
    },
})

-- ###########################################
-- ##   __                               _  ##
-- ##  / _| ___   ___ _   _ ___  ___  __| | ##
-- ## | |_ / _ \ / __| | | / __|/ _ \/ _` | ##
-- ## |  _| (_) | (__| |_| \__ \  __/ (_| | ##
-- ## |_|  \___/ \___|\__,_|___/\___|\__,_| ##
-- ###########################################

hl.window_rule({ match = { class = "wofi" }, stay_focused = true })

-- ##################################################
-- ##   __       _ _                               ##
-- ##  / _|_   _| | |___  ___ _ __ ___  ___ _ __   ##
-- ## | |_| | | | | / __|/ __| '__/ _ \/ _ \ '_ \  ##
-- ## |  _| |_| | | \__ \ (__| | |  __/  __/ | | | ##
-- ## |_|  \__,_|_|_|___/\___|_|  \___|\___|_| |_| ##
-- ##################################################

hl.window_rule({
    name       = "windowrule-2",
    fullscreen = true,
    match      = {
        title = "^(nwgbar)$",
        class = "^(nwgbar)$",
    },
})

-- #########################################################
-- ##  _____ _                           _           _    ##
-- ## |  ___| | __ _ _ __ ___   ___  ___| |__   ___ | |_  ##
-- ## | |_  | |/ _` | '_ ` _ \ / _ \/ __| '_ \ / _ \| __| ##
-- ## |  _| | | (_| | | | | | |  __/\__ \ | | | (_) | |_  ##
-- ## |_|   |_|\__,_|_| |_| |_|\___||___/_| |_|\___/ \__| ##
-- #########################################################
-- # if someone wants it by heart
-- but you can edit the picture with all the features
-- in gweenview(KDE image viewer) just same as flameshot

hl.window_rule({
    name  = "windowrule-3",
    move  = "0 0",
    match = {
        title = "^(flameshot)",
    },
})

-- windowrulev2=nofullscreenrequest,title:^(flameshot)

hl.window_rule({ match = { class = "kitty" },                    float = true })
hl.window_rule({ match = { class = "garuda-assistant" },         float = true })
hl.window_rule({ match = { class = "garuda-boot-options" },      float = true })
hl.window_rule({ match = { class = "garuda-boot-repair" },       float = true })
hl.window_rule({ match = { class = "garuda-gamer" },             float = true })
hl.window_rule({ match = { class = "garuda-network-assistant" }, float = true })
hl.window_rule({ match = { class = "garuda-settings-manager" },  float = true })
hl.window_rule({ match = { class = "garuda-welcome" },           float = true })
hl.window_rule({ match = { class = "calamares" },                float = true })

-- Example windowrule v2
-- windowrulev2 = float,class:^(kitty)$,title:^(kitty)$
-- See https://wiki.hyprland.org/Configuring/Window-Rules/ for more

-- #########################################################
-- ##     _          _                 _   _              ##
-- ##    / \   _ __ (_)_ __ ___   __ _| |_(_) ___  _ __   ##
-- ##   / _ \ | '_ \| | '_ ` _ \ / _` | __| |/ _ \| '_ \  ##
-- ##  / ___ \| | | | | | | | | | (_| | |_| | (_) | | | | ##
-- ## /_/   \_\_| |_|_|_| |_| |_|\__,_|\__|_|\___/|_| |_| ##
-- #########################################################

hl.config({
    dwindle = {
        -- See https://wiki.hyprland.org/Configuring/Dwindle-Layout/ for more
        -- pseudotile was removed as a config key in newer Hyprland versions.
        -- Pseudotiling is still toggled with mainMod + P below.
        preserve_split = true, -- you probably want this
    },
})

hl.config({
    master = {
        -- See https://wiki.hyprland.org/Configuring/Master-Layout/ for more
        new_status = "master",
    },
})

hl.gesture({
    fingers   = 3,
    direction = "up",
    action    = function()
        hl.exec_cmd("garuda-toolbox")
    end,
})

hl.gesture({
    fingers   = 3,
    direction = "down",
    action    = function()
        hl.exec_cmd("nwg-drawer -mb 10 -mr 10 -ml 10 -mt 10")
    end,
})

hl.gesture({
    fingers   = 3,
    direction = "horizontal",
    action    = "workspace",
})

-- #######################################################
-- ##  _____                     _   _                  ##
-- ## | ____|_  _____  ___ _   _| |_(_) ___  _ __  ___  ##
-- ## |  _| \ \/ / _ \/ __| | | | __| |/ _ \| '_ \/ __| ##
-- ## | |___ >  <  __/ (__| |_| | |_| | (_) | | | \__ \ ##
-- ## |_____/_/\_\___|\___|\__,_|\__|_|\___/|_| |_|___/ ##
-- #######################################################

hl.on("hyprland.start", function()
    -- network applet
    hl.exec_cmd("nm-applet --indicator")

    -- mako(notifications)
    hl.exec_cmd("mako")
    hl.exec_cmd("/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1")

    -- start foot server
    --
    hl.exec_cmd("foot --server")

    -- pre executions (under development)
    hl.exec_cmd("xrdb -load ~/.Xresources")

    -- Clipboard Manager
    hl.exec_cmd("wl-paste --type text --watch cliphist store")  -- Stores only text data
    hl.exec_cmd("wl-paste --type image --watch cliphist store") -- Stores only image data

    -- Use gtk-settings
    hl.exec_cmd("apply-gsettings")

    -- low battery warning script
    hl.exec_cmd(".local/bin/battery_warn.sh")

    -- ############################
    -- ## ____   ___   ____ _  __##
    -- ##|  _ \ / _ \ / ___| |/ /##
    -- ##| | | | | | | |   | ' / ##
    -- ##| |_| | |_| | |___| . \ ##
    -- ##|____/ \___/ \____|_|\_\##
    -- ############################

    hl.exec_cmd('sleep 10 && nwg-dock-hyprland -x -p "left" -i 24 -mt 10 -mb 10 -ml 5 -f')

    -- #############################################################################
    -- ## _   _                    _____                        _                 ##
    -- ##| | | |_   _ _ __  _ __  | ____|___ ___  ___ _   _ ___| |_ ___ _ __ ___  ##
    -- ##| |_| | | | | '_ \| '__| |  _| / __/ _ \/ __| | | / __| __/ _ \ '_ ` _ \ ##
    -- ##|  _  | |_| | |_) | |    | |__| (_| (_) \__ \ |_| \__ \ ||  __/ | | | | |##
    -- ##|_| |_|\__, | .__/|_|    |_____\___\___/|___/\__, |___/\__\___|_| |_| |_|##
    -- ##       |___/|_|                              |___/                       ##
    -- #############################################################################

    -- hypridle
    hl.exec_cmd("hypridle")
end)

-- Install Garuda Hyprland
bind_exec(mainMod .. " + I", ".local/bin/calamares.sh")

-- #############################################
-- ##  _     _           _ _                  ##
-- ## | |__ (_)_ __   __| (_)_ __   __ _ ___  ##
-- ## | '_ \| | '_ \ / _` | | '_ \ / _` / __| ##
-- ## | |_) | | | | | (_| | | | | | (_| \__ \ ##
-- ## |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___/ ##
-- ##                              |___/      ##
-- #############################################

-- See https://wiki.hyprland.org/Configuring/Keywords/ for more
-- Reaload waybar
bind_exec(mainMod .. " + w", "~/.config/hypr/brain/scripts/waybar_restart.sh")

-- Example binds, see https://wiki.hyprland.org/Configuring/Binds/ for more
bind_exec(mainMod .. " + SHIFT + R", "hyprctl reload")
bind_exec(mainMod .. " + code:36", "footclient")
bind_exec(mainMod .. " + T", "footclient")
hl.bind(mainMod .. " + Q", hl.dsp.window.close())
bind_exec(mainMod .. " + SHIFT + E", "nwgbar")
bind_exec(mainMod .. " + N", "thunar")
hl.bind(mainMod .. " + SHIFT + code:65", hl.dsp.window.float({ action = "toggle" }))
bind_exec("SUPER + D", "pkill wofi || wofi --normal-window --show drun --allow-images", { release = true })
bind_exec(mainMod .. " + E", "emote")
bind_exec(mainMod .. " + SHIFT + D", "nwg-drawer -mb 10 -mr 10 -ml 10 -mt 10")
hl.bind(mainMod .. " + P", hl.dsp.window.pseudo()) -- dwindle
hl.bind(mainMod .. " + SHIFT + P", hl.dsp.layout("togglesplit")) -- dwindle

-- Mainmod + Function keys
bind_exec(mainMod .. " + F1",  "firedragon")
bind_exec(mainMod .. " + F2",  "thunderbird")
bind_exec(mainMod .. " + F3",  "thunar")
bind_exec(mainMod .. " + F4",  "geany")
bind_exec(mainMod .. " + F5",  "github-desktop")
bind_exec(mainMod .. " + F6",  "gparted")
bind_exec(mainMod .. " + F7",  "inkscape")
bind_exec(mainMod .. " + F8",  "blender")
bind_exec(mainMod .. " + F9",  "meld")
bind_exec(mainMod .. " + F10", "joplin-desktop")
bind_exec(mainMod .. " + F11", "snapper-tools")
bind_exec(mainMod .. " + F12", "galculator")

-- Move focus with mainMod + arrow keys
hl.bind(mainMod .. " + left",  hl.dsp.focus({ direction = "l" }))
hl.bind(mainMod .. " + H",     hl.dsp.focus({ direction = "l" }))
hl.bind(mainMod .. " + right", hl.dsp.focus({ direction = "r" }))
hl.bind(mainMod .. " + L",     hl.dsp.focus({ direction = "r" }))
hl.bind(mainMod .. " + up",    hl.dsp.focus({ direction = "u" }))
hl.bind(mainMod .. " + K",     hl.dsp.focus({ direction = "u" }))
hl.bind(mainMod .. " + down",  hl.dsp.focus({ direction = "d" }))
hl.bind(mainMod .. " + J",     hl.dsp.focus({ direction = "d" }))

-- Switch workspaces with mainMod + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind(mainMod .. " + " .. key, hl.dsp.focus({ workspace = i }))
end

-- Move to workspace with focused container with ALT + SHIFT + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind("ALT + SHIFT + " .. key, hl.dsp.window.move({ workspace = i, follow = true }))
end

-- Move active window to a workspace with mainMod + SHIFT + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind(mainMod .. " + SHIFT + " .. key, hl.dsp.window.move({ workspace = i, follow = false }))
end

-- Scroll through existing workspaces with mainMod + scroll
hl.bind(mainMod .. " + mouse_down", hl.dsp.focus({ workspace = "e+1" }))
hl.bind(mainMod .. " + mouse_up",   hl.dsp.focus({ workspace = "e-1" }))

-- Move/resize windows with mainMod + LMB/RMB and dragging
hl.bind(mainMod .. " + mouse:272", hl.dsp.window.drag(),   { mouse = true })
hl.bind(mainMod .. " + mouse:273", hl.dsp.window.resize(), { mouse = true })

-- set volume (laptops only and may or may not work on PCs)
bind_exec("code:122", 'pamixer --decrease 5; notify-send " Volume: "$(pamixer --get-volume) -t 500')
bind_exec("code:123", 'pamixer --increase 5; notify-send " Volume: "$(pamixer --get-volume) -t 500')
bind_exec("code:121", 'pamixer --toggle-mute; notify-send " Volume: Toggle-mute" -t 500')
bind_exec("XF86AudioMicMute", 'pactl set-source-mute @DEFAULT_SOURCE@ toggle; notify-send "System Mic: Toggle-mute" -t 500')

-- other bindings
bind_exec(mainMod .. " + O", "firedragon")
hl.bind(mainMod .. " + M", hl.dsp.window.fullscreen({ mode = "maximized" }))
hl.bind(mainMod .. " + F", hl.dsp.window.fullscreen({ mode = "fullscreen" }))
hl.bind(mainMod .. " + SHIFT + F", hl.dsp.window.fullscreen_state({ internal = 0, client = 2 }))
bind_exec("code:232", "brightnessctl -c backlight set 5%-")
bind_exec("code:233", "brightnessctl -c backlight set +5%")

-- for resizing window
-- will switch to a submap called resize
hl.bind(mainMod .. " + R", hl.dsp.submap("resize"))

-- will start a submap called "resize"
hl.define_submap("resize", function()
    -- sets repeatable binds for resizing the active window
    hl.bind("right", hl.resize({ x = 50,  y = 0,   relative = true }), { repeating = true })
    hl.bind("L",     hl.resize({ x = 50,  y = 0,   relative = true }), { repeating = true })
    hl.bind("left",  hl.resize({ x = -50, y = 0,   relative = true }), { repeating = true })
    hl.bind("H",     hl.resize({ x = -50, y = 0,   relative = true }), { repeating = true })
    hl.bind("up",    hl.resize({ x = 0,   y = -50, relative = true }), { repeating = true })
    hl.bind("K",     hl.resize({ x = 0,   y = -50, relative = true }), { repeating = true })
    hl.bind("down",  hl.resize({ x = 0,   y = 50,  relative = true }), { repeating = true })
    hl.bind("J",     hl.resize({ x = 0,   y = 50,  relative = true }), { repeating = true })

    -- use reset to go back to the global submap
    hl.bind("escape", hl.dsp.submap("reset"))
end)

-- will reset the submap, meaning end the current one and return to the global one
-- submap=reset

-- to move window
hl.bind(mainMod .. " + SHIFT + up",    hl.dsp.window.move({ direction = "u" }))
hl.bind(mainMod .. " + SHIFT + K",     hl.dsp.window.move({ direction = "u" }))
hl.bind(mainMod .. " + SHIFT + down",  hl.dsp.window.move({ direction = "d" }))
hl.bind(mainMod .. " + SHIFT + J",     hl.dsp.window.move({ direction = "d" }))
hl.bind(mainMod .. " + SHIFT + left",  hl.dsp.window.move({ direction = "l" }))
hl.bind(mainMod .. " + SHIFT + H",     hl.dsp.window.move({ direction = "l" }))
hl.bind(mainMod .. " + SHIFT + right", hl.dsp.window.move({ direction = "r" }))
hl.bind(mainMod .. " + SHIFT + L",     hl.dsp.window.move({ direction = "r" }))

-- video play paues bindings
bind_exec("code:172", "playerctl play-pause")
bind_exec("code:171", "playerctl next")
bind_exec("code:173", "playerctl previous")

-- screenshot
-- Snip a selection and pipe to swappy
-- bind_exec("Print", 'grim -g "$(slurp)" - | swappy -f -')
-- Screenshot a window and pipe to swappy
-- bind_exec("CTRL + Print", ".config/hypr/scripts/screenshot_window.sh")
-- Screenshot the current display and pipe to swappy
-- bind_exec("SHIFT + Print", ".config/hypr/scripts/screenshot_display.sh")

bind_exec("Print", "grimblast save screen && notify-send Screenshot captured")
bind_exec("SHIFT + Print", "grimblast save area && notify-send Selected\\ area captured")
bind_exec("ALT + Print", "grimblast save active && notify-send Active\\ window captured")
bind_exec("ALT + SHIFT + Print", "grimblast output active && notify-send Output captured")

bind_exec("ALT + SHIFT + H", "~/.config/hypr/brain/scripts/nwg_dock_toggle.sh")

hl.layer_rule({ match = { namespace = "gtk-layer-shell" }, blur = true })
hl.layer_rule({ match = { namespace = "gtk-layer-shell" }, ignore_alpha = 0.5 })
hl.layer_rule({ match = { namespace = "nwg-dock" },        blur = true })

-- Other layer rules
hl.layer_rule({
    match      = { namespace = ".*nwgbar.*" },
    blur       = true,
    dim_around = false,
})

-- #####################################
-- ##   ___  _   _                    ##
-- ##  / _ \| |_| |__   ___ _ __ ___  ##
-- ## | | | | __| '_ \ / _ \ '__/ __| ##
-- ## | |_| | |_| | | |  __/ |  \__ \ ##
-- ##  \___/ \__|_| |_|\___|_|  |___/ ##
-- #####################################

hl.config({
    misc = {
        disable_hyprland_logo = true,
    },
})

-- Example per-device config
-- See https://wiki.hyprland.org/Configuring/Keywords/#executing for more
-- hl.device({
--     name        = "epic mouse V1",
--     sensitivity = -0.5,
-- })

-- Performance Tweaks (For less power consumption without sacrificing performance)
-- See : https://wiki.hyprland.org/Configuring/Performance/
-- NOTE: misc:vfr causes an "unknown config key" error on newer Hyprland.
-- If you need it, use debug.vfr only after verifying your Hyprland version supports it:
--
-- hl.config({
--     debug = {
--         vfr = true,
--     },
-- })

-- some env variables
hl.env("XDG_CURRENT_DESKTOP", "Hyprland")
-- we usually prefer things to be set up through script
-- have a look at hyprstart file

-- ########################################################################################
-- ##                      _       _                      _                              ##
-- ##  ___ _ __   ___  ___(_) __ _| | __      _____  _ __| | _____ _ __   __ _  ___ ___  ##
-- ## / __| '_ \ / _ \/ __| |/ _` | | \ \ /\ / / _ \| '__| |/ / __| '_ \ / _` |/ __/ _ \ ##
-- ## \__ \ |_) |  __/ (__| | (_| | |  \ V  V / (_) | |  |   <\__ \ |_) | (_| | (_|  __/ ##
-- ## |___/ .__/ \___|\___|_|\__,_|_|   \_/\_/ \___/|_|  |_|\_\___/ .__/ \__,_|\___\___| ##
-- ##     |_|                                                     |_|                    ##
-- ########################################################################################

hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.window.move({ workspace = "+0", follow = true }))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.window.move({ workspace = "special:magic", follow = true }))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.workspace.toggle_special("magic"))
