{
  config,
  lib,
  pkgs,
  ...
}: 
with config.lib.stylix.colors; {
  xsession.windowManager.xmonad = {
    enable = true;
    enableContribAndExtras = true;
  };

  xsession.windowManager.xmonad.config =
    pkgs.writeText "xmonad.hs" ''
    import XMonad

    import XMonad.Config.Desktop

    import XMonad.Layout.ResizableTile
    import XMonad.Layout.NoBorders
    import XMonad.Layout.Gaps
    import XMonad.Layout.Spacing
    import XMonad.Layout.PerScreen
    
    import XMonad.Util.EZConfig
    import XMonad.Util.Ungrab
    import XMonad.Util.Loggers

    import XMonad.Hooks.EwmhDesktops 
    import XMonad.Hooks.DynamicLog
    import XMonad.Hooks.ManageDocks
    import XMonad.Hooks.ManageHelpers
    import XMonad.Hooks.StatusBar
    import XMonad.Hooks.StatusBar.PP

    import System.Exit

    import qualified XMonad.StackSet as W

    mobarStart = "xmobar ~/.config/xmobar/xmobarrc"

    main :: IO()
    main = xmonad
        . ewmhFullscreen
        . ewmh
        . docks
        . withEasySB mySB defToggleStrutsKey
        $ myConfig
      where
        mySB = statusBarProp mobarStart (pure myXmobarPP)      

    myConfig = def
        { modMask = mod4Mask
        , terminal = "alacritty"

        -- Border
        , borderWidth = 1
        , normalBorderColor = "#${base03}"
        , focusedBorderColor = "#${base0B}"

        -- Hooks
        , manageHook = myHook
        -- , layoutHook = desktopLayoutModifiers $ smartBorders myLayout
        , layoutHook = myLayout
        -- , startupHook = myStartup
        }
      `additionalKeysP`
        [ ("M-b", spawn "chromium")
        , ("M-<Return>", spawn "alacritty")
        , ("M-d", spawn "rofi -show drun")

        -- Windows
        , ("M-S-<Return>", windows W.swapMaster)
        , ("M-S-q", kill)
        , ("M-S-x", io (exitWith ExitSuccess))

        -- Media keys
        , ("<XF86AudioMute>", spawn "pamixer -t")
        , ("<XF86AudioLowerVolume>", spawn "pamixer -d2")
        , ("<XF86AudioRaiseVolume>", spawn "pamixer -i2")
        ]

    myLayout = avoidStruts $ lessBorders Screen $ smartBorders $ tall ||| Full
      where
        addSpaces = spacingRaw True (Border 2 2 2 2) True (Border 2 2 2 2) True
        -- addGaps = gaps [(U,10),(R,10),(L,10),(D,10)]
        -- tall = ResizableTall 2 (1/10) 1 []
        tall = ifWider 1280 (tiled) (Mirror tiled)
        tiled = Tall nmaster delta ratio
        nmaster = 1
        ratio   = 1/2
        delta   = 3/100

    myHook :: ManageHook
    myHook = composeAll
      [ isDialog --> doFloat
      , isFullscreen --> (doF W.focusDown <+> doFullFloat)
      ]

    -- myStartup :: X ()
    -- myStartup = do
    --       spawn mobarStart
    
    myXmobarPP :: PP
    myXmobarPP = def
        { ppSep             = magenta " • "
        , ppTitleSanitize   = xmobarStrip
        , ppCurrent         = wrap " " "" . xmobarBorder "Top" "#${base0C}" 2
        , ppHidden          = white . wrap " " ""
        , ppHiddenNoWindows = lowWhite . wrap " " ""
        , ppUrgent          = red . wrap (yellow "!") (yellow "!")
        , ppOrder           = \[ws, l, _, wins] -> [ws, l, wins]
        , ppExtras          = [logTitles formatFocused formatUnfocused]
        }
      where
        formatFocused   = wrap (white    "[") (white    "]") . magenta . ppWindow
        formatUnfocused = wrap (lowWhite "[") (lowWhite "]") . blue    . ppWindow

        -- | Windows should have *some* title, which should not not exceed a
        -- sane length.
        ppWindow :: String -> String
        ppWindow = xmobarRaw . (\w -> if null w then "untitled" else w) . shorten 30

        blue, lowWhite, magenta, red, white, yellow :: String -> String
        magenta  = xmobarColor "#${base0E}" ""
        blue     = xmobarColor "#${base0D}" ""
        white    = xmobarColor "#${base05}" ""
        yellow   = xmobarColor "#${base0A}" ""
        red      = xmobarColor "#${base08}" ""
        lowWhite = xmobarColor "#${base04}" ""
  '';

  programs.xmobar.enable = true;
  programs.xmobar.extraConfig = ''
  Config 
    { overrideRedirect = False
    , font     = "Mononoki"
    , bgColor  = "#${base00}"
    , fgColor  = "#${base05}"
    , position = BottomH 20
    , lowerOnStart = True
    , commands = 
      [ Run Cpu
          [ "-L", "3"
          , "-H", "50"
          , "--high"  , "red"
          , "--normal", "green"
          ] 10
      , Run Alsa "default" "Master"
          [ "--template", "Vol: <volumestatus>"
          , "--suffix"  , "True"
          , "--"
          , "--on", ""
          ]
      , Run Memory ["--template", "Mem: <usedratio>%"] 10
      , Run Swap [] 10
      , Run Date "%Y-%m-%d %a <fc=#${base0C}>%H:%M:%S</fc>" "date" 1
      , Run XMonadLog
      ]
    , sepChar  = "%"
    , alignSep = "}{"
    , template = "%XMonadLog% }{ %cpu% | %memory% | %swap% | %date% "
    }
  '';
}
