#!/usr/bin/env python

import sys
import subprocess
import json

if __name__ == "__main__":
    try:
        result = subprocess.run(
            ['hyprctl', '-j', 'monitors'],
            capture_output=True, text=True, check=True
        )
        monitors = json.loads(result.stdout)
        monitor_names = []
        for monitor in monitors:
            if 'name' in monitor:
                monitor_names.append(monitor['name'])
        if "Virtual-1" in monitor_names:
            result = subprocess.run(
                ["hyprctl", "keyword", "monitor", "Virtual-1,disable"],
                capture_output=True, text=True, check=True
            )
            if result.returncode != 0:
                print("Failed to disable monitor Virtual-1", file=sys.stderr)
                sys.exit(1)
            else:
                result = subprocess.run(["pkill", "-x", "wayvnc"],
                                        capture_output=True, text=True)
                if result.returncode != 0:
                    print("Failed to stop wayvnc", file=sys.stderr)
                    sys.exit(1)
                else:
                    subprocess.run(["hyprctl", "keyword", "decoration:blur:enabled", "true"])
                    subprocess.run(["hyprctl", "keyword", "decoration:drop_shadow", "true"])
                    subprocess.run(["hyprctl", "keyword", "decoration:active_opacity", "1.0"])
                    subprocess.run(["hyprctl", "keyword", "decoration:inactive_opacity", "0.7"])
                    sys.exit(0)
        else:
            result = subprocess.run(
                ["hyprctl", "keyword", "monitor",
                 "Virtual-1, 2800x1752@120,1920x0,2"],
                capture_output=True, text=True, check=True
            )
            if result.returncode != 0:
                print("Failed to set monitor Virtual-1", file=sys.stderr)
                sys.exit(1)
            else:
                result = subprocess.run(
                    ["hyprctl", "output", "create", "headless", "Virtual-1"],
                    capture_output=True, text=True, check=True
                )
            if result.returncode != 0:
                print("Failed to create headless monitor Virtual-1",
                      file=sys.stderr)
                sys.exit(1)
            else:
                subprocess.run(["hyprctl", "keyword", "decoration:blur:enabled", "false"])
                subprocess.run(["hyprctl", "keyword", "decoration:drop_shadow", "false"])
                subprocess.run(["hyprctl", "keyword", "decoration:active_opacity", "1.0"])
                subprocess.run(["hyprctl", "keyword", "decoration:inactive_opacity", "1.0"])
                result = subprocess.Popen(
                    ["wayvnc", "--output=Virtual-1", "0.0.0.0", "5900", "&"],
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL)
            
    except subprocess.CalledProcessError as e:
        print(f"Error executing hyprctl: {e}", file=sys.stderr)
        sys.exit(1)
    except Exception as e:
        print(f"An unexpected error occurred: {e}", file=sys.stderr)
        sys.exit(1)
