from libqtile import widget

from defines import base_color, term
from defines import blue_color, light_foreground_color, dark_foreground_color, red_color, light_purple_color, purple_color, green_color, orange_color, magenta_color, yellow_color
from defines import ChassisType, chassis_type
from Custom_Widgets import MemoryC, Mic

#region Powerline
def powerline_arrow(direction, color1, color2,size):
    if direction == "r":
        return [
            widget.TextBox(
                text=u"\ue0b0",
                foreground=color1,
                background=color2,
                fontsize=size,
                borderwidth=0,
                padding=0
            ),
        ]
    else:
        return [
            widget.TextBox(
                text=u"\ue0b2",
                foreground=color2,
                background=color1,
                fontsize=size,
                borderwidth=0,
                padding=0
            ),
        ]
#endregion

#region Left_widgets
def Left_widgets(size,fontsize,prompt=False):
    return [
        widget.CurrentLayoutIcon(
            scale = 0.9,
        ),
        *powerline_arrow('r',base_color,blue_color,size),
        widget.TextBox(
            text=' ',
            foreground=light_foreground_color,
            fontsize=fontsize,
            background=blue_color
        ),
        *powerline_arrow('r',blue_color,base_color,size),
        widget.GroupBox(
            fontsize=fontsize,
            rounded=False,
            disable_drag=True,
            active=light_foreground_color,
            inactive=dark_foreground_color,
            highlight_method='block',
            highlight_color=red_color,
            other_screen_border = light_purple_color,
            other_current_screen_border = purple_color,
            this_current_screen_border = blue_color,
            this_screen_border = blue_color,
            urgent_alert_method='block',
            urgent_border = red_color,
        ),
        widget.WindowName(fontsize=fontsize-2),
    ]
#endregion

#region Volume_widget
def volume_widget(prev_color,color,size,fontsize):
    return[
        *powerline_arrow('l',prev_color,color,size),
        widget.Volume(
            foreground=light_foreground_color,
            background=color,
            emoji=True,
            fontsize=fontsize - 4,
            ),
        widget.Volume(
            foreground=light_foreground_color,
            background=color,
            fontsize=fontsize,
            padding=0
            ),
        Mic(
            foreground=light_foreground_color,
            background=color,
            emoji=True,
            fontsize=fontsize,
        ),
        Mic(
            foreground=light_foreground_color,
            background=color,
            fontsize=fontsize,
            padding=0
        ),
        widget.TextBox(" ",background=color),
    ]
#endregion

#region System_widgets
def launch_htop():
    qtile.cmd_spawn(term + ' -e htop')

def System_widgets(prev_color,last_color,size,fontsize):
    sys_widgets = []
    sys_widgets.append(*powerline_arrow('l',prev_color,orange_color,size))
    # first widget is battery for laptop and cpu temp otherwise
    if chassis_type == ChassisType.LAPTOP:
        sys_widgets.append(
            widget.Battery(
                foreground=light_foreground_color,
                background=orange_color,
                fontsize=fontsize,
                update_interval=1,
                format="{char} {percent:2.0%}",
                low_percentage=0.2,
                notify_below=True,
                low_foreground=red_color,
                )
        )
    else:
        sys_widgets.extend([
            widget.TextBox(
                text='󰈸',
                foreground=light_foreground_color,
                background=orange_color,
                fontsize=fontsize+6
                ),
            widget.ThermalSensor(
                foreground=light_foreground_color,
                background=orange_color,
                fontsize=fontsize,
                tag_sensor='Tctl',
                mouse_callbacks = {"Button1": launch_htop},
                )
        ])
    sys_widgets.append(*powerline_arrow('l',orange_color,green_color,size))

    sys_widgets.extend([
        widget.TextBox(
            text=' ',
            foreground=light_foreground_color,
            background=green_color,
            fontsize=fontsize
            ),
        widget.CPU(
            foreground=light_foreground_color,
            background=green_color,
            fontsize=fontsize,
            format='{load_percent}%' if chassis_type == ChassisType.LAPTOP else'{load_percent}% @ {freq_current}GHz',
            mouse_callbacks = launch_htop,
            ),
        *powerline_arrow('l',green_color,yellow_color,size),
        widget.TextBox(
            text='',
            foreground=light_foreground_color,
            background=yellow_color,
            fontsize=fontsize
            ),
        MemoryC(
            foreground=light_foreground_color,
            background=yellow_color,
            fontsize=fontsize,
            format='{MemUsed}GB|{SwapUsed}GB' if chassis_type == ChassisType.LAPTOP else'{MemUsed}GB({MemPercent}%) | {SwapUsed}GB({SwapPercent}%)',
            mouse_callbacks = launch_htop,
            ), 
        *powerline_arrow('l',yellow_color,last_color,size),
        widget.TextBox(
            text='󰈀 ',
            foreground=light_foreground_color,
            background=blue_color,
            fontsize=fontsize
            ),
        widget.Net(
            background=last_color,
            foreground=light_foreground_color,
            fontsize=fontsize,
            prefix='M',
            format='{down:6.2f}{down_suffix}↓{up:6.2f}{up_suffix}↑',
            mouse_callbacks = launch_htop,
            )
        ]
    )
    return sys_widgets
#endregion

#region End_widgets
def end_widgets(prev_color,size,fontsize):
    return [
        *powerline_arrow('l',prev_color,purple_color,size),
        widget.TextBox(
            text='󰸗 ',
            foreground=light_foreground_color,
            background=purple_color,
            fontsize=fontsize
            ),
        widget.Clock(
            foreground=light_foreground_color,
            background=purple_color,
            fontsize=fontsize,
            format='%Y-%m-%d'
            ),
        *powerline_arrow('l',purple_color,base_color,size),
        widget.Clock(
            font='dseg7 classic bold',
            fontsize=16,
            format='%H:%M'
            ),
    ]
#endregion
