"""
Wait helper utilities for Cognitive Prism Automation Tests
"""
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from config.config import EXPLICIT_WAIT


class WaitHelpers:
    """Helper class for explicit waits"""
    
    def __init__(self, driver):
        """
        Initialize WaitHelpers
        
        Args:
            driver: WebDriver instance
        """
        self.driver = driver
        self.wait = WebDriverWait(driver, EXPLICIT_WAIT)
    
    def wait_for_element_clickable(self, locator):
        """
        Wait for element to be clickable
        
        Args:
            locator: Tuple of (By, locator_string)
            
        Returns:
            WebElement: Clickable element
        """
        return self.wait.until(EC.element_to_be_clickable(locator))
    
    def wait_for_element_present(self, locator):
        """
        Wait for element to be present in DOM
        
        Args:
            locator: Tuple of (By, locator_string)
            
        Returns:
            WebElement: Present element
        """
        return self.wait.until(EC.presence_of_element_located(locator))
    
    def wait_for_element_visible(self, locator, timeout=None):
        """
        Wait for element to be visible
        
        Args:
            locator: Tuple of (By, locator_string)
            timeout: Optional timeout override (uses EXPLICIT_WAIT if not provided)
            
        Returns:
            WebElement: Visible element
        """
        if timeout is not None:
            wait = WebDriverWait(self.driver, timeout)
            return wait.until(EC.visibility_of_element_located(locator))
        else:
            return self.wait.until(EC.visibility_of_element_located(locator))
    
    def wait_for_text_present(self, locator, text):
        """
        Wait for text to be present in element
        
        Args:
            locator: Tuple of (By, locator_string)
            text: Text to wait for
            
        Returns:
            bool: True if text is present
        """
        return self.wait.until(EC.text_to_be_present_in_element(locator, text))
    
    def wait_for_element_invisible(self, locator, timeout=None):
        """
        Wait for element to be invisible
        
        Args:
            locator: Tuple of (By, locator_string)
            timeout: Optional timeout override (uses EXPLICIT_WAIT if not provided)
            
        Returns:
            bool: True if element is invisible
        """
        if timeout is not None:
            wait = WebDriverWait(self.driver, timeout)
            return wait.until(EC.invisibility_of_element_located(locator))
        else:
            return self.wait.until(EC.invisibility_of_element_located(locator))
    
    def wait_for_url_contains(self, url_part):
        """
        Wait for URL to contain specific text
        
        Args:
            url_part: Part of URL to wait for
            
        Returns:
            bool: True if URL contains the text
        """
        return self.wait.until(EC.url_contains(url_part))
    
    def wait_for_page_load(self):
        """Wait for page to load completely"""
        self.wait.until(lambda driver: driver.execute_script("return document.readyState") == "complete")
    
    def wait_for_loading_to_disappear(self, timeout=None):
        """
        Wait for loading indicator to disappear
        
        Args:
            timeout: Optional timeout override
        """
        wait_time = timeout or EXPLICIT_WAIT
        wait = WebDriverWait(self.driver, wait_time)
        try:
            wait.until(EC.invisibility_of_element_located(
                (By.XPATH, "//div[contains(., 'Loading')]")
            ))
        except TimeoutException:
            # Loading might not be present, which is fine
            pass






