# author:qinguanglei
# ddate:2023/11/16 16:46
import datetime
import random
import time
import traceback
import allure
from common import constant
from common.logger import log
from common.read_yaml import get_filed_from_yaml
from common.save_screenshot import ScreenShot
from common.verifycode import VerifyCode
from test_login.page_login import PageLogin

module = 'test_login'


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

    @allure.step("打开浏览器")
    def open_browser(self):
        try:
            log.info('当前url： %s', get_filed_from_yaml(module, "data", "url"))
            self.page_login.open_page(get_filed_from_yaml(module, "data", "url"))
        except Exception as e:
            log.error(f'打开浏览器问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False

    @allure.step("输入账号密码")
    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)
        except Exception as e:
            log.error(f'账号密码输入过程出现问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False

    @allure.step('输入验证码')
    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)
            log.info(f'输入的验证码为：{verify_code_text}')
        except Exception as e:
            log.error(f'获取验证码问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False

    @allure.step('点击登录')
    def click_login_button(self):
        try:
            # 获取登录按钮
            login_button_element = self.page_login.get_login_button()
            # 点击登录
            self.page_login.single_click(login_button_element)
        except Exception as e:
            log.error(f'点击登录动作出现问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False

    @allure.step('判断是否登录成功')
    def judge_result(self):
        try:
            # 判断登录是否成功，登录成功会切换到另外一个页面，通过页面title来判断是否切换成功
            if self.page_login.get_window_title() == constant.LOGIN_SUCCESS:
                assert True
            else:
                log.error('案例执行失败')
                log.error(f'self.page_login.get_window_title()={self.page_login.get_window_title()}')
                log.error(f'constant.LOGIN_SUCCESS={constant.LOGIN_SUCCESS}')
                log.error('self.page_login.get_window_title()与constant.LOGIN_SUCCESS不一致')
                assert False
        except Exception as e:
            log.error(f'判断是否登录成功问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False

    @allure.step('登录错误结果校验')
    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):
                assert True
            else:
                log.error('登录错误结果校验，案例执行失败')
                log.error(
                    f'self.page_login.get_text(login_result_element)={self.page_login.get_text(login_result_element)}')
                log.error(f'result_value={result_value}')
                log.error('self.page_login.get_text(login_result_element)与result_value不一致')
                assert False
        except Exception as e:
            log.error(f'登录错误结果校验问题：{e.__doc__}{traceback.format_exc()}')
            # 截图
            allure.attach.file(source=ScreenShot(self.driver, module, int(time.time())).screen_shot(), name='失败截图',
                               attachment_type=allure.attachment_type.PNG)
            assert False
