# ---------------------------------------------------------------------------------
# Alert Wait Conditions
#
# These are a collection of classes for WebDriverWait specific to alerts
#
# --------------------------------------------------------------------------------
from accelarobotkeywords.config.seleniumsettings import SeleniumSettings


class AlertAcceptIfPresent(object):
    """Condition to accept alert if present"""
    def __init__(self):
        pass

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            alert = driver.switch_to.alert
            alert.accept()
            return True
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())


class AlertGetAlertText(object):
    """Condition to get text from alert popup"""
    def __init__(self):
        pass

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            alert = driver.switch_to.alert
            alert_text = alert.text
            return alert_text.strip()
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())


class AlertEnterTextAndAccept(object):
    """Condition to enter text into alert if present"""
    def __init__(self, text):
        self.text = text

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            alert = driver.switch_to.alert
            alert.send_keys(self.text)
            alert.accept()
            return True
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())
