#!/usr/bin/env python3
# https://git.cyberia.club/reese/tilde/src/branch/main/scripts/wal.py
# pywal wrapper

import pathlib as path
from subprocess import run
from subprocess import Popen

from time import sleep
from datetime import datetime

import argparse
import re
import json
import yaml
import pywal

from sunposition.sunposition import sunpos

from PIL import Image
from PIL import ImageFilter

from colors import draw
# from rgbctl import keeb

from pywalfox.channel.unix.client import Client
from pywalfox.config import COMMANDS


wallpapers_root = path.Path("/home/reese/pictures/wallpapers")
config_file = path.Path("/home/reese/.secrets/location.yml")
mode_file = path.Path("/home/reese/.cache/wal/light_mode")


# Find out whether it should use a light or dark theme based on the position of the sun.
def get_auto_mode():
    with open(config_file) as stream:
        config = yaml.safe_load(stream)
    if not (config["latitude"] and config["longitude"] and config["elevation"]):
        print(
            f"Please put your location (latitude, longitude, and elevation) in {config_file} for auto mode to work!"
        )
        return False
    sun = sunpos(
        dt=datetime.utcnow(),
        latitude=config["latitude"],
        longitude=config["longitude"],
        elevation=config["elevation"],
    )
    angle = sun[1]
    if angle < 84:
        return "light"
    return "dark"


# Hang until the value returned by get_auto_mode() has changed, checking every `wait` seconds
def auto_wait(wait):
    initial = None
    try:
        with open(mode_file, "r") as modefile:
            initial = ("dark", "light")[modefile.read() == "True"]
    except:
        pass
    if initial is None:
        initial = get_auto_mode()

    new = initial
    while new == initial:
        sleep(wait)
        new = get_auto_mode()

    with open(mode_file, "w") as modefile:
        modefile.write(str(new == "light"))

    return new


# Get aspect ratio of current monitor setup.
# TODO: wayland
def get_ratio_str():
    ratio = run(
        ["/home/reese/scripts/screen_ratio"], encoding="utf-8", capture_output=True
    ).stdout
    if not ratio:
        return "normal"
    return ratio.rstrip()


# Generate blurred/tinted background for i3lock so it doesn't have to do it on-the-fly.
def gen_i3lock_bg(image, bgcolor):
    im = Image.open(image).filter(ImageFilter.GaussianBlur(radius=48))
    tint = Image.new(mode=im.mode, size=im.size, color=bgcolor)
    Image.blend(im, tint, 0.5).save("/tmp/i3lock_bg.png")


# Load color scheme from JSON file or, if it doesn't exist, generate one from the wallpaper.
def load_scheme(image, args):
    colors = None
    scheme_path = path.Path(
        f"/home/reese/.config/wal/colorschemes/{args.mode}/{path.Path(image).parts[-1]}.json"
    )
    if scheme_path.is_file():
        with open(scheme_path) as scheme:
            return json.load(scheme)
    return pywal.colors.get(
        image, light=(args.mode == "light"), backend=args.backend, sat=args.saturation
    )


# Tell various apps to reload their themes.
def update_apps(colors, image, args):
    pywal.reload.kitty()
    pywal.reload.sway()

    # change sway wallpaper
    # run(['swaymsg', 'output', '*', 'bg', image, 'fill'])

    # change GNOME appearance
    # run(['gsettings', 'set', 'org.gnome.desktop.background', 'picture-uri'+('-dark' if args.mode == 'dark' else ''), f''file://{image}''])
    # run(['gsettings', 'set', 'org.gnome.desktop.interface', 'color-scheme', f''prefer-{args.mode}''])

    # change XFCE wallpaper
    # run(['xfconf-query', '-c', 'xfce4-desktop', '-p', '/backdrop/screen0/monitorDP-1/workspace0/last-image', '-s', image])

    # gen_i3lock_bg(image, colors['colors']['color0'])

    # light/dark mode toggle for some applications
    run(["lightswitch.mjs", ("-l" if args.mode == "light" else ""), "-t", "sublime"])

    # (re)start dunst
    run(["killall", "dunst"])
    Popen(["dunst"])

    # set laptop keyboard color
    # keeb(list(colors['colors'].values())[1:7])

    # update firefox and thunderbird themes
    client = Client()
    for host in client.hosts:
        connected = client.connect(host)
        if connected is True:
            client.send_message(COMMANDS["UPDATE"])
            if args.mode == "dark":
                client.send_message(COMMANDS["THEME_MODE_DARK"])
            else:
                client.send_message(COMMANDS["THEME_MODE_LIGHT"])

    # trick eww into updating
    path.Path("/home/reese/.config/eww/eww.scss").touch()


