import XMonad
import XMonad.Actions.SpawnOn(spawnHere)
import XMonad.Hooks.ManageDocks
import XMonad.Util.EZConfig(additionalKeysP)
import XMonad.Layout.NoBorders
import XMonad.Hooks.SetWMName
import XMonad.Hooks.ManageHelpers
import XMonad.Util.Run(spawnPipe)
import XMonad.Layout.ResizableTile
import XMonad.Layout.ResizableTile(MirrorResize(MirrorShrink, MirrorExpand))
import XMonad.StackSet
import XMonad.Hooks.EwmhDesktops(ewmh, ewmhFullscreen)

main :: IO ()
main = do
    xmproc <- spawnPipe "xmobar"
    xmproc <- spawnPipe "blueman-applet"
    xmproc <- spawnPipe "xss-lock i3lock"
    xmproc <- spawnPipe "feh --bg-scale ~/Pictures/emotion.png"
    xmonad $ ewmhFullscreen . ewmh $ docks $ def
        { manageHook = manageDocks <+> manageHook def
        , layoutHook = myLayoutHook
        , modMask = mod4Mask
        , terminal = "st"
        , startupHook = myStartup
        } `additionalKeysP` myKeyPBindings

myKeyPBindings :: [(String, X ())]
myKeyPBindings =
    [
      ("<F1>",      spawn "volume toggle")
    , ("<F2>",      spawn "volume down")
    , ("<F3>",      spawn "volume up")
    , ("M-S-s",     spawn "volume rotate")

    , ("<F4>",      spawn "player backward")
    , ("S-<F4>",    spawn "player previous")
    , ("<F5>",      spawn "player play-pause")
    , ("S-<F5>",    spawn "player rotate")
    , ("<F6>",      spawn "player forward")
    , ("S-<F6>",    spawn "player next")

    , ("<F7>",      spawn "brightnessctl -- set -10%")
    , ("<F8>",      spawn "brightnessctl -- set +10%")

    , ("M-c",       spawn "mate-calc")
    , ("M-u",       spawn "wms uuid")
    , ("M-t",       spawn "wms date")
    , ("M-m",       spawn "wms clear-notification")
    , ("M-p",       spawn "menu bin")
    , ("M-o",       spawn "menu emoji")
    , ("M-v",       spawn "menu clipboard")
    , ("M-i",       spawn "menu password")
    , ("C-M-i",     spawn "menu username")
    , ("C-M-S-i",   spawn "menu yubikey")

    , ("<Print>",   spawn "shot")
    , ("M-<Print>", spawn "shot -m")

      -- Laptop setup: single monitor or multiple monitors
    , ("C-M-1",     spawn "with-one-monitor")
    , ("C-M-2",     spawn "with-two-monitors")

      -- Stop floating
    , ("M-S-<Space>", withFocused $ windows . XMonad.StackSet.sink)

      -- Lock screen
    , ("C-M-l", spawn "loginctl lock-session")

      -- Toggle appearence of xmobar
      -- https://stackoverflow.com/a/27646652
    , ("M-f", sendMessage ToggleStruts)
    , ("M-ç", spawn "xmessage 'xmonad EuAndreh debug'")

      -- Vertically resize, taken from:
      -- https://stackoverflow.com/questions/25857674/xmonad-vertical-resize-tile-window
    , ("M-a",  sendMessage MirrorExpand)
    , ("M-z",  sendMessage MirrorShrink)
    ]

myLayoutHook =
  smartBorders $ avoidStruts $ (tiled ||| Mirror tiled ||| Full)
  where
    tiled = ResizableTall nmaster delta ratio []
    nmaster = 1
    delta = 3/100
    ratio = 1/2

myStartup :: X ()
myStartup = do
  -- Taken from:
  -- https://stackoverflow.com/questions/30742662/java-swing-gui-not-displaying-in-xmonad
  setWMName "LG3D"
