import           XMonad hiding ((|||))
import           XMonad.Core

import           XMonad.Hooks.DynamicLog hiding (dzen)
import           XMonad.Hooks.ManageDocks
import           XMonad.Hooks.FadeInactive (fadeInactiveLogHook)

import           XMonad.Layout hiding ((|||))
import           XMonad.Layout.WindowNavigation
import           XMonad.Layout.NoBorders
import           XMonad.Layout.ResizableTile
import           XMonad.Layout.SimpleFloat
import           XMonad.Layout.Grid
import           XMonad.Layout.IM
import           XMonad.Layout.LayoutCombinators
import           XMonad.Layout.PerWorkspace
import           XMonad.Layout.LayoutHints

import qualified XMonad.Actions.GridSelect as GridSet

import           Data.Char (toLower)
import           Data.List (isInfixOf)
import           Data.Ratio ((%))
import           Text.Printf
import           XMonad.Hooks.EwmhDesktops (ewmh)
import           XMonad.Hooks.ManageHelpers (doFullFloat, isFullscreen)
import qualified XMonad.StackSet as W
import           XMonad.Util.EZConfig
import           XMonad.Util.NamedScratchpad

import           XMonad.Actions.CopyWindow

import           XMonad.Prompt
import           XMonad.Prompt.Shell
import           XMonad.Prompt.RunOrRaise

import           XMonad.Config.Kde (kde4Config)

myWorkspaces = ["code","www","3","fm","bo","dia","7","tty","im"]

scratchpads = [ termScratchPad "mpsyt" "mpsyt" defaultFloating
              , termScratchPad "htop" "htop" defaultFloating
              , termScratchPad "alsamixer" "alsamixer" defaultFloating
              , NS "goldendict" "goldendict" (className =? "GoldenDict") defaultFloating
              ]

termScratchPad name cmd layout = NS name
                                    -- (printf "%s -t %s -e %s" myTerminal name cmd) -- Mate-terminal
                                    (printf "%s -p tabtitle=%s -e %s" myTerminal name $ cmd) -- Konsole
                                    (fmap (isInfixOf name . map toLower) title) layout

myManageHook = composeAll
               [ className =? "Mate-panel"     --> doFloat
               , className =? "Smplayer"       --> doFloat
               , className =? "Goldendict"     --> doFloat
               , className =? "Firefox"        --> doShift "www"
               , className =? "Firefox-esr"    --> doShift "www"
               , className =? "Navigator"      --> doShift "www"
               , className =? "Okular"         --> doShift "bo"
               , className =? "Mendeleydesktop.x86_64"  --> doShift "bo"
               , className =? "Qtcreator"      --> doShift "code"
               , className =? "Evolution"      --> doShift "im"
               , className =? "Workrave"       --> doF copyToAll
               , className =? "Clementine"     --> doFloat
               , className =? "Slack"          --> doShift "im"
               , title     =? "kred_cluster"   --> doShift "tty"
               , isFullscreen --> (doF W.focusDown <+> doFullFloat)
               -- Desperate attempts to fix plasma:
               , wmName    =? "Desktop"        --> doFloat
               , className =? "plasmashell"    --> doFloat
               ]
    where wmRole = stringProperty "WM_WINDOW_ROLE"
          wmName = stringProperty "WM_NAME"
          -- isNotification = do
          --   wm_type <- getAtom "_NET_WM_WINDOW_TYPE"
          --   notification <- getAtom "_NET_WM_WINDOW_TYPE_NOTIFICATION"

          -- wmType = stringProperty ""

deltaL = 3/100
defaultSize = 1/2

zoom = 1.3

--layoutIM = withIM (1%7) (Role "MainWindow#1") Full
--layoutIM = withIM (1%7) (Title "Gwenview") Full
layoutIM = myLayout

layoutTall = Tall 1 deltaL defaultSize

myLayout =      layoutTall
            ||| (Mirror $ layoutTall)
            ||| Full
            ||| simpleFloat

bgColor1 = "#1D1F21"
fgColor1 = "#93AEC6"

bgColor2 = "#373B41"
fgColor2 = "#9B94BB"

myTerminal :: String
-- myTerminal = "mate-terminal"
myTerminal = "konsole"

spawnTerm a = spawn $ myTerminal ++ " -e " ++ a

--screenShot = spawn "scrot '%Y-%m-%d_$wx$h.png' -e 'mv $f ~/shots/'"

myKeybinds = [ ("M-<Shift>-<Return>", spawn myTerminal)
             , ("M-z", GridSet.goToSelected GridSet.def)
             , ("M-c", spawn myDmenu)
             -- Spawn things:
             , ("M-s M-d", namedScratchpadAction scratchpads "goldendict")
             -- ,("M-s  M-s", screenShot)
             , ("M-s M-e", spawn "emacs")
             , ("M-s M-t", spawn orgAgenda)
             , ("M-s M-m", namedScratchpadAction scratchpads "mpsyt")
             , ("M-s M-h", namedScratchpadAction scratchpads "htop")
             , ("M-s M-a", namedScratchpadAction scratchpads "alsamixer")
             , ("M-s M-k", spawn "kcharselect")
             -- , ("<XF86AudioNext>", spawn "mpc next")
             -- , ("<XF86AudioPrev>", spawn "mpc prev")
             -- , ("<XF86AudioPlay>", spawn "mpc toggle")
             -- 2D window navigation:
             , ("M-<R>", sendMessage $ Go R)
             , ("M-<L>", sendMessage $ Go L)
             , ("M-<U>", sendMessage $ Go U)
             , ("M-<D>", sendMessage $ Go D)
             , ("M-s M-s", spawn "rofi -show ssh")
             , ("M-q", spawn "quote")
             ] ++
             [ (otherModMasks ++ "M-" ++ [key], action tag)
                   | (tag, key)  <- zip myWorkspaces ['1' .. '9']
             , (otherModMasks, action) <- [ ("", windows . W.view)
                                          , ("S-", windows . W.shift)
                                          , ("C-", windows . W.greedyView)
                                          ]
             ]

myConf = kde4Config
         { terminal = myTerminal
         , workspaces = myWorkspaces
         , focusFollowsMouse = True
         , modMask = mod4Mask
         , borderWidth = 2
         , normalBorderColor = bgColor1
--         , focusedBorderColor = "#CCAA11"
         , startupHook = spawn "compton"
         , manageHook = namedScratchpadManageHook scratchpads
                          <+> myManageHook <+> manageHook kde4Config
         , layoutHook = avoidStruts . smartBorders . windowNavigation $
                               onWorkspace "bo" (Full ||| layoutTall) $
                               onWorkspace "im" layoutIM $
                               onWorkspace "tty" (Grid ||| layoutTall) $
                               onWorkspace "3" (Grid ||| layoutTall)
                               myLayout

         --, logHook = fadeInactiveLogHook 0xf0000000
         } `additionalKeysP` myKeybinds

main = xmonad $ docks $ ewmh myConf

myDmenu :: String
myDmenu = "LANG=en_US.utf8 rofi -show run"
-- myDmenu = printf "$(yeganesh -x -- -b -nb '%s' -nf '%s' -sb '%s' -sf '%s')"
--                  bgColor1 fgColor1 bgColor2 fgColor2

orgAgenda = "emacsclient -c --eval '(let ((org-agenda-window-setup `only-window)) (org-todo-list))'"
