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

import qualified Data.Map as M
import Graphics.X11.ExtraTypes.XF86
import System.IO
import XMonad
import XMonad.Actions.CopyWindow (kill1, copyToAll, killAllOtherCopies)
import XMonad.Actions.CycleWS
import qualified XMonad.Actions.FlexibleResize as Flex
import XMonad.Actions.GridSelect (goToSelected, defaultGSConfig)
import XMonad.Actions.Search (promptSearchBrowser, selectSearchBrowser, google)
import XMonad.Hooks.DynamicLog
import XMonad.Hooks.FadeWindows
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
import XMonad.Hooks.SetWMName
import XMonad.Layout.Gaps
-- import XMonad.Layout.Grid (Grid(..))
import XMonad.Layout.IndependentScreens (countScreens, withScreens, onCurrentScreen)
import XMonad.Layout.Minimize (minimize, minimizeWindow, MinimizeMsg(..))
import XMonad.Layout.NoBorders
import XMonad.Layout.ResizableTile (ResizableTall(..), MirrorResize(..))
import XMonad.Layout.Simplest (Simplest(..))
import XMonad.Layout.Spacing
import XMonad.Layout.StackTile (StackTile(..))
-- import XMonad.Layout.ThreeColumns (ThreeCol(..))
import XMonad.Layout.ToggleLayouts
import XMonad.Layout.TwoPane (TwoPane(..))
import XMonad.Prompt
import XMonad.Prompt.RunOrRaise (runOrRaisePrompt)
import XMonad.Prompt.Shell (shellPrompt)
import XMonad.Prompt.Ssh (sshPrompt)
import qualified XMonad.StackSet as W
import XMonad.Util.EZConfig (additionalKeys, removeKeys)
import XMonad.Util.NamedScratchpad
import XMonad.Util.Run (spawnPipe)
import XMonad.Util.WorkspaceCompare (getSortByIndex)

-- 1. local variables

myWorkspaces :: [String]
myWorkspaces = map show [1..5]

modm :: KeyMask
modm = mod4Mask

-- Color Setting
colorBlue :: String
colorBlue = "#9fc7e8"

colorGreen :: String
colorGreen = "#2ABB9B"

colorRed :: String
colorRed = "#CF000F"

colorGray :: String
colorGray = "#9e9e9e"

colorWhite :: String
colorWhite = "#ffffff"

colorGrayAlt :: String
colorGrayAlt = "#eceff1"

colorNormalbg :: String
colorNormalbg = "#1c1c1c"

colorfg :: String
colorfg = "#ffffff"

-- Border width
borderwidth :: Dimension
borderwidth = 3

-- Border color
mynormalBorderColor :: String
mynormalBorderColor  = colorNormalbg
myfocusedBorderColor :: String
myfocusedBorderColor = colorGreen

-- gapwidth
gapwidth :: Int
gapwidth  = 4
gapwidthU :: Int
gapwidthU = 2
gapwidthD :: Int
gapwidthD = 2
gapwidthL :: Int
gapwidthL = 2
gapwidthR :: Int
gapwidthR = 2

myTerminal :: String
myTerminal = "urxvtc"

-- 2. scratchpad settings

myScratchpads :: [NamedScratchpad]
myScratchpads = [
    NS "qurxvt"  "urxvtc -name qurxvt" (appName =? "qurxvt")
        (customFloating $ W.RationalRect 0 0.02 1 0.48)
  , NS "qurxvt2"  "urxvtc -name qurxvt2" (appName =? "qurxvt2")
        (customFloating $ W.RationalRect 0 0.5 1 0.48)
  , NS "htop"   "urxvtc -e htop" (title =? "htop")
        (customFloating $ W.RationalRect 0 0.02 1 0.4)
  , NS "ranger"   "urxvtc -e ranger" (title =? "ranger")
        (customFloating $ W.RationalRect 0 0.02 1 0.6)
  , NS "chrome" "google-chrome-stable" (className =? "google-chrome")
        (customFloating $ W.RationalRect 0 0.02 1 0.98)
  , NS "firefox" "firefox" (className =? "Firefox")
        (customFloating $ W.RationalRect 0 0.02 1 0.98)
 ]

-- 3. keybindings

-- shiftAndView dir = findWorkspace getSortByIndexNoSP dir HiddenNonEmptyWS 1
--                    >>= \t -> (windows . W.shift $ t) >> (windows . W.greedyView $ t)
--     where notSP = (return $ ("SP" /=) . W.tag) :: X (WindowSpace -> Bool)
--           getSortByIndexNoSP = fmap (.namedScratchpadFilterOutWorkspace) getSortByIndex
--           myToggle = windows $ W.view =<< W.tag . head . filter
--                      ((\x -> x /= "NSP" && x /= "SP") . W.tag) . W.hidden

notSP :: X (WindowSpace -> Bool)
notSP = return $ ("NSP" /=) . W.tag

nextWS' :: X ()
nextWS' = moveTo Next (WSIs notSP)
prevWS' :: X ()
prevWS' = moveTo Prev (WSIs notSP)

