import os
import random
import re
import time

import allure
from playwright.sync_api import sync_playwright

from BasePage.BasePage import BasePage
from BuildInLibrary.BuildInLibrary import BuildInLibrary


class ProjectManagementPage(BasePage):

    def __init__(self, page):
        super().__init__(page)
        """工程管理页面元素定位器"""
        # 创建空白工程
        self.add_project_button_locator = page.locator(".el-tooltip__trigger > .iconfont").first
        self.create_blank_project_locator = page.get_by_role("menuitem", name="新建空白工程")
        self.project_name_input_locator = page.get_by_placeholder("请输入工程名称")
        self.create_project_confirm_button_locator = page.get_by_role("button", name="确认")
        # 新建文件夹
        self.create_folder_button_locator = page.get_by_title("新建文件夹")
        self.input_folder_name_locator = page.get_by_placeholder("请输入文件夹名称")
        self.create_folder_confirm_button_locator = page.get_by_role("button", name="确认")
        # 导入本地工程
        self.add_project_button_locator = page.locator(".el-tooltip__trigger > .iconfont").first
        self.import_local_project_locator = page.get_by_role("menuitem", name="导入本地工程")
        # 选择工程(测试工程1)
        self.select_project_locator = page.locator("tr:nth-child(2) > td > .vxe-cell > .vxe-cell--checkbox > span:nth-child(2)")
        #数据备份
        self.select_project_locator1 = page.get_by_role("row", name="222222 CMS 2024/06/26 13:45:48 ").locator("i") #name的值要根据实际值来调整
        self.data_backup_locator = page.get_by_role("menuitem", name="数据备份")
        self.select_backup_data_locator = page.get_by_role("dialog", name="数据备份").locator("label span").nth(1)
        self.backup_data_confirm_button_locator = page.locator(".el-button.el-button--primary.cms-settings-button")
        self.backup_file_name = page.get_by_placeholder("请输入备份文件名")
        self.input_backup_file_name_confirm_button = page.locator("//*[@id=\"app\"]/div/div[1]/div[2]/div/div[3]/div/div/div/div[2]/div/div[2]/div/div/div/div[3]/div/div/button[2]")
        self.data_backup_success_toast_locator = page.locator("//*[@id=\"message_1\"]")
        #数据还原
        self.data_recovery_locator = page.get_by_role("menuitem", name="数据恢复")
        self.data_recovery_success_toast_locator = page.locator("//*[@id=\"message_1\"]")
        #数据清除
        self.data_clear_locator = page.get_by_role("menuitem", name="数据清除")
        self.select_clear_data_locator = page.get_by_role("dialog", name="数据清除").locator("label span").nth(1)
        self.clear_data_confirm_button_locator = page.locator("//*[@id=\"app\"]/div/div[1]/div[2]/div/div[3]/div/div/div/div[3]/div/div/button[2]")
        self.data_clear_success_toast_locator = page.locator("//*[@id=\"message_1\"]")
        #删除工程
        self.delete_project_locator = page.locator("//*[@id=\"app\"]/div/div[1]/div[2]/div/div[1]/div[2]/div/div[2]/div[1]/div[1]/table/thead/tr/th[1]/div/span/label/span/span")


    """进入工程管理页面"""
    def goto_project_management_page(self,url):
        self._goto_url(url)

    """点击工程管理"""
    def click_project_management(self):
        self._click(pw_locator=self.add_project_button_locator)

    """新建空白工程"""
    def create_blank_project(self, project_name):
        self._click(pw_locator=self.add_project_button_locator)
        self._click(pw_locator=self.create_blank_project_locator)
        self._click(pw_locator=self.project_name_input_locator)
        self._fill(project_name,pw_locator=self.project_name_input_locator)
        self._click(pw_locator=self.create_project_confirm_button_locator)

    """导入本地工程"""
    def import_local_project(self):
        self._click(pw_locator=self.add_project_button_locator)
        with self.page.expect_file_chooser() as fc_info:
            self._click(pw_locator=self.import_local_project_locator)  # 点击上传文件按钮
        file_chooser = fc_info.value
        bl = BuildInLibrary()
        file_chooser.set_files(bl.get_path(f"UI自动化测试工程.cmsproj"))  # 上传文件 ，或者上传多个文件file_chooser.set_files(['file1.txt', 'file2.txt'])
        self.page.wait_for_load_state()

    """新建文件夹"""
    def create_folder(self, folder_name):
        self._click(pw_locator=self.create_folder_button_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())
        self._fill(folder_name,pw_locator=self.input_folder_name_locator)
        self._click(pw_locator=self.create_folder_confirm_button_locator)

    """选择工程"""
    def select_project(self, project_name):
        with self.page.expect_popup() as page1_info:
            self.page.get_by_role("cell", name=project_name).get_by_text(project_name).click()
        return page1_info.value

    """编辑工程_移动到"""
    def move_to(self):
        pass

    """编辑工程_重命名"""
    def rename_project(self):
        pass

    """编辑工程_创建副本"""
    def create_copy(self):
        pass

    """编辑工程_批量删除"""
    def batch_delete_project(self):
        self._click(pw_locator=self.delete_project_locator)

    """编辑工程_数据备份"""
    def data_backup(self,backup_file_name):
        self.page.on("dialog", lambda dialog: dialog.accept())  # 工程编辑弹窗处理
        self._click(pw_locator=self.select_project_locator1)
        self._click(pw_locator=self.data_backup_locator)
        self.page.on("dialog", lambda dialog1: dialog1.accept())  # 选择备份数据弹窗处理
        self._click(pw_locator=self.select_backup_data_locator)
        self._click(pw_locator=self.backup_data_confirm_button_locator)
        self.page.on("dialog", lambda dialog2: dialog2.accept())  # 输入备份文件名称弹窗处理
        self._fill(backup_file_name,pw_locator=self.backup_file_name)
        self._click(pw_locator=self.input_backup_file_name_confirm_button)

    """编辑工程_数据恢复"""
    def data_recovery(self,backup_file_name):
        self.page.on("dialog", lambda dialog: dialog.accept()) #功能编辑弹框处理
        self._click(pw_locator=self.select_project_locator1)
        with self.page.expect_file_chooser() as fc_info:
            self._click(pw_locator=self.data_recovery_locator)
        file_chooser = fc_info.value
        file_chooser.set_files(backup_file_name)

    """编辑工程_数据清除"""
    def data_clear(self):
        self.page.on("dialog", lambda dialog: dialog.accept()) #工程编辑弹框处理
        self._click(pw_locator=self.select_project_locator1)
        self.page.on("dialog", lambda dialog1: dialog1.accept()) #选择清除数据弹框处理
        self._click(pw_locator=self.data_clear_locator)
        self._click(pw_locator=self.select_clear_data_locator)
        self._click(pw_locator=self.clear_data_confirm_button_locator)