# -----------------------------------------------------------------------
# List Wait Conditions
#
# These are a collection of classes for WebDriverWait specific to Lists
#
# ------------------------------------------------------------------------

from accelarobotkeywords.config.seleniumsettings import SeleniumSettings


class ListWaitForItemCount(object):
    """
    An expected condition for waiting until list displays specified number of items
    """
    def __init__(self, locator, count):
        self.locator_type = locator.locator_type
        self.locator_value = locator.locator_value
        self.count = count

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            parent_element = driver.find_element(self.locator_type, self.locator_value)
            list_elements = parent_element.find_elements_by_xpath("li[not(@style='display: none;')]")
            return len(list_elements) == self.count
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())