def colorpickle(image, args):
    colors = (
        run(
            [
                "/home/reese/.cargo/bin/colorpickle",
                image,
                "--saturate",
                str(args.saturation),
                "--lighten",
                str(args.lightness),
            ],
            encoding="utf-8",
            capture_output=True,
        )
        .stdout.strip()
        .split("\n")
    )
    print(colors)
    scheme = {
        "wallpaper": image,
        "alpha": "100",
        "special": {
            "background": "#050f21",
            "foreground": "#c0c3c7",
            "cursor": "#c0c3c7",
        },
        "colors": {
            "color0": "#050f21",
            "color1": "#3a5689",
            "color2": "#5f6aba",
            "color3": "#4b3f93",
            "color4": "#6b3c8b",
            "color5": "#9a45a1",
            "color6": "#c372bf",
            "color7": "#c0c3c7",
            "color8": "#434b58",
            "color9": "#3a5689",
            "color10": "#5f6aba",
            "color11": "#4b3f93",
            "color12": "#6b3c8b",
            "color13": "#9a45a1",
            "color14": "#c372bf",
            "color15": "#c0c3c7",
        },
    }

    for i, color in enumerate(colors):
        scheme["colors"][f"color{i}"] = color

    if args.mode == "light":
        scheme["special"][f"background"] = scheme["colors"]["color15"]
        scheme["special"][f"foreground"] = scheme["colors"]["color0"]
    else:
        scheme["special"][f"background"] = scheme["colors"]["color0"]
        scheme["special"][f"foreground"] = scheme["colors"]["color15"]
    scheme["special"][f"cursor"] = scheme["colors"]["color8"]

    return scheme


def main(args):
    if args.mode in {"auto", "auto_loop"}:
        args.mode = get_auto_mode()

    img_path = None
    if args.path:
        img_path = path.Path(args.path)
    else:
        ratio = get_ratio_str()
        img_path = wallpapers_root / ratio / args.mode

    image = pywal.image.get(str(img_path))
    print(image)
    colors = load_scheme(image, args)
    # colors = colorpickle(image, args)
    # print(colors)

    pywal.sequences.send(colors, vte_fix=True)
    pywal.export.every(colors)
    # pywal.wallpaper.change(image)
    # swaybg(image)

    update_apps(colors, image, args)

    draw("small")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Set wallpaper and change color schemes of apps to match."
    )
    parser.add_argument(
        "--mode", "-m", choices=["dark", "light", "auto", "auto_loop"], default="auto"
    )
    parser.add_argument(
        "--backend",
        "-b",
        choices=[
            "colorthief",
            "fast_colorthief",
            "colorz",
            "haishoku",
            "schemer2",
            "wal",
        ],
        default="colorz",
    )
    parser.add_argument("--saturation", "-s", type=float, default=0.4)
    parser.add_argument("--lightness", "-l", type=float, default=0)
    parser.add_argument("path", default="", nargs="?")
    args = parser.parse_args()

    if args.mode == "auto_loop":
        while True:
            main(args)
            args.mode = auto_wait(60)  # check every minute
    else:
        main(args)
