from libqtile.config import Key, Drag, Click
from libqtile.command import lazy

from defines import mod, term, ChassisType, chassis_type

keys = [
    #screen lock & hibernation
    Key([mod], "End" , lazy.spawn('dm-tool lock'),desc="locks session"),
    Key([], "Pause" , lazy.spawn('dm-tool lock'),desc="locks session"),
    Key([], "Cancel" , lazy.spawn('systemctl hibernate'),desc="enter hibernation"),
    

    #moving focus aroung
    Key([mod], "h", lazy.layout.left(),desc="move focus left"),
    Key([mod], "l", lazy.layout.right(),desc="move focus right"),
    Key([mod], "j", lazy.layout.down(),desc="move focus down"),
    Key([mod], "k", lazy.layout.up(),desc="move focus up"),
    Key([mod,"mod1"], "k", lazy.to_screen(0),desc="move focus to top screen"),
    Key([mod,"mod1"], "j", lazy.to_screen(2),desc="move focus to main screen"),
    Key([mod,"mod1"], "l", lazy.to_screen(2),desc="move focus to main screen"),
    Key([mod,"mod1"], "h", lazy.to_screen(1),desc="move focus to left screen"),
    Key([mod,"mod1"], "n", lazy.next_screen(),desc="move focus to left screen"),

    # moving windows around
    Key([mod, "shift"], "h", lazy.layout.swap_left(),desc="move focused window left"),
    Key([mod, "shift"], "l", lazy.layout.swap_right(),desc="move focused window right"),
    Key([mod, "shift"], "j", lazy.layout.shuffle_down(),desc="move focused window down"),
    Key([mod, "shift"], "k", lazy.layout.shuffle_up(),desc="move focused window up"),
    Key([mod, "shift", "control"], "h", lazy.layout.swap_column_left(),desc="move focused window one column to the left"),
    Key([mod, "shift", "control"], "l", lazy.layout.swap_column_right(),desc="move focused window one column to the right"),

    # resize windows
    Key([mod], "plus", lazy.layout.grow(),desc="increase window size"),
    Key([mod], "minus", lazy.layout.shrink(),desc="decrease window size"),
    Key([mod], "n", lazy.layout.normalize(),desc="normalize windows"),
    Key([mod], "m", lazy.layout.maximize(),desc="maximize current window"),
    Key([mod], "space", lazy.window.toggle_fullscreen(),desc="make current window fullscreen"),
    
    # app hotkeys
    Key([mod],"t", lazy.spawn(term), desc="Launch terminal"),
    Key([mod],"f", lazy.spawn("firefox"),desc="Launch firefox"),
    Key([mod],"e", lazy.spawn("pcmanfm"),desc="Launch pcmanfm"),
    Key([mod],"c", lazy.spawn("code"),desc="Launch visual studio code"),
    Key([mod],"v", lazy.spawn(term + " -e nvim"),desc="Launch NeoVim"),
    Key([mod, "shift"],"s", lazy.spawn('gscreenshot -s -o -f /tmp/screenshots'),desc="take a screenshot"),
    Key([mod], "o", lazy.spawn("emacsclient -c -a 'emacs'"), desc="Launch Emacs (Client)"),

    # Toggle between different layouts as defined below
    Key([mod], "Tab", lazy.next_layout(), desc="Toggle between layouts"),
    Key([mod], "BackSpace", lazy.window.kill(), desc="Kill focused window"),

    # qtile hotkeys
    Key([mod, "control"], "r", lazy.restart(), desc="Restart qtile"),
    Key([mod, "control"], "q", lazy.shutdown(), desc="Shutdown qtile"),

    #rofi
    Key([mod],'Return',lazy.spawn("rofi -show drun -show-icons -modi drun"),desc="launch rofi (drun)"),

    # audio hotkeys
    Key([], 'XF86AudioRaiseVolume', lazy.spawn(f'pulseaudio-ctl up {5 if chassis_type == ChassisType.LAPTOP else 1}'), desc="increase speaker volume"),
    Key([], 'XF86AudioLowerVolume', lazy.spawn(f'pulseaudio-ctl down {5 if chassis_type == ChassisType.LAPTOP else 1}'), desc="decrease speaker volume"),
    Key([], 'XF86AudioMute', lazy.spawn('pulseaudio-ctl mute'), desc="toggle speaker mute"),
    Key(['control'], 'XF86AudioRaiseVolume', lazy.spawn(f'amixer set Capture {5 if chassis_type == ChassisType.LAPTOP else 1}%+'), desc="increase mic volume"),
    Key(['control'], 'XF86AudioLowerVolume', lazy.spawn(f'amixer set Capture {5 if chassis_type == ChassisType.LAPTOP else 1}%-'), desc="decrease mic volume"),
    Key(['control'], 'XF86AudioMute', lazy.spawn('amixer set Capture toggle'), desc="toggle mic mute"),
    Key([], 'XF86AudioMicMute', lazy.spawn('amixer set Capture toggle'), desc="toggle mic mute"),

    # Media hotkeys
    Key([], 'XF86AudioNext', lazy.spawn('playerctl next')),
    Key([], 'XF86AudioPrev', lazy.spawn('playerctl previous')),
    Key([], 'XF86AudioPlay', lazy.spawn('playerctl play-pause')),
    Key([mod], 'Right', lazy.spawn('playerctl next')),
    Key([mod], 'Left', lazy.spawn('playerctl previous')),
    Key([mod], 'Down', lazy.spawn('playerctl play-pause')),

    # backlight keys
    Key([], 'XF86MonBrightnessUp', lazy.spawn('xbacklight -inc 5')),
    Key([], 'XF86MonBrightnessDown', lazy.spawn('xbacklight -dec 5')),
]


# Drag floating layouts.
mouse = [
    Drag([mod], "Button1", lazy.window.set_position_floating(),
         start=lazy.window.get_position()),
    Drag([mod], "Button3", lazy.window.set_size_floating(),
         start=lazy.window.get_size()),
    Click([mod], "Button2", lazy.window.bring_to_front())
]
