import os
import subprocess
import sys
from swayipc import Swayipc
import swayworkspace
from pathlib import Path
from warehouse import get_base
    
def run_sh(c): 
    subprocess.Popen(c, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

def apply_ui_and_environment(sway):
    base = os.path.expanduser(get_base())
    
    sway.cmd("floating_modifier Mod4")
    sway.cmd("default_border none")
    sway.cmd("default_floating_border none")
    
    sway.cmd("client.focused          #ffffff #ffffff #ffffff #ffffff")
    sway.cmd("client.focused_inactive #000000 #000000 #000000 #000000")
    sway.cmd("client.unfocused        #000000 #000000 #000000 #000000")
    sway.cmd("client.urgent           #ffffff #ffffff #ffffff #ffffff")
    sway.cmd("client.placeholder      #000000 #000000 #000000 #000000")
    
    sway.cmd('for_window [app_id="xpad"] floating enable')

    sway.cmd("exec_always gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'")
    sway.cmd("exec_always gsettings set org.gnome.desktop.interface gtk-theme 'MonoThemeDark'")
    sway.cmd("exec_always gsettings set org.gnome.desktop.interface.wm.preferences theme 'MonoThemeDark'")
    sway.cmd("exec_always gsettings set org.gnome.desktop.interface cursor-size '24'")
    sway.cmd("exec_always gsettings set org.gnome.desktop.interface font-name 'Noto Sans Mono 10'")
    sway.cmd("exec_always gsettings set org.gnome.desktop.interface.desktop-app-info-default-handler 'librewolf.desktop'")
    run_sh("wl-paste --watch cliphist store")
    run_sh("/usr/lib/xdg-desktop-portal -r")
    sway.cmd("setenv GTK_USE_PORTAL 0")
    sway.cmd("setenv XDG_CURRENT_DESKTOP sway")
    sway.cmd("setenv XDG_SESSION_TYPE wayland")
    sway.cmd("setenv XDG_PORTAL_BACKEND wlroots")
        
    run_sh(f"swaybg -i {base}/wallpaper/metrowaymaka.png -m fill")
    run_sh("blueman-applet")
    run_sh("kdeconnectd")
    run_sh("/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1")
    swayworkspace.SwaySome().init(1)
    run_sh("dbus-update-activation-environment WAYLAND_DISPLAY XDG_CURRENT_DESKTOP SWAYSOCK")

def bind_keys(sway):
    base = get_base()
    mod = "Mod4"
    
    binding_actions = {}
    script_path = os.path.abspath(__file__)

    def assign_key(key_combo):
        def decorator(func):
            result = func()
            sway.cmd(f"bindsym {key_combo} {result}")
            return func
        return decorator

    @assign_key(f"{mod}+h")
    def focus_left(): return "focus left"

    @assign_key(f"{mod}+j")
    def focus_down(): return "focus down"

    @assign_key(f"{mod}+k")
    def focus_up(): return "focus up"

    @assign_key(f"{mod}+l")
    def focus_right(): return "focus right"

    @assign_key(f"{mod}+Shift+h")
    def move_left(): return "move left"

    @assign_key(f"{mod}+Shift+j")
    def move_down(): return "move down"

    @assign_key(f"{mod}+Shift+k")
    def move_up(): return "move up"

    @assign_key(f"{mod}+Shift+l")
    def move_right(): return "move right"

    @assign_key(f"{mod}+bracketleft")
    def workspace_prev(): return "workspace prev"

    @assign_key(f"{mod}+bracketright")
    def workspace_next(): return "workspace next"

    @assign_key(f"{mod}+Q")
    def open_terminal(): return "exec kitty"

    @assign_key(f"{mod}+c")
    def kill_window(): return "kill"

    @assign_key(f"{mod}+Shift+s")
    def take_screenshot(): return f"exec python3 {base}/main/moves.py screenshot --out ~/screenshots/"

    @assign_key(f"{mod}+Shift+escape")
    def exit_sway(): return "exit"

    @assign_key(f"{mod}+Up")
    def volume_up(): return "exec pamixer --increase 5"

    @assign_key(f"{mod}+Down")
    def volume_down(): return "exec pamixer --decrease 5"

    @assign_key(f"{mod}+Shift+grave")
    def reload_sway(): return "reload"

    @assign_key(f"{mod}+Shift+f")
    def record_screen(): return f"exec python3 {base}/main/moves.py record --fullscreen"

    @assign_key(f"{mod}+r")
    def open_menu(): return "exec wofi --show drun --width 400 --height 500 --layer overlay --location 1"

    @assign_key(f"{mod}+o")
    def open_menu(): return "exec cliphist list | wofi -S dmenu --width 400 --height 500 --layer overlay --location 3 | cliphist decode | wl-copy"

    @assign_key("Ctrl+Shift+Alt+Mod4+L")
    def open_linkedin(): return "exec flatpak run io.github.ungoogled_software.ungoogled_chromium https://www.linkedin.com"

    @assign_key(f"{mod}+Shift+c")
    def force_kill(): return f"exec python3 {base}/main/moves.py kill"

    @assign_key(f"{mod}+n")
    def open_notes(): return "exec xpad"

    @assign_key(f"{mod}+v")
    def toggle_floating(): return "floating toggle"

    @assign_key("XF86AudioPlay")
    def media_play(): return "exec playerctl play-pause"

    @assign_key("XF86AudioPause")
    def media_pause(): return "exec playerctl play-pause"

    for i in range(1, 11):
        num = 0 if i == 10 else i
        sway.cmd(f"bindsym {mod}+{num} exec python3 {base}/main/swayworkspace.py focus {i}")
        sway.cmd(f"bindsym {mod}+Shift+{num} exec python3 {base}/main/swayworkspace.py move {i}")

    return binding_actions

def build_bar(sway):
    base = os.path.expanduser(get_base())
    
    sway.cmd("bar bar-main position top")
    sway.cmd(f"bar bar-main status_command python3 {base}/main/sway_status.py")
    sway.cmd("bar bar-main font pango:Noto Sans Mono 10")
    sway.cmd("bar bar-main mode dock")
    
    sway.cmd("bar bar-main colors background #000000")
    sway.cmd("bar bar-main colors statusline #ffffff")
    sway.cmd("bar bar-main colors separator #ffffff")
    sway.cmd("bar bar-main colors focused_workspace #000000 #ffffff #000000")
    sway.cmd("bar bar-main colors active_workspace #000000 #ffffff #000000")
    sway.cmd("bar bar-main colors inactive_workspace #000000 #000000 #ffffff")
    sway.cmd("bar bar-main colors urgent_workspace #ffffff #ffffff #ffffff")

if __name__ == "__main__":
    sway = Swayipc()
    apply_ui_and_environment(sway)
    bind_keys(sway)
    build_bar(sway)
