from datetime import  timedelta
import sys
import os
# 获取当前脚本的绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 构建预定系统的绝对路径
预定系统_path = os.path.abspath(os.path.join(current_dir, '..','..','..'))
# 添加路径
sys.path.append(预定系统_path)
# 导入模块
from 预定系统.Base.base import *

# 获取当前脚本所在的目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 构建CSV文件的绝对路径
csv_file_path = os.path.join(current_dir, '../../测试数据/展厅巡检/会议预约.csv')
# 定义时间格式
time_format = "%H:%M"

class Exhibition_hall_inspection_000x:
    """
        执行指令：
            1.cd 预定系统
            2.hytest --report_title 会议预约测试报告 --report_url_prefix http://nat.ubainsyun.com:31133 --test 展厅巡检_0**
    """
    ddt_cases = read_csv_data(csv_file_path)

    def teststeps(self):
        """
            执行测试步骤以会议室删除功能。
            本函数根据参数输入会议室名称进行搜索并删除会议室，并检查删除后的提示信息是否与预期相符。
            字段含义：
              name:用例名称
              conference_name:会议室名称
              message_name:会议名称
              message_type:会议类型
              book_type:预约类型
              repetitive_cycle:重复周期
              host_name:主持人名称
              message_content:会议内容
              book_start_time:会议开始时间
              book_end_time:会议结束时间
              inside_participant_name:内部参会人名称
              outside_participant_name:外部参会人名称
              outside_participant_phone:外部参会人手机号码
              message_notification:消息提醒时间
              notification_method:通知方式
              check_text:检查提示文本
        """
        # 初始化通知文本为空字符串
        notify_text = ""
        # 从全局存储中获取webdriver实例
        wd = GSTORE['wd']
        name = self.name
        conference_name,message_name,book_type,issue_name,book_start_time,book_end_time,inside_participant_name,message_notification,notification_method,check_text = self.para

        # 在会议室列表搜索会议室
        STEP(1, f"在会议室列表搜索：{conference_name}")
        # 展开筛选下拉框
        safe_click((By.XPATH, "//i[@class='el-collapse-item__arrow el-icon-arrow-right']"),wd)
        sleep(1)
        # 输入会议室名称，并点击【查询】按钮
        safe_send_keys((By.XPATH, "//input[@placeholder='请输入会议室名称']"),conference_name,wd)
        safe_click((By.XPATH, "//button[@type='button']//span[contains(text(),'查询')]"),wd)
        sleep(1)
        INFO("检查搜索结果")
        # 获取搜索出的会议室名称
        notify_text = elment_get_text((By.XPATH, "//span[@class='MeetingCityList_name']"),wd)
        INFO(f"搜索结果为：{notify_text}")

        # 点击【会议预定】按钮，进入会议预定界面
        STEP(2, "点击【会议预定】按钮")
        safe_click((By.XPATH, "//span[@class='MeetingCityList_t_btn']"),wd)
        sleep(1)
        INFO("检查页面标题是否正确")
        # 获取页面标题
        notify_text = elment_get_text((By.XPATH, "//div[@class='reserve_Title']"),wd)
        INFO(f"页面标题为：{notify_text}")
        sleep(1)

        # 在会议预定第一步界面进行数据填入
        STEP(3,"会议第一步数据填写")
        INFO(f"输入会议名称：{message_name}，选择会议类型：视讯融合会议，选择预约类型：{book_type}，选择会议开始时间：{book_start_time}，选择会议结束时间：{book_end_time}")
        # 输入会议名称
        safe_send_keys((By.XPATH, "//input[@placeholder='请输入会议名称']"),message_name,wd)

        # 判断会议类型
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='SMC3.0视频会议']"),wd)
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='讯飞语音转录']"),wd)
        safe_click((By.XPATH, f"//div[@class='reserve_input']//span[@class='el-checkbox__label'][normalize-space()='腾讯会议']"),wd)
        sleep(2)

        # 预约类型
        INFO("预约类型为：普通会议")
        # 选择普通会议
        safe_click((By.XPATH, f"//span[contains(text(),'{book_type}')]"), wd)
        sleep(1)

        # 调用函数添加议题
        INFO("添加议题文件")
        # issue_send_and_upload(wd, 2, issue_name, "议题图片.png")

        # 将字符串转换为时间对象
        start_time = datetime.strptime(book_start_time, time_format)
        end_time = datetime.strptime(book_end_time, time_format)

        # 如果需要将时间差转换为分钟数
        time_diff_minutes = (end_time - start_time).total_seconds() / 60
        INFO(f"时间差为：time_diff_minutes")

        # 判断如果开始时间与结束时间相等，则不重复点击
        if book_start_time == book_end_time:
            INFO(f"开始时间与结束时间相等，选择时间块：{book_start_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
        elif book_start_time != book_end_time:
            INFO(f"开始时间与结束时间不相等，选择时间块：{book_start_time}，{book_end_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
            # 根据结束时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_end_time}'])[1]"), wd)
        sleep(2)

        # 判断时间差是否大于最大预约时长1小时，即60分钟
        if time_diff_minutes > 60:
            INFO(f"时间差大于最大预约时长1小时，选择【超出预约时长】")
            notify_text = get_notify_text(wd, (By.XPATH, "//p[contains(@class,'el-message__content')]"), "Message_Manage", "Message_Book", f"{name}_超出预约时长")
            INFO(f"提示信息为：{notify_text}")
            sleep(2)

            # 不选时间点击【下一步】查看提示信息
            INFO("不选时间点击【下一步】查看提示信息")
            safe_click((By.XPATH, "//div[contains(text(),'下一步')]"),wd)
            sleep(1)
            notify_text = get_notify_text(wd, (By.XPATH, "//p[contains(@class,'el-message__content')]"), "Message_Manage", "Message_Book", f"{name}请选择会议时间提示信息")
            INFO(f"提示信息为：{notify_text}")
            sleep(1)

            # 重新选择时间，将字符串转换为时间对象
            time_obj = datetime.strptime(book_end_time, time_format)
            time_obj_minus_5_minutes = time_obj - timedelta(minutes=30)

            # 将时间对象转换回字符串
            book_end_time = time_obj_minus_5_minutes.strftime(time_format)
            INFO(f"重新选择结束时间为：{book_end_time}")
            # 根据开始时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_start_time}'])[1]"), wd)
            # 根据结束时间选择对应时间块，时间格式为：**:**
            safe_click((By.XPATH, f"(//div[normalize-space()='{book_end_time}'])[1]"), wd)
            sleep(2)

        # 在会议预定第二步界面进行数据填入
        STEP(4, "会议第二步数据填写")
        # 点击【下一步】进入会议预定第二步界面
        safe_click((By.XPATH, "//div[contains(text(),'下一步')]"),wd)
        sleep(1)
        # 根据参会人名称搜搜对应参会人
        safe_send_keys((By.XPATH, "//input[@placeholder='账号/用户名']"),inside_participant_name,wd)
        send_keyboard((By.XPATH, "//input[@placeholder='账号/用户名']"), wd)
        sleep(1)
        # 选择内部参会人
        INFO(f"选择内部参会人：{inside_participant_name}")
        safe_click((By.XPATH,"//tbody//tr//td//div//label//span//span"),wd)
        # 获取已选的参会人名称
        notify_text = elment_get_text((By.XPATH, f"//span[normalize-space()='{inside_participant_name}']"),wd)
        INFO(f"已选择参会人：{notify_text}")

        # 点击【下一步】按钮
        safe_click((By.XPATH,"//div[contains(text(),'下一步')]"),wd)
        sleep(1)
        safe_click((By.XPATH,"//div[contains(text(),'下一步')]"),wd)

        # 进入会议预约第三步进行填写
        STEP(5, "会议第三步数据填写")
        INFO("检查页面标题是否正确")
        sleep(1)
        # 获取页面标题
        notify_text = elment_get_text((By.XPATH,"//h2[contains(text(),'消息提醒')]"),wd)
        INFO(f"页面标题为：{notify_text}")

        # 选择消息提醒时间
        INFO(f"选择消息提醒时间：{message_notification}")
        # 判断消息提醒时间是否为全选
        if message_notification == "全选":
            # 点击【全选】按钮
            safe_click((By.XPATH,"//div[@class='Reminds']//span[@class='el-checkbox__label'][contains(text(),'全选')]"),wd)
            sleep(1)
        else:
            # 选择对应的消息提醒时间
            safe_click((By.XPATH, f"//span[contains(text(),'{message_notification}')]"), wd)
            sleep(1)

        # 选择通知方式
        INFO(f"选择通知方式：{notification_method}")
        if notification_method == "全选":
            # 点击【全选】按钮
            safe_click((By.XPATH,"//div[@class='Sendout']//span[@class='el-checkbox__label'][contains(text(),'全选')]"),wd)
        else:
            # 选择对应的通知方式
            safe_click((By.XPATH, f"//span[contains(text(),'{notification_method}')]"), wd)
            sleep(1)

        # 点击【确定】按钮
        STEP(6, "点击【确定】按钮")
        safe_click((By.XPATH,"//div[contains(text(),'确定')]"),wd)

        # 点击最后【确定】按钮，预定成功后会跳转至已预定界面
        STEP(7, "点击【确定】按钮")
        safe_click((By.XPATH,"//div[contains(@aria-label,'会议信息')]//span[contains(text(),'确定')]"),wd)
        sleep(3)

        # 搜索会议名称
        safe_send_keys((By.XPATH,"//input[@placeholder='输入关键字搜索']"),message_name,wd)
        send_keyboard((By.XPATH,"//input[@placeholder='输入关键字搜索']"), wd)

        # 在已预定列表界面【提前开始】会议
        INFO("提前开始会议")
        safe_click((By.XPATH,"//span[contains(text(),'更多操作')]"),wd)
        safe_click((By.XPATH,"//li[contains(text(),'会议状态')]"),wd)
        sleep(2)
        safe_click((By.XPATH,"//div[@slot='footer']//span[contains(text(),'确定')]"),wd)
        sleep(10)

        STEP(8, "检查门口屏会议显示")
        # 进入后台-安卓信息界面截屏查看门口屏显示
        safe_click((By.XPATH,"(//img[contains(@title,'后台系统')])[1]"),wd)
        sleep(1)
        safe_click((By.XPATH,"(//span[contains(text(),'设备管理')])[1]"),wd)
        safe_click((By.XPATH,"(//li[contains(text(),'安卓信息')])[1]"),wd)
        sleep(2)
        safe_click((By.XPATH,"(//span[contains(text(),'应用截屏')])[1]"),wd)
        sleep(20)
        # 截屏查看门口屏的显示
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "Android", "截屏")
        # 返回上一级界面
        safe_click((By.XPATH,"//div[@aria-label='门口屏截屏']//i[@class='el-dialog__close el-icon el-icon-close']"), wd)

        STEP(9, "检查会控界面功能")
        # 返回前台已预定列表界面
        safe_click((By.XPATH,"//img[@title='返回预定首页']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//span[contains(text(),'已预订会议')]"),wd)
        sleep(1)

        # 点击【进入会控】
        INFO("进入会控")
        safe_click((By.XPATH,"//span[contains(text(),'进入会控')]"),wd)
        sleep(5)

        # 切换窗口2，转到会控界面
        wd.switch_to.window(wd.window_handles[1])

        # 会控操作
        STEP(10, "检查会控界面功能")
        INFO("检查会控界面功能")

        # 添加SMC会场
        INFO("添加SMC会场：展厅指挥中心、展厅会议室")
        safe_click((By.XPATH,"//body/div[@id='app']/div[@id='Energy']/div[@class='main']/div[@class='mainList']/div[@class='main_l']/div[@class='mainFNC']/img[1]"),wd)
        safe_send_keys((By.XPATH,"//input[@placeholder='搜索与会方']"),"展厅指挥中心",wd)
        sleep(1)
        send_keyboard((By.XPATH,"//input[@placeholder='搜索与会方']"), wd)
        sleep(1)
        safe_click((By.XPATH,"//div[contains(@class,'el-table el-table--fit el-table--enable-row-hover el-table--enable-row-transition')]//div[contains(@class,'el-table__header-wrapper')]//span[contains(@class,'el-checkbox__inner')]"),wd)

        # 添加展厅指挥中心
        INFO("添加腾讯会场")
        safe_click((By.XPATH,"//div[@id='tab-TencentUser']"),wd)
        safe_click((By.XPATH,"//li[normalize-space()='6']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//li[normalize-space()='8']"),wd)
        sleep(1)
        safe_click((By.XPATH,"//body[1]/div[1]/div[1]/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/div[3]/div[1]/div[1]/div[1]/div[3]/table[1]/tbody[1]/tr[7]/td[1]/div[1]/label[1]/span[1]/span[1]"),wd)
        sleep(1)
        safe_click((By.XPATH,"//div[contains(text(),'确定')]"),wd)
        sleep(1)

        # 一键呼叫会场
        # safe_click((By.XPATH,"//body/div[@id='app']/div[@id='Energy']/div[contains(@class,'main')]/div[contains(@class,'mainList')]/div[contains(@class,'main_l')]/div[contains(@class,'mainFNC')]/img[1]"),wd)

        # 选择分屏功能
        safe_click((By.XPATH, "//li[4]//img[1]"), wd)

        # 将左侧会场拖拽到右侧画面进行播放
        INFO("将左侧会场拖拽到右侧画面进行播放")
        double_click_and_drag((By.XPATH,"//div[contains(@class,'cell')]//span[contains(text(),'展厅指挥中心')]"), (By.XPATH,"//div[@id='screens4']//div[1]//p[1]//span[1]") , wd)
        sleep(1)
        double_click_and_drag((By.XPATH,"//div[@class='cell']//span[contains(text(),'391558766')]"), (By.XPATH,"//div[@id='screens4']//div[1]//p[1]//span[1]") , wd)
        sleep(4)
        SELENIUM_LOG_SCREEN(wd, "50%", "Exhibit_Inspect", "MeetingControl", "会场控制")