# author:qinguanglei
# ddate:2023/11/16 16:46
import traceback

from actions.actions_login.page_login import PageLogin
from common import constant
from common.logger import logger
from common.read_yaml import get_filed_from_yaml
from common.verifycode import VerifyCode
from common.common_func import commonFuc
from airtest.core.api import *

module = 'login'


class StepsLogin(object):
    def __init__(self, driver):
        self.page_login = PageLogin(driver)
        self.driver = driver

    def open_browser(self):
        try:
            logger.info('当前url： %s', get_filed_from_yaml(module, "data", "url"))
            self.page_login.open_page(get_filed_from_yaml(module, "data", "url"))
            commonFuc.step_ok_info(commonFuc(), "浏览器打开成功。")
        except Exception as e:
            commonFuc.step_fail_info(commonFuc(), self.driver, "浏览器打开失败。")
            logger.error(f'打开浏览器问题：{e.__doc__}{traceback.format_exc()}')

    def enter_account_password(self, flag):
        try:
            # 获取账号输入元素
            account_element = self.page_login.get_account()
            # 获取密码输入元素
            password_element = self.page_login.get_password()
            # 获取账号与密码
            account = get_filed_from_yaml(module, "data", "account")
            password = get_filed_from_yaml(module, "data", "password")
            # 输入账号与密码
            # flag为0,1,2，0输入账号与密码，1输入账号，2输入密码
            # 3账号输入错误，4密码输入错误
            if flag == 3:
                # 保证账号错误
                account = '1' + account
            if flag == 4:
                # 保证密码错误
                password = '1' + password
            # 如下两条，前者保证1，只输入账号，后者保证2，只输入密码
            if flag == 0 or flag == 1 or flag == 3 or flag == 4:
                self.page_login.input_text(account_element, account)
            if flag == 0 or flag == 2 or flag == 3 or flag == 4:
                self.page_login.input_text(password_element, password)
            commonFuc.step_ok_info(commonFuc(), "成功输入账号密码。")
        except Exception as e:
            commonFuc.step_fail_info(commonFuc(), self.driver, "输入账号密码失败。")
            logger.error(f'账号密码输入过程出现问题：{e.__doc__}{traceback.format_exc()}')

    def enter_verify_code(self, flag):
        try:
            # 获取验证码
            # 先获取验证码图片元素
            verify_code_element = self.page_login.get_verify_image()
            # 获取验证码输入元素
            verify_input_element = self.page_login.get_verify_code()
            # 获取验证码中的文字信息
            verify_code_text = VerifyCode(verify_code_element, self.driver).indentify_verify_image()
            # 填充验证码文字
            # flag为0，输入正确的验证码，为1输入错误的验证码，为2不输入验证码
            if flag == 1:
                verify_code_text = '4571'
            if flag == 1 or flag == 0:
                self.page_login.input_text(verify_input_element, verify_code_text)
            logger.info(f'输入的验证码为：{verify_code_text}')
            commonFuc.step_ok_info(commonFuc(), "成功输入验证码。")
        except Exception as e:
            logger.error(f'获取验证码问题：{e.__doc__}{traceback.format_exc()}')
            commonFuc.step_fail_info(commonFuc(), self.driver, "输入验证码失败。")

    def click_login_button(self):
        try:
            # 获取登录按钮
            login_button_element = self.page_login.get_login_button()
            # 点击登录
            self.page_login.single_click(login_button_element)
            commonFuc.step_ok_info(commonFuc(), "点击登录正常。")
        except Exception as e:
            logger.error(f'点击登录动作出现问题：{e.__doc__}{traceback.format_exc()}')
            commonFuc.step_fail_info(commonFuc(), self.driver, "点击登录失败。")

    def judge_result(self):
        try:
            # 判断登录是否成功，登录成功会切换到另外一个页面，通过页面title来判断是否切换成功
            if self.page_login.get_window_title() == constant.LOGIN_SUCCESS:
                commonFuc.step_ok_info(commonFuc(), "成功登录。")
            else:
                logger.error('案例执行失败')
                logger.error(f'self.page_login.get_window_title()={self.page_login.get_window_title()}')
                logger.error(f'constant.LOGIN_SUCCESS={constant.LOGIN_SUCCESS}')
                logger.error('self.page_login.get_window_title()与constant.LOGIN_SUCCESS不一致')
                commonFuc.step_fail_info(commonFuc(), self.driver, "登录失败。")
        except Exception as e:
            logger.error(f'判断是否登录成功问题：{e.__doc__}{traceback.format_exc()}')
            commonFuc.step_fail_info(commonFuc(), self.driver, "登录失败。")

    def judge_result_error(self, flag):
        try:
            # 获取登录结果
            login_result_element = self.page_login.get_login_result()
            # 断言案例是否执行成功
            # flag判断
            # 登录验证码错误-----对应flag为0
            # VERIFY_CODE_ERROR = "验证码错误！！"
            # 登录用户名或者密码错误-----对应flag为1
            # USER_PASSWORD_ERROR = "用户名或密码错误"
            # 登录不输入用户名或者密码-----对应flag为2
            # NO_USER_OR_PASSWORD = "不适当的用户名或口令，请重新确认"
            # 登录不输入验证码-----对应flag为3
            # NO_VERIFY_CODE = "请输入验证码后再登录！！"
            # 结果值
            result_value = ' '
            if flag == 0:
                result_value = constant.VERIFY_CODE_ERROR
            if flag == 1:
                result_value = constant.USER_PASSWORD_ERROR
            if flag == 2:
                result_value = constant.NO_USER_OR_PASSWORD
            if flag == 3:
                result_value = constant.NO_VERIFY_CODE

            if result_value in self.page_login.get_text(login_result_element):
                commonFuc.step_ok_info(commonFuc(), "登录结果错误与预期错误一致。")
            else:
                logger.error('登录错误结果校验，案例执行失败')
                logger.error(
                    f'self.page_login.get_text(login_result_element)={self.page_login.get_text(login_result_element)}')
                logger.error(f'result_value={result_value}')
                logger.error('self.page_login.get_text(login_result_element)与result_value不一致')
                commonFuc.step_fail_info(commonFuc(), self.driver, "登录结果错误与预期错误不一致。")
        except Exception as e:
            logger.error(f'登录错误结果校验问题：{e.__doc__}{traceback.format_exc()}')
            commonFuc.step_fail_info(commonFuc(), self.driver, "登录结果错误与预期错误不一致。")
