from typing import Match
from libqtile.config import Group, ScratchPad, DropDown, Key, Match
from libqtile.command import lazy

import re

from defines import mod,term
from Keys import keys

group_names = [(" ","Org",'o',{'layout': 'monadtall'}),
               (" ","Home", 'h',{'layout': 'monadtall'}),
               (" ","Browser", 'f',{'layout': 'monadtall'}),
               ("  ","Mail", 'm',{'layout': 'monadtall', 'matches':[Match(wm_class="Mail")]}),
               (" ","Coding", 'c',{'layout': 'monadtall', 'matches' : [Match(wm_class="code")]}),
               (" ","Documents", 'l',{'layout': 'monadtall'}),
               ("󰝚 ","Music", 'u',{'layout': 'monadtall', 'matches' : [Match(wm_class="spotify")]}),
               (" ","Video", 'v',{'layout': 'monadtall', 'matches' : [Match(wm_class="vlc")]}),
               ("󰙯 ","VideoChat", 'z',{'layout': 'monadtall', 'matches':[Match(wm_class="discord")]}),
               ("一","etc1", '1', {'layout': 'monadtall'}),
               ("二","etc2", '2', {'layout': 'monadtall'}),
               ("三","etc3", '3', {'layout': 'monadtall'}),
               ("四","etc4", '4', {'layout': 'monadtall'})]

groups = [Group(icon, **kwargs) for icon, name, key, kwargs in group_names]

groups.append(
    ScratchPad("scratchpad",[
        DropDown(
            'ARandR',
            'arandr',
            height = 0.5,
            width = 0.8,
            x = .1,
            on_focus_lost_hide=True,
            opacity=1,
            match = Match(wm_class='arandr')
        ),
        DropDown(
            "term",
            term,
        ),
        DropDown(
            "htop",
            term + ' -e htop',
        ),
        DropDown(
            'sound',
            'pavucontrol'
        ),
        DropDown(
            'filemanager',
            'pcmanfm',
            on_focus_lost_hide=False
        ),
        DropDown(
            'bitwarden',
            'bitwarden-desktop',
            on_focus_lost_hide=False,
            match = Match(wm_class='bitwarden')
        ),
        DropDown(
            'WhatsApp',
            'whatsapp-nativefier',
            height = 0.5,
            width = 0.8,
            x = .1,
            on_focus_lost_hide=False,
            opacity=1
        ),
        DropDown(
            'Signal',
            'signal-desktop',
            height = 0.5,
            width = 0.8,
            x = .1,
            on_focus_lost_hide=False,
            opacity = 1,
            match = Match(wm_class='signal')
        ),
        DropDown(
            'Qalculate!',
            'qalculate-gtk',
            height = 0.5,
            width = 0.5,
            x = .25,
            on_focus_lost_hide=True,
            opacity = 1
        ),
        DropDown(
            'Slack',
            'slack',
            height = 0.5,
            width = 0.8,
            x = 0.1,
            on_focus_lost_hide=False,
            opacity=1
        ),
        DropDown(
            'Deepl',
            'surf deepl.com',
            height = 0.5,
            width = 0.8,
            x = 0.1,
            on_focus_lost_hide=True,
            opacity=1
        ),
        DropDown(
            'Telegram',
            'telegram-desktop',
            height = 0.5,
            width = 0.8,
            x = 0.1,
            on_focus_lost_hide=False,
            opacity=1,
            match = Match(wm_class='telegram-desktop')
        ),
        DropDown(
            'Org Agenda',
            'emacsclient -c -e \'(org-agenda-list)\'',
            height = 0.5,
            width = 0.8,
            x = 0.1,
            on_focus_lost_hide=True,
            opacity=1,
            match = Match(wm_class='emacs')
        )
    ])
)

for (icon,name,key, kwargs) in group_names:
    keys.extend([
        Key(["mod1","control"], str(key), lazy.group[icon].toscreen(toggle=True),
            desc="Switch to group {}".format(name)),

        Key(['mod1','control', "shift"], str(key), lazy.window.togroup(icon),
            desc="move focused window to group {}".format(name)),
    ])

keys.extend([
    Key([mod],'p',lazy.group['scratchpad'].dropdown_toggle('ARandR'),
        desc="open the dropdown for ARandR"),
    Key(['mod1','control'],'space',lazy.group['scratchpad'].dropdown_toggle('term'),
        desc="open the dropdown terminal"),
    Key(['control','shift'],'Escape',lazy.group['scratchpad'].dropdown_toggle('htop'),
        desc="open the dropdown terminal"),
    Key(['mod1','control'],'a',lazy.group['scratchpad'].dropdown_toggle('sound'),
        desc="open the dropdown pavucontrol"),
    Key(['mod1','control'],'e',lazy.group['scratchpad'].dropdown_toggle('filemanager'),
        desc="open the dropdown filemanager"),
    Key(['mod1','control'],'b',lazy.group['scratchpad'].dropdown_toggle('bitwarden'),
        desc="open the dropdown password manager KeePassXC"),
    Key(['mod1','control'],'w',lazy.group['scratchpad'].dropdown_toggle('WhatsApp'),
        desc="open the dropdown for WhatsApp"),
    Key(['mod1','control'],'i',lazy.group['scratchpad'].dropdown_toggle('Signal'),
        desc="open the dropdown for Signal"),
    Key(['mod1','control'],'s',lazy.group['scratchpad'].dropdown_toggle('Slack'),
        desc="open the dropdown for Slack"),
    Key(['mod1','control'],'t',lazy.group['scratchpad'].dropdown_toggle('Telegram'),
        desc="open the dropdown for Telegram"),
    Key(['mod1','control'],'q',lazy.group['scratchpad'].dropdown_toggle('Qalculate!'),
        desc="open the dropdown for Qalculate!"),
    Key(['mod1','control'],'d',lazy.group['scratchpad'].dropdown_toggle('Deepl'),
        desc="open the dropdown for Deepl"),
    Key(['mod1','control'],'k',lazy.group['scratchpad'].dropdown_toggle('Org Agenda'),
        desc="open the dropdown for Org Agenda")
])
