import os
import time

import allure
import pytest
import re

from playwright.sync_api import expect

from BasePage.BasePage import BasePage
from BuildInLibrary.BuildInLibrary import BuildInLibrary
from Common.AllurePretty import PrettyAllure
from Pages.ProjectManagementPage.ProjectManagementPage import ProjectManagementPage
from Config import Config
from Utils.ReadYaml import ReadYaml
from Utils.VisualRegression import visual_regression


@allure.epic("CMS2.0UI自动化测试")
@allure.feature("工程管理")
class TestProjectManagement:
    @allure.story("新建空白工程")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestCreateProjectData.yaml")).read())
    def test_create_project(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.click_project_management()
        pmp.create_blank_project(CaseData["参数化值-工程名"])
        expect(pmp.page.get_by_role("cell", name=CaseData["参数化值-工程名"]).locator("span")).to_have_text(CaseData["参数化值-工程名"])

    @allure.story("新建工程文件夹")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestCreateProjectFolderData.yaml")).read())
    def test_create_folder(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.create_folder(CaseData["参数化值-文件夹名"])
        expect(pmp.page.get_by_role("cell", name=CaseData["参数化值-文件夹名"]).locator("span")).to_have_text(CaseData["参数化值-文件夹名"])

    @allure.story("导入本地工程")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestImportLocalProjectData.yaml")).read())
    def test_import_local_project(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.import_local_project()

    @allure.story("编辑工程_数据备份")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestDataBackupData.yaml")).read())
    def test_data_backup(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.data_backup(CaseData["参数化值-工程数据备份文件名"])
        expect(pmp.data_backup_success_toast_locator).to_have_text("备份成功")#断言备份成功

    @allure.story("编辑工程_数据恢复")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestDataRecoveryData.yaml")).read())
    def test_data_recovery(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.data_recovery(CaseData["参数化值-工程数据备份文件名"])
        expect(pmp.data_recovery_success_toast_locator).to_have_text("数据恢复成功")

    @allure.story("编辑工程_数据清除")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestDataClearData.yaml")).read())
    def test_data_clear(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.data_clear()
        expect(pmp.data_clear_success_toast_locator).to_have_text("清除成功")

    @allure.story("编辑工程_删除(批量删除)")
    @pytest.mark.run(order=1)
    @PrettyAllure.PrettyAllureCaseWarpper
    @pytest.mark.parametrize("CaseData", ReadYaml(os.path.join(Config.test_datas_dir, "ProjectManagementData/TestBatchDeleteProjectData.yaml")).read())
    def test_batch_delete_project(self, page, CaseData: dict):
        bp = BasePage(page)
        pmp = ProjectManagementPage(page)
        bp._goto_url_project_management()
        pmp.batch_delete_project()