# Time    : 2024/3/7 18:02
# Author  : wangchao
# File    : VariablePage.py
# Description : Page object of VariableManagementPage
import re
from time import sleep

import allure
from BasePage.BasePage import BasePage
from Config import Config
from Utils.logger_util import write_log


class VariableManagementPage(BasePage):

    def __init__(self, page):
        super().__init__(page) # 调用基类构造函数
        """元素定位器"""
        # 开发版页面左侧菜单栏
        self.variable_menu_locator = page.get_by_role("listitem").filter(has_text="变量").locator("i")  # 变量菜单
        # 全局导入变量
        self.import_variable_locator = page.locator("//*[@id=\"app\"]/div/div[1]/div[2]/div/div/div/div[2]/button[6]")
        self.import_mode_choice_up_down_menu_locator = page.get_by_role("dialog", name="导入").locator("i").first
        self.choice_import_mode_locator = page.get_by_text("覆盖导入")
        self.choice_import_mode_confirm_locator = page.get_by_role("button", name="确定")
        self.import_variable_success_toast_locator = page.locator("//*[@id=\"message_1\"]/p")
        # 全局导出变量
        self.export_variable_locator = page.locator("//*[@id=\"app\"]/div/div[1]/div[2]/div/div/div/div[2]/button[5]")
        self.export_variable_success_toast_locator = page.locator("//*[@id=\"message_1\"]/p")
        # IO变量导入
        self.IO_variable_group_check_locator = page.locator(".el-tooltip__trigger > .iconfont").first
        self.IO_variable_group_import_locator = page.get_by_role("menuitem", name="导入")
        # IO变量导出
        self.IO_variable_group_export_locator = page.get_by_role("menuitem", name="导出")
        # 内部变量导入
        self.click_inner_channel_tab_locator = page.get_by_role("button", name="内部通道")
        self.click_inner_variable_channel_locator = page.locator("#app").get_by_text("内部变量") # 点击内部通道下的内部变量
        self.inner_variable_edit_menu_locator = page.locator(".el-tooltip__trigger > .iconfont").first # 点击“内部变量”编辑按钮
        self.inner_variable_group_import_locator = page.get_by_role("menuitem", name="导入")
        self.inner_variable_import_mode_choice_up_down_menu_locator = page.get_by_role("dialog", name="导入").locator("i").first
        self.inner_variable_import_mode_locator = page.get_by_text("覆盖导入")
        self.inner_variable_import_mode_confirm_locator = page.get_by_role("button", name="确定")
        self.inner_variable_import_success_toast_locator = page.locator("//*[@id=\"message_1\"]/p")
        # 内部变量导出
        self.inner_variable_group_export_locator = page.get_by_role("menuitem", name="导出")
        self.inner_variable_export_success_toast_locator = page.locator("//*[@id=\"message_1\"]/p")
        # 批量添加变量（IO变量）
        self.check_io_variable_locator = page.locator(".vxe-cell > .vxe-cell--checkbox > span:nth-child(2)").first # 选择IO通道变量列表第一个变量
        self.add_io_variables_in_batches_button_locator = page.locator("//button[@class='iconfont icon-piliangtianjia']") # 点击批量添加变量按钮
        # 变量批量处理
        self.click_io_variable_group_locator = page.get_by_title("IO_Channel_Modbus_TCP").get_by_text("IO_Channel_Modbus_TCP") # 点击IO通道变量组
        self.check_for_all_io_variables_locator = page.locator("//span[@class='vxe-cell--title']//span[@class='vxe-checkbox--icon vxe-checkbox--unchecked-icon']") # 全选变量
        self.deal_with_variable_in_batches_button_locator = page.locator("//button[@class='iconfont icon-bianliangpiliangchuli']") # 点击批量处理变量按钮
        self.variable_read_and_write_mode_up_and_down_button_locator = page.locator("div").filter(has_text=re.compile(r"^读写访问：$")).locator("i") # 变量读写方式下拉框
        self.check_read_and_write_mode_of_variable_locator = page.get_by_role("tooltip", name="请选择 读写 只读 只写").get_by_text("读写") # 选择读写
        self.batch_modification_variable_confirm_button_locator = page.get_by_role("button", name="确定") # 批量修改页面确定
        self.batch_modification_variable_save_changes_locator = page.get_by_role("button", name="保存") # 保存修改
        self.batch_modification_variable_success_toast_locator = page.locator("//*[@id=\"message_1\"]/p") # 保存成功toast

    """变量全局导入"""
    @allure.step("变量全局导入")
    def import_global_variable_group(self, import_file_name):
        # 点击变量菜单
        self._click(pw_locator=self.variable_menu_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())  # 弹窗处理
        # 点击变量全局导入按钮
        self._click(pw_locator=self.import_variable_locator)
        # 导入方式下拉框
        self._click(pw_locator=self.import_mode_choice_up_down_menu_locator)
        # 点击覆盖导入
        self._click(pw_locator=self.choice_import_mode_locator)
        # 点击确定按钮并选择文件
        with self.page.expect_file_chooser() as fc_info:
            self._click(pw_locator=self.choice_import_mode_confirm_locator)
        file_chooser = fc_info.value
        file_chooser.set_files(import_file_name)

    """变量全局导出"""
    @allure.step("变量全局导出")
    def export_global_variable_group(self):
        # 点击变量菜单
        self._click(pw_locator=self.variable_menu_locator)
        # 点击变量全局导出
        self._click(pw_locator=self.export_variable_locator)

    """IO变量导入"""
    @allure.step("IO变量导入")
    def import_io_variable_group(self, import_file_name):
        # 点击变量菜单
        self._click(pw_locator=self.variable_menu_locator)
        # 选择IO变量组
        self.page.on("dialog", lambda dialog1: dialog1.accept())  # IO变量组编辑弹窗处理
        self._click(pw_locator=self.IO_variable_group_check_locator)
        # 点击导入
        self.page.on("dialog", lambda dialog2: dialog2.accept())  # 导入方式选择弹窗处理
        self._click(pw_locator=self.IO_variable_group_import_locator)
        # 导入方式下拉框
        self._click(pw_locator=self.import_mode_choice_up_down_menu_locator)
        # 点击覆盖导入
        self._click(pw_locator=self.choice_import_mode_locator)
        # 点击确定按钮并选择文件
        with self.page.expect_file_chooser() as fc_info:
            self._click(pw_locator=self.choice_import_mode_confirm_locator)
        file_chooser = fc_info.value
        file_chooser.set_files(import_file_name)

    """IO变量导出"""
    @allure.step("IO变量导出")
    def export_io_variable_group(self):
        # 点击变量菜单
        self._click(pw_locator=self.variable_menu_locator)
        # 选择IO变量组
        self._click(pw_locator=self.IO_variable_group_check_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())  # 弹窗处理
        # 点击导出
        self._click(pw_locator=self.IO_variable_group_export_locator)

    """内部变量导入"""
    @allure.step("内部变量导入")
    def import_internal_variable_group(self, import_file_name):
        # 点击内部通道tab
        self._click(pw_locator=self.click_inner_channel_tab_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())  # 内部变量组编辑菜单弹窗处理
        # 点击内部变量组编辑菜单
        self._click(pw_locator=self.inner_variable_edit_menu_locator)
        # 点击导入按钮
        self._click(pw_locator=self.inner_variable_group_import_locator)
        # 导入方式下拉框
        self._click(pw_locator=self.inner_variable_import_mode_choice_up_down_menu_locator)
        # 点击覆盖导入
        self._click(pw_locator=self.inner_variable_import_mode_locator)
        # 点击确定按钮并选择文件
        with self.page.expect_file_chooser() as fc_info:
            self._click(pw_locator=self.inner_variable_import_mode_confirm_locator)
        file_chooser = fc_info.value
        file_chooser.set_files(import_file_name)

    """内部变量导出"""
    @allure.step("内部变量导出")
    def export_internal_variable_group(self):
        # 点击内部通道tab
        self._click(pw_locator=self.click_inner_channel_tab_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())  # 内部变量组编辑菜单弹窗处理
        # 点击内部变量组编辑菜单
        self._click(pw_locator=self.inner_variable_edit_menu_locator)
        # 点击导出
        self._click(pw_locator=self.inner_variable_group_export_locator)

    """批量处理变量"""
    @allure.step("批量处理变量")
    def batch_handle_variable(self):
        # 点击IO通道变量组
        self._click(pw_locator=self.click_io_variable_group_locator)
        # 等待2S
        self.page.wait_for_timeout(2000)
        # 全选IO变量
        self._click(pw_locator=self.check_for_all_io_variables_locator)
        self.page.on("dialog", lambda dialog: dialog.accept())  # 变量批量处理弹窗处理
        # 点击批量处理变量按钮
        self._click(pw_locator=self.deal_with_variable_in_batches_button_locator)
        # 变量读写方式下拉框
        self._click(pw_locator=self.variable_read_and_write_mode_up_and_down_button_locator)
        # 选择读写
        self._click(pw_locator=self.check_read_and_write_mode_of_variable_locator)
        # 批量修改页面确定
        self._click(pw_locator=self.batch_modification_variable_confirm_button_locator)
        # 保存修改
        self._click(pw_locator=self.batch_modification_variable_save_changes_locator)

