from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class BaseAction:
    def __init__(self, driver):
        self.driver = driver

    def base_find_element(self, loc, timeout=10, poll=1.0):
        """
        根据特征，找元素
        :param loc: 特征
        :param timeout: 超时时间
        :param poll: 频率
        :return: 元素
        """
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll)\
            .until(lambda x:x.find_element(*loc))

    def base_find_elements(self, loc, timeout=30, poll=1.0):
        """
        根据特征，找多个符合条件的元素
        :param loc: 特征
        :param timeout: 超时时间
        :param poll: 频率
        :return: 元素
        """
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll)\
            .until(lambda x:x.find_elements(*loc))

    def base_click_element(self, loc):
        """
        封装点击操作函数
        """
        self.base_find_element(loc).click()

    def base_input_text(self, loc, text):
        """
        封装输入操作函数
        """
        self.fm = self.base_find_element(loc)
        self.fm.clear()  # 需要先清空输入框，防止有默认内容
        self.fm.send_keys(text)

    def base_input_clear(self, loc):
        """
        封装清空操作函数
        """
        self.base_find_element(loc).clear()

    def base_get_value(self, loc):
        """
        获取元素的值
        """
        return self.base_find_element(loc).text

    def base_press_back(self):
        """
        点击返回健
        """
        self.driver.press_keycode(4)

    def base_press_enter(self):
        """
        点击回车
        """
        self.driver.press_keycode(66)

    def is_toast_exist(self, message):
        """
        根据 部分内容，判断toast是否存在
        :param message: 部分内容
        :return: 是否存在
        """
        message_xpath = By.XPATH, "//*[contains(@text,'%s')]" % message
        try:
            self.base_find_element(message_xpath, 5, 0.1)
            return True
        except TimeoutException:
            return False

    def get_toast_text(self, message):
        """
        根据 部分内容，获取toast上的所有内容
        :param message: 部分内容
        :return: 所有内容
        """
        if self.is_toast_exist(message):
            message_xpath = By.XPATH, "//*[contains(@text,'%s')]" % message
            return self.base_find_element(message_xpath, 5, 0.1).text
        else:
            raise Exception("toast未出现，请检查参数是否正确或toast有没有出现在屏幕上")

    def scroll_page_one_time(self, direction="up"):
        """
        滑动一次屏幕
        :param direction:方向
            "up":从下往上
            "down":从上往下
            "left":从右往左
            "down":从左往右
        :return:
        """
        width = self.driver.get_window_size()["width"]
        height = self.driver.get_window_size()["height"]

        center_x = width / 2
        center_y = height / 2

        left_x = width / 4 * 1
        left_y = center_y
        right_x = width / 4 * 3
        right_y = center_y

        bottom_x = center_x
        bottom_y = height / 4 * 3
        top_x = center_x
        top_y = height / 4 * 1

        if direction == "up":
            self.driver.swipe(bottom_x, bottom_y, top_x, top_y, 3000)
        elif direction == "down":
            self.driver.swipe(top_x, top_y, bottom_x, bottom_y, 3000)
        elif direction == "left":
            self.driver.swipe(right_x, right_y, left_x, left_y, 3000)
        elif direction == "right":
            self.driver.swipe(left_x, left_y, right_x, right_y, 3000)
        else:
            raise Exception("请检查参数是否正确，up/down/left/right")

    def find_element_with_scroll(self, feature, direction="up"):
        """
        边滑边找某个元素的特征
        :param feature:元素的特征
        :param direction:方向
            "up":从下往上
            "down":从上往下
            "left":从右往左
            "down":从左往右
        :return:
        """
        page_source = ""
        while True:
            try:
                return self.base_find_element(feature)
            except Exception:
                self.scroll_page_one_time(direction)
                if page_source == self.driver.page_source:
                    print("到底了")
                    break
                page_source = self.driver.page_source