myKeyBinding :: [((KeyMask, KeySym), X ())]
myKeyBinding = [
    -- Shrink / Expand the focused window
    ((modm                , xK_comma  ), sendMessage Shrink)
  , ((modm                , xK_period ), sendMessage Expand)
  , ((modm                , xK_z      ), sendMessage MirrorShrink)
  , ((modm                , xK_a      ), sendMessage MirrorExpand)
  -- Close the focused window
  , ((modm                , xK_c      ), kill1)
  -- Toggle layout (Fullscreen mode)
  , ((modm                , xK_f      ), sendMessage ToggleLayout)

  -- Go to the next / previous workspace
  , ((modm                , xK_Down ), nextWS' )
  , ((modm                , xK_Up   ), prevWS' )
  -- , ((modm                , xK_l      ), nextWS )
  -- , ((modm                , xK_h      ), prevWS )
  -- Shift the focused window to the next / previous workspace
  , ((modm .|. shiftMask  , xK_Down ), shiftToNext)
  , ((modm .|. shiftMask  , xK_Up   ), shiftToPrev)
  -- , ((modm .|. shiftMask  , xK_l      ), shiftToNext)
  -- , ((modm .|. shiftMask  , xK_h      ), shiftToPrev)
  -- CopyWindow
  , ((modm                , xK_v      ), windows copyToAll)
  , ((modm .|. shiftMask  , xK_v      ), killAllOtherCopies)
  -- Move the focus down / up
  , ((modm                , xK_j      ), windows W.focusDown)
  , ((modm                , xK_k      ), windows W.focusUp)
  -- Swap the focused window down / up
  , ((modm .|. shiftMask  , xK_j      ), windows W.swapDown)
  , ((modm .|. shiftMask  , xK_k      ), windows W.swapUp)

  -- Google Search
  , ((modm                  , xK_g    ), promptSearchBrowser myXPConfig2 "/usr/bin/google-chrome-stable" google)
  , ((modm .|. shiftMask    , xK_g    ), selectSearchBrowser "/usr/bin/google-chrome-stable" google)

-- Open SSH
  , ((modm .|. shiftMask    , xK_s    ), sshPrompt myXPConfig)
  , ((modm .|. controlMask  , xK_s    ), shellPrompt myXPConfig2)

  , ((0                     , xK_F12),   namedScratchpadAction myScratchpads "qurxvt")
  , ((0                     , xK_F11),   namedScratchpadAction myScratchpads "qurxvt2")
  , ((modm                  , xK_h),     namedScratchpadAction myScratchpads "htop")
  , ((modm .|. shiftMask    , xK_f),     namedScratchpadAction myScratchpads "firefox")
  , ((modm .|. shiftMask    , xK_r),     namedScratchpadAction myScratchpads "ranger")
  , ((modm                  , xK_b),     namedScratchpadAction myScratchpads "chrome")
  , ((modm                  , xK_e),     spawn "emacs")

  -- RunOrRaisePrompt
  , ((modm                  , xK_r    ), runOrRaisePrompt myXPConfig)

  -- Grid Select Binding
  , ((modm                , xK_Tab), goToSelected defaultGSConfig)

  , ((modm                , xK_m), withFocused minimizeWindow)
  , ((modm .|. shiftMask  , xK_m), sendMessage RestoreNextMinimizedWin)

  , ((modm                , xK_F7), spawn "xset dpms force suspend")
  , ((0                   , xF86XK_MonBrightnessDown), spawn "xbacklight -10")
  , ((0                   , xF86XK_MonBrightnessUp),   spawn "xbacklight +10")
  , ((0                   , xF86XK_AudioLowerVolume),  spawn "pactl set-sink-volume 0 -1.5%")
  , ((0                   , xF86XK_AudioRaiseVolume),  spawn "pactl set-sink-volume 0 +1.5%")
  , ((0                   , xF86XK_AudioMute),         spawn "pactl set-sink-mute 0 toggle")
  , ((0                   , xK_Print),   spawn "shutter")
  ]

-- 4. layouts

myLayout = spacing gapwidth $
           gaps [(U,gapwidth + gapwidthU),(D,gapwidth + gapwidthD),(L,gapwidth + gapwidthL),(R,gapwidth + gapwidthR)] $
                 minimize (ResizableTall 1 (1/100) (3/5) []
             ||| StackTile 1 (1/100) (3/5)
             ||| TwoPane (1/100) (3/5)
--             ||| ThreeColMid 1 (1/100) (16/35)
             ||| Simplest)


-- 5. startup programms

startup' :: [String] -> X ()
startup' = mapM_ spawn

myStartupHook :: X ()
myStartupHook = startup' myStartup >> setWMName "LG3D"

myStartup :: [String]
myStartup = [
    "xsetroot -solid black"
  , "feh --bg-fill ~/Pictures/monochrome.jpg"
  , "xsetroot -cursor_name left_ptr"
  , "setxkbmap -layout jp"
  , "xset r rate 300 45"
  , "xset dpms 0 0 0"
  , "xset s off"
  , "xset dpms force on"
  , "xautolock -detectsleep -time 10 -locker 'dm-tool lock'"
  , "syndaemon -t -k -i 3 -d"
  , "urxvtd -o -f -q"
  , "sudo hciconfig hci0 up"
  , "fcitx"
  , "xcompmgr"
  , "$HOME/.dropbox-dist/dropboxd"
  ]

-- 6. manage hooks

myManageHookShift :: ManageHook
myManageHookShift = composeAll []


myManageHookFloat :: ManageHook
myManageHookFloat = composeAll
                    [ className =? "Gimp"             --> doFloat
                    , className =? "MPlayer"          --> doCenterFloat
                    , className =? "SMPlayer"         --> doCenterFloat
                    , className =? "Shutter"          --> doCenterFloat
                    , className =? "Blueman-manager"  --> doCenterFloat
                    , className =? "Zeal"  --> doCenterFloat
                    ]


myLogHook :: Handle -> X ()
myLogHook h = dynamicLogWithPP . namedScratchpadFilterOutWorkspacePP $ wsPP { ppOutput = hPutStrLn h }

-- 7. xmobar setting                                        {{{

myWsBar :: String
myWsBar = "xmobar $HOME/.xmonad/xmobarrc"

wsPP :: PP
wsPP = xmobarPP { ppOrder           = \(ws:_:t:_)  -> [ws,t]
                , ppCurrent         = xmobarColor colorRed   colorNormalbg . const "*"
                , ppUrgent          = xmobarColor colorfg    colorNormalbg . const "*"
                , ppVisible         = xmobarColor colorfg    colorNormalbg . const "*"
                , ppHidden          = xmobarColor colorfg    colorNormalbg . const "*"
                , ppHiddenNoWindows = xmobarColor colorfg    colorNormalbg . const "-"
                , ppTitle           = xmobarColor colorGreen colorNormalbg
                , ppSort            = fmap (.namedScratchpadFilterOutWorkspace) getSortByIndex
                , ppOutput          = putStrLn
                , ppWsSep           = " "
                , ppSep             = " : "
                }

-- 8. XPConfig

myXPConfig :: XPConfig
myXPConfig = defaultXPConfig
                { font              = "xft:Ricty:size=12:antialias=true"
                , fgColor           = colorfg
                , bgColor           = colorNormalbg
                , borderColor       = colorNormalbg
                , height            = 20
                , promptBorderWidth = 1
                , autoComplete      = Just 100000
                , bgHLight          = colorWhite
                , fgHLight          = colorNormalbg
                , position          = Bottom
                }

myXPConfig2 :: XPConfig
myXPConfig2 = defaultXPConfig
                { font              = "xft:Ricty:size=12:antialias=true"
                , fgColor           = colorfg
                , bgColor           = colorNormalbg
                , borderColor       = colorNormalbg
                , height            = 20
                , promptBorderWidth = 1
                , autoComplete      = Nothing
                , bgHLight          = colorWhite
                , fgHLight          = colorNormalbg
                , position          = Top
                }

--9. mouse bindings

myMouse :: t -> [((KeyMask, Button), Window -> X ())]
myMouse _ = [ ((modm, button3), \w -> focus w >> Flex.mouseResizeWindow w) ]
newMouse :: XConfig Layout -> M.Map (ButtonMask, Button) (Window -> X ())
newMouse x = M.union (mouseBindings defaultConfig x) (M.fromList (myMouse x))

-- 10. Define keys to remove

myRemoveKey :: [(KeyMask, KeySym)]
myRemoveKey = [
        (modm, xK_p)
      , (modm .|. shiftMask, xK_p)
 ]



------------------------------------------------------------------------------------------------
-- 11 main
------------------------------------------------------------------------------------------------

main :: IO ()
main = do
    nScreens <- countScreens
    wsbar <- spawnPipe myWsBar
    xmonad $ defaultConfig
       { borderWidth        = borderwidth
       , terminal           = myTerminal
       , focusFollowsMouse  = False
       , normalBorderColor  = mynormalBorderColor
       , focusedBorderColor = myfocusedBorderColor
       , startupHook        = myStartupHook
       , manageHook         = myManageHookShift
                              <+> myManageHookFloat
                              <+> namedScratchpadManageHook myScratchpads
                              <+> manageDocks
       , layoutHook         = lessBorders OnlyFloat $
                              toggleLayouts (avoidStruts $ noBorders Full) $
                              avoidStruts myLayout
        -- xmobar setting
       , logHook            = myLogHook wsbar
       , handleEventHook    = fadeWindowsEventHook
       , workspaces         = withScreens nScreens myWorkspaces
       , modMask            = modm
       , mouseBindings      = newMouse
       }

       `additionalKeys` myKeyBinding
       `additionalKeys`
       [ ((modm .|. m, k), windows $ onCurrentScreen f i)
         | (i, k) <- zip myWorkspaces [xK_1 .. xK_9]
         , (f, m) <- [(W.greedyView, 0), (W.shift, shiftMask)]
       ]
       `removeKeys` myRemoveKey