# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/14
# 描述:
import logging
from time import sleep

from unit.public.UI.BasePage import BasePage

log = logging.getLogger(__name__)


class ModalBoxElement(BasePage):
    @staticmethod
    def get_search_input(title=None, placeholder=None):
        if title is not None:
            search_input = "//div[contains(@aria-label, '{}')]//div[@class='dictionary-search']//input".format(title)
        elif placeholder is not None:
            search_input = "//input[@placeholder='{}']".format(placeholder)
        else:
            search_input = "//div[@aria-modal='true']/div[@tabindex='-1']//input[@type='text']"
        return search_input

    def search_and_choose_items(self, title=None, placeholder=None, text=None, sing_choose=True, all_choose=False):
        search_input = self.get_search_input(title, placeholder)
        items = []
        if type(text) is str:
            items.append(text)
        elif type(text) is list:
            items = text
        else:
            log.error('查找参数格式不正确！')
        for item in items:
            self.type(search_input, item)
            self.enter(search_input)
            sleep(0.5)
            if all_choose is False:
                self.click_cell(title, placeholder, item) if sing_choose is True else 0
                sleep(1)
            else:
                all_checkbox = "//div[@aria-modal='true']/div[@tabindex='-1']//th[@colspan=1]//input[@aria-hidden='false']"
                self.click(all_checkbox)
                sleep(0.5)
        confirm_button = "{}/ancestor::div[@class='el-overlay']//div[@class='pagination-footer']//span[text()='确定']/parent::button[@aria-disabled='false']".format(
            search_input)
        self.click(confirm_button) if sing_choose is True or all_choose is True else 0

    def click_cell(self, title=None, placeholder=None, text=None):
        search_input = self.get_search_input(title, placeholder)
        cell = "{}/ancestor::div[@class='el-overlay']//div[contains(@class, 'tableNoDesign')]//tbody//tr[1]//div[text()='{}']".format(search_input, text)
        check_box = "{}/ancestor::tr/td[1]//label".format(cell)
        while "is-checked" not in self.get_attribute(check_box, 'class'):
            self.click(cell)
            sleep(1)
