import speech_recognition as sr
import pyautogui
import time
import subprocess
import ctypes
import webbrowser
import os

def minimize_window():
    try:
        hwnd = ctypes.windll.kernel32.GetConsoleWindow()
        if hwnd:
            ctypes.windll.user32.ShowWindow(hwnd, 6)
    except:
        pass

GERMAN_NUMBERS = {
    'eins': 1, 'zwei': 2, 'drei': 3, 'vier': 4, 'fünf': 5,
    'sechs': 6, 'sieben': 7, 'acht': 8, 'neun': 9, 'zehn': 10,
    'elf': 11, 'zwölf': 12, 'dreizehn': 13, 'vierzehn': 14, 'fünfzehn': 15,
    'sechzehn': 16, 'siebzehn': 17, 'achtzehn': 18, 'neunzehn': 19, 'zwanzig': 20
}

KEY_MAPPINGS = {
    'strg': 'ctrl', 'shift': 'shift', 'alt': 'alt', 'tabulator': 'tab',
    'enter': 'enter', 'leertaste': 'space', 'entfernen': 'delete', 'escape': 'esc'
}

pyautogui.FAILSAFE = True

def process_commands(lower_text):
    if "löschen" in lower_text:
        num = 1
        for word in lower_text.split():
            if word in GERMAN_NUMBERS: num = GERMAN_NUMBERS[word]
            elif word.isdigit(): num = int(word)
        for _ in range(num): pyautogui.press('backspace')
    elif "öffne bericht" in lower_text:
        webbrowser.open("https://www.azubiheft.de/Azubi/Tagesbericht.aspx#nbb")
    elif "öffne teams" in lower_text:
        pyautogui.press('win'); time.sleep(0.5)
        pyautogui.write('teams'); pyautogui.press('enter'); time.sleep(3)
        pyautogui.hotkey('ctrl', '4'); time.sleep(1)
        pyautogui.press('tab', presses=4, interval=0.1); pyautogui.press('enter')
    elif "öffne word" in lower_text:
        os.system("start winword")
    elif "schrägstrich" in lower_text: pyautogui.press('/')
    elif "beenden" in lower_text: 
        os._exit(0)
    elif "befehl auf" in lower_text: pyautogui.hotkey('ctrl', 'alt', '7')
    elif "befehl zu" in lower_text: pyautogui.hotkey('ctrl', 'alt', '0')
    elif "alt tab" in lower_text: pyautogui.hotkey('alt', 'tab')
    elif "alt f4" in lower_text: pyautogui.hotkey('alt', 'f4')
    elif "kopieren" in lower_text: pyautogui.hotkey('ctrl', 'c')
    elif "einfügen" in lower_text: pyautogui.hotkey('ctrl', 'v')
    elif "ausschneiden" in lower_text: pyautogui.hotkey('ctrl', 'x')
    elif "alles auswählen" in lower_text: pyautogui.hotkey('ctrl', 'a')
    elif "windows" in lower_text:
        for word, number in GERMAN_NUMBERS.items():
            if word in lower_text:
                pyautogui.hotkey('win', str(number))
                break
    elif lower_text in KEY_MAPPINGS:
        pyautogui.press(KEY_MAPPINGS[lower_text])

if __name__ == "__main__":
    minimize_window()
    r = sr.Recognizer()
    r.dynamic_energy_threshold = True 
    r.pause_threshold = 0.8 
    with sr.Microphone() as source:
        try:
            r.adjust_for_ambient_noise(source, duration=1)
        except:
            pass
        while True:
            try:
                audio = r.listen(source, timeout=None, phrase_time_limit=4)
                text = r.recognize_google(audio, language='de-DE')
                process_commands(text.lower())
            except:
                continue
