import time

import allure
import pytest

from common.DButils import DButils
from common.untils import DriverTools
from config import user_name, password, word_text, \
    updata_traditional_path, updata_music_path, updata_qrcode_path, updata_video_path, updata_picture_path, \
    knowledge_text, login_url, userid, updata_pdf_path, updata_pdf_name, \
    example_knowlage, example_Content_risk, find_word_text
from paga.AIEditingRoomPage import AIER
from paga.AiPrequalificationPage import Aiprequalification
from paga.AiReviewPage import Aireview
from paga.LoginPage import Login


# AI审校ui自动化测试
class TestAireview:
    # 类前置
    def setup_class(self):
        self.login = Login()
        self.AIroom = AIER()
        self.aiprequailification = Aiprequalification()
        self.dbtuils = DButils()
        self.aireview = Aireview()

    def setup_method(self):
        self.driver = DriverTools.get_driver().get(login_url)
        self.login.login(user_name, password)
        self.AIroom.to_AI_review()

    # 类后置
    def teardown_class(self):
        DriverTools.quit_driver()

    # 图书审校流程
    @allure.feature("图书审校流程")
    def test_book_review(self):
        self.aireview.book_review(updata_pdf_path)
        time.sleep(3)
        data = self.dbtuils.select_sql(
            f'SELECT file_name ,status  FROM aireview.assistant_task where create_user ={userid} and assistant_code '
            '="diction" order by biz_id desc limit 1;')

        assert data[0][0] == updata_pdf_name

    # 示例文件审校
    @allure.feature("示例审校流程")
    @pytest.mark.parametrize("example_file_name,error_count,count", [example_knowlage, example_Content_risk])
    def test_example(self, example_file_name, error_count, count):
        if count == 1:
            self.aireview.review_example_knowlage_file()
            time.sleep(5)
            data = self.dbtuils.select_sql(
                f"select biz_id from aireview.assistant_task where file_name ='{example_file_name}' and DATE("
                "create_time) = CURDATE()  order by create_time desc ;")
            result = self.dbtuils.select_sql(
                f"SELECT coordinates FROM aireview.assistant_fusion_item where biz_id = {data[0][0]}")
            assert len(result) == error_count
            for i in range(len(result)):
                assert result[i] is not None
        elif count == 2:
            self.aireview.review_Content_risk_example_file()
            time.sleep(5)
            data = self.dbtuils.select_sql(
                f"select biz_id from aireview.assistant_task where file_name ='{example_file_name}' and DATE("
                "create_time) = CURDATE()  order by create_time desc ;")
            result = self.dbtuils.select_sql(
                f"SELECT coordinates FROM aireview.assistant_fusion_item where biz_id = {data[0][0]}")
            assert len(result) == error_count
            for i in range(len(result)):
                assert result[i] is not None

    # 文本审校流程
    @allure.feature("文本审校流程")
    def test_text_revierw(self):
        self.aireview.text_revierw(word_text)
        time.sleep(5)
        data = self.dbtuils.select_sql(f"SELECT file_type  FROM aireview.assistant_task where create_user ={userid}  "
                                       "order by biz_id desc limit 1;")
        assert data[0][0] == "text"

    # 繁体字审校流程
    @allure.feature("繁体字审校流程")
    def test_traditional_review(self):
        self.aireview.traditional_review(updata_traditional_path)
        time.sleep(5)
        data = self.dbtuils.select_sql(f"SELECT assistant_code  FROM aireview.assistant_task where create_user ={userid} "
                                       "order by biz_id desc limit 1;")
        assert data[0][0] == "unsimplified"

    # 多媒体审校流程
    @allure.feature("多媒体审校流程")
    def test_media_review(self):
        self.aireview.media_review([updata_picture_path, updata_video_path, updata_qrcode_path, updata_music_path])
        time.sleep(5)

    # 查词查术语流程
    @allure.feature("查词查术语流程")
    def test_find_word(self):
        self.aireview.find_word(find_word_text)
        time.sleep(5)

    # 知识查证流程
    @allure.feature("知识查证流程")
    def test_find_knowledge(self):
        self.aireview.find_knowledge(knowledge_text)
        time.sleep(5)
