import os
import subprocess
from libqtile import layout, bar, widget, hook
from libqtile.config import Click, Drag, Group, Match, Screen, Rule
from libqtile.command import lazy
from libqtile.widget import Spacer
from keys import MyKeys
from libqtile.dgroups import simple_key_binder
from colors import gruvbox, nord_fox, my_colors
#from widgets import *

@hook.subscribe.startup_once
def start_once():
    autostart_path = os.path.expanduser('~/.config/autostart/autostart.sh')
    subprocess.run([autostart_path])

## Keybindings
my_keys = MyKeys(mod="mod4",
                 browser="qutebrowser",
                 term="alacritty")

keys = my_keys.init_keys()
dgroups_key_binder = simple_key_binder("mod4")

## Groups and layouts
groups = []
group_names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0",]
group_labels = ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ",]
group_layouts = ["monadtall", "monadtall", "monadtall", "monadtall", "monadtall", "monadtall", "monadtall", "monadtall", "monadtall", "monadtall",]

for i in range(len(group_names)):
    groups.append(
        Group(
            name=group_names[i],
            layout=group_layouts[i].lower(),
            label=group_labels[i],
        ))

def init_layout_theme():
    return {"margin":5,
            "border_width":2,
            "border_focus": "#5e81ac",
            "border_normal": "#4c566a"
            }

layout_theme = init_layout_theme()


layouts = [
    #layout.MonadTall(margin=8, border_width=2, border_focus="#5e81ac", border_normal="#4c566a"),
    layout.MonadTall(**layout_theme),
    #layout.MonadWide(margin=8, border_width=2, border_focus="#5e81ac", border_normal="#4c566a"),
    # layout.MonadWide(**layout_theme),
    # layout.Matrix(**layout_theme),
    # layout.Bsp(**layout_theme),
    # layout.Floating(**layout_theme),
    # layout.RatioTile(**layout_theme),
    layout.Max(**layout_theme)
]

# Widgets

def init_widgets_defaults():
    return dict(font="Noto Sans",
                fontsize = 12,
                padding = 2,
                background=my_colors["black"])

widget_defaults = init_widgets_defaults()

def init_widgets_list():
    widgets_list = [
            widget.Sep(
                linewidth = 1,
                padding = 6,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.Image(
                filename = "~/.config/qtile/caduceus.jpg",
                scale = "True",
            ),
            widget.GroupBox(
                font="FontAwesome",
                fontsize = 20,
                margin_y = 3,
                margin_x = 0,
                padding_y = 6,
                padding_x = 5,
                borderwidth = 1,
                disable_drag = True,
                active = my_colors["blue"],
                inactive = my_colors["grey"],
                rounded = False,
                highlight_color = my_colors["black"],
                highlight_method = "line",
                this_current_screen_border = my_colors["red"],
                other_current_screen_border = my_colors["grey"],
                foreground = my_colors["black"],
                background = my_colors["black"],
            ),
            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.CurrentLayout(
                font = "Noto Sans Bold",
                foreground = my_colors["orange"],
                background = my_colors["black"]
            ),
            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.WindowName(font="Noto Sans",
                fontsize = 12,
                foreground = my_colors["green"],
                background = my_colors["black"],
            ),

            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
           widget.TextBox(
                font="FontAwesome",
                text= '|' + str(subprocess.check_output("uname -r", shell=True).strip()).replace('b','') + '|',
                foreground=my_colors["blue"],
                background=my_colors["black"],
                padding = 0,
                fontsize=14
            ),
            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.Memory(
                measure_mem="G",
                fontsize=15,
                graph_color = my_colors["blue"]
            ),
            widget.MemoryGraph(
                type='box'
            ),
            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.ThermalSensor(
                threshold=50,
                fontsize=15,
            ),
            widget.CPUGraph(
                type='box',
                graph_color = my_colors["blue"]
            ),
            widget.Sep(
                linewidth = 1,
                padding = 10,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
            widget.TextBox(
                font="FontAwesome",
                text="  ",
                foreground=my_colors["blue"],
                background=my_colors["black"],
                padding = 0,
                fontsize=18
            ),
            widget.Clock(
                font='Mono',
                foreground = my_colors["white"],
                background = my_colors["black"],
                fontsize = 18,
                format="%d-%m-%Y | %H:%M"
            ),
            widget.Sep(
                linewidth = 1,
                padding = 3,
                foreground = my_colors["black2"],
                background = my_colors["black"],
            ),
            widget.Systray(
                background= my_colors["black"],
                icon_size=21,
                padding = 10
            ),
            widget.Sep(
                linewidth = 1,
                padding = 20,
                foreground = my_colors["black2"],
                background = my_colors["black"]
            ),
        ]
    return widgets_list

widgets_list = init_widgets_list()


def init_widgets_screen1():
    widgets_screen1 = init_widgets_list()
    return widgets_screen1

def init_widgets_screen2():
    widgets_screen2 = init_widgets_list()
    return widgets_screen2[:-2]

widgets_screen1 = init_widgets_screen1()
widgets_screen2 = init_widgets_screen2()


def init_screens():
    return [Screen(top=bar.Bar(widgets=init_widgets_screen1(), size=33, opacity=0.8)),
            Screen(top=bar.Bar(widgets=init_widgets_screen2(), size=26, opacity=0.8))]

screens = init_screens()

follow_mouse_focus = True
bring_front_click = False
cursor_warp = False
auto_fullscreen = True

focus_on_window_activation = "focus" # or smart


wmname = "Qtile-Apo11o"