# pip install selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from os import getcwd

# chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument("--use-fake-ui-for-media-stream") 
# chrome_options.add_argument("--headless=new")  # Use headless mode for Chrome 

# driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

# # Load local HTML file with correct path scheme
# website = f"file:///{getcwd().replace('\\', '/')}/index.html"
# driver.get(website)

# rec_file = f"{getcwd()}\\input.txt"

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--use-fake-ui-for-media-stream") 
chrome_options.add_argument("--headless=new")  # Use headless mode for Chrome 
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

website = "https://speech-to-text-beige-ten.vercel.app/"  # Use the correct URL for your web app
driver.get(website)
rec_file = f"{getcwd()}\\input.txt"


def listen():
    try:
        # Wait for and click the start button
        start_button = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.ID, "startButton"))  # ✅ corrected ID
        )
        start_button.click()
        print("Listening...")

        output_text = ""
        is_second_click = False

        while True:
            # Wait for output element
            output_element = WebDriverWait(driver, 20).until(
                EC.presence_of_element_located((By.ID, "output"))
            )
            current_text = output_element.text.strip()
            if "start Listning" in start_button.text and is_second_click:
                if output_text:
                    is_second_click = False
            elif "listening..." in start_button.text:
                is_second_click = True

            if current_text != output_text:
                output_text = current_text
                with open(rec_file, "w") as file:
                    file.write(output_text.lower())
                print("USER:" + output_text)

    except KeyboardInterrupt:
        pass
    except Exception as e:
        print(e)
