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))

main :: IO ()
main = do
    xmproc <- spawnPipe "xmobar ~/.xmobarrc"
    xmonad $ def
        -- Fullscreen handling:
        -- https://stackoverflow.com/questions/20446348/xmonad-toggle-fullscreen-xmobar
        { manageHook = manageDocks <+> (isFullscreen --> doFullFloat) <+> (className =? "trayer" --> doIgnore) <+> manageHook def
        , handleEventHook = handleEventHook def <+> docksEventHook
        , layoutHook = myLayoutHook
        , modMask = mod4Mask -- User Super instead of Alt
        , terminal = myTerminal
        , startupHook = myStartup
        } `additionalKeysP` myKeyPBindings

myTerminal :: String
myTerminal = "gnome-terminal"

dbg :: String
dbg = "xmessage 'xmonad EuAndreh debug'"

myKeyPBindings :: [(String, X ())]
myKeyPBindings =
    [ -- Volume
      ("<F1>", spawn "amixer -q sset Master toggle")
    , ("<F2>", spawn "amixer -q sset Master 5%- unmute")
    , ("<F3>", spawn "amixer -q sset Master 5%+ unmute")

      -- Playback
    , ("<F4>",   spawn "playerctl --player=$(cat ~/.cache/andreh/mpris-player.txt) position 5-")
    , ("S-<F4>", spawn "playerctl --player=$(cat ~/.cache/andreh/mpris-player.txt) previous")
    , ("<F5>",   spawn "playerctl --player=$(cat ~/.cache/andreh/mpris-player.txt) play-pause")
    , ("S-<F5>", spawn "~/dev/libre/dotfiles/scripts/ad-hoc/rotate-mpris-player.sh")
    , ("<F6>",   spawn "playerctl --player=$(cat ~/.cache/andreh/mpris-player.txt) position 5+")
    , ("S-<F6>", spawn "playerctl --player=$(cat ~/.cache/andreh/mpris-player.txt) next")

      -- Brightness
    , ("<F7>",   spawn "~/dev/libre/dotfiles/scripts/ad-hoc/brightness.sh -10")
    , ("S-<F7>", spawn "~/dev/libre/dotfiles/scripts/ad-hoc/zero-brightness.sh")
    , ("<F8>",   spawn "~/dev/libre/dotfiles/scripts/ad-hoc/brightness.sh +10")


      -- yeganesh launcher
    , ("M-p", spawnHere "exe=$(yeganesh -x) && exec $exe")

      -- yeganesh emoji
    , ("M-o", spawnHere "~/dev/libre/dotfiles/scripts/ad-hoc/dmenu-emoji.sh")

      -- Lock screen
    , ("C-M-l", spawn "dm-tool switch-to-greeter")

      -- print whole screen
    , ("M-<Print>", spawn "escrotum ~/Bureau/%Y-%m-%d-%T-screenshot.png")
      -- print screen of clicked window
    , ("C-M-<Print>", spawn "escrotum ~/Bureau/%Y-%m-%d-%T-screenshot.png -s")

      -- xrandr single monitor setup
    , ("C-M-1", spawn "~/dev/libre/dotfiles/scripts/ad-hoc/single-monitor.sh")

      -- xrandr three monitor setup
    , ("C-M-3", spawn "~/dev/libre/dotfiles/scripts/ad-hoc/three-monitors.sh")

      -- restart network
    , ("M-x", spawn "gksudo -S systemctl restart NetworkManager")

      -- Toggle appearence of xmobar
      -- https://stackoverflow.com/a/27646652
    , ("M-f", sendMessage ToggleStruts)

    , ("M-ç", spawn dbg)

      -- 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"
