# ---------------------------------------------------------------------------------
# Table Wait Conditions
#
# These are a collection of classes for WebDriverWait specific to Tables
#
# --------------------------------------------------------------------------------

from accelarobotkeywords.config.seleniumsettings import SeleniumSettings
from accelarobotkeywords.framework.selenium.table.tablescanner import TableScanner


class GetTableDetails(object):
    """Condition to get the tables details"""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            table_info = TableScanner.get_table_details(driver, self.locator)
            return table_info
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())


class GetCellText(object):
    """Condition to get the text from a cell"""
    def __init__(self, locator, row_number, column_number):
        self.locator_type = locator.locator_type
        self.locator_value = locator.locator_value
        self.row_num = row_number
        self.col_num = column_number

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            table_element = driver.find_element(self.locator_type, self.locator_value)
            xpath = 'descendant::tr[%s]' % self.row_num + '/descendant::td[%s]' % self.col_num
            cell_element = table_element.find_element(by='xpath', value=xpath)
            return cell_element.text
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())


class SearchForCellValue(object):
    """Condition to search text value in a column"""
    def __init__(self, locator, column_index, search_text, start_row, max_rows):
        self.locator = locator
        self.column_index = column_index
        self.search_text = search_text
        self.max_rows = max_rows
        self.start_row = start_row

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            row_index = TableScanner.search_column_for_value(driver=driver, locator=self.locator,
                                                             column_index=self.column_index,
                                                             search_text=self.search_text, max_rows=self.max_rows,
                                                             start_row=self.start_row)
            return row_index
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())


class ClickOnCell(object):
    """Condition to search text value in column and click on child element in cell"""
    def __init__(self, locator, column_index, search_text, start_row, max_rows, click_column_index):
        self.locator = locator
        self.column_index = column_index
        self.search_text = search_text
        self.max_rows = max_rows
        self.start_row = start_row
        self.click_column_index = click_column_index

    def __call__(self, driver):
        try:
            driver.implicitly_wait(1)
            row_index = TableScanner.search_column_for_value(driver=driver, locator=self.locator,
                                                             column_index=self.column_index,
                                                             search_text=self.search_text, max_rows=self.max_rows,
                                                             start_row=self.start_row)
            table_element = driver.find_element(self.locator.locator_type, self.locator.locator_value)
            xpath = 'descendant::tr[%s]' % row_index + '/descendant::td[%s]' % self.click_column_index + '/*[1]'
            element = table_element.find_element(by='xpath', value=xpath)
            element.click()
            return True
        except Exception:
            return False
        finally:
            driver.implicitly_wait(SeleniumSettings.get_implicit_wait())
