from subprocess import run
from time import sleep

from ..abstractionhelper import is_installed
from .typer import Key, Typer


class DotoolTyper(Typer):
    @staticmethod
    def supported() -> bool:
        return is_installed("dotool")

    @staticmethod
    def name() -> str:
        return "dotool"

    def get_active_window(self) -> str:
        return "not possible with dotool"

    def type_characters(self, characters: str, start_delay: float, key_delay: int, active_window: str) -> None:
        sleep(start_delay)
        run(["dotool"], input=f"typedelay {key_delay}\ntype {characters}", encoding="utf-8")

    def press_key(self, key: Key) -> None:
        match key:
            case Key.ENTER:
                key_name = "enter"
            case Key.TAB:
                key_name = "tab"
            case _:
                raise Exception("Unknown key")

        run(["dotool"], input=f"key {key_name}", encoding="utf-8")
