import os
import platform
import subprocess
from pathlib import Path

from pynput.keyboard import Key, KeyCode
from pynput.mouse import Button


def name_to_key(name: str) -> Key | KeyCode:
    try:
        return getattr(Key, name)
    except AttributeError:
        return KeyCode.from_char(name)

def name_to_button(name: str) -> Button:
    return getattr(Button, name)

def get_recordings_dir() -> str:
    documents_folder = Path.home() / 'Documents' / 'DuckTrack_Recordings'
    return str(documents_folder)

def fix_windows_dpi_scaling():
    """
    Fixes DPI scaling issues with legacy windows applications
    Reference: https://pynput.readthedocs.io/en/latest/mouse.html#ensuring-consistent-coordinates-between-listener-and-controller-on-windows
    """
    import ctypes
    PROCESS_PER_MONITOR_DPI_AWARE = 2
    ctypes.windll.shcore.SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE)

def open_file(path):
    if platform.system() == "Windows":
        os.startfile(path)
    elif platform.system() == "Darwin":
        subprocess.Popen(["open", path])
    else:
        subprocess.Popen(["xdg-open", path])
