def check_status():
    """
    0 >> no setting
    1 >> domain_1
    2 >> domain_2
    :return:
    """
    SwitchThinProMode(switch_to='admin')
    time.sleep(20)
    open_window("active directory")
    ad = get_position('ad.png')
    if not ad:
        Logger().info("find ad fail")
        return
    authenticate = get_position('authenticate.png')
    if authenticate:
        close_window()
        return 0
    authenticate_checked = get_position('authenticate_checked.png')
    if authenticate_checked:
        join_authenticate = get_position('join_authenticate.png')
        if join_authenticate:
            close_window()
            return 1
        join_authenticate_checked = get_position('join_authenticate_checked.png')
        if join_authenticate_checked:
            close_window()
            return 2

    close_window()
 def lib_action_status(self):
     lib_action = os.path.join(self.folder, 'lib_action')
     file_list=get_folder_items(lib_action)
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=lib_action,similaity=0.99)
         if res:
             return self.check_power_action_type(res[0], (200, 0))
 def cpu_mode_status(self):
     cpu_mode = os.path.join(self.folder, 'cpu_mode')
     file_list=get_folder_items(cpu_mode)
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=cpu_mode,similaity=0.99)
         if res and tag=="ondemand":
             print("ondemand")
             return "ondemand"
         elif res and tag=="performance":
             print('performance')
             return 'performance'
 def look(self,sub_folder):
     full_folder = os.path.join(self.folder, sub_folder)
     file_list=get_folder_items(full_folder)
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=full_folder,similaity=0.99)
         if res and tag=="checked":
             print("checked")
             return True
         elif res and tag=="unchecked":
             print('unchecked')
             return False
def domain_login(user_name='bamboo.pan', password="******"):
    domain_login_user = get_position('domain_login_user.png')
    if not domain_login_user:
        Logger().info("find domain_login_user fail")
        return
    domain_login_pwd = get_position('domain_login_pwd.png')
    if not domain_login_pwd:
        Logger().info("find domain_login_pwd fail")
        return
    pyautogui.click(domain_login_user[1])
    pyautogui.typewrite(user_name)
    pyautogui.click(domain_login_pwd[1])
    pyautogui.typewrite(password)
    domain_login_button = get_position('domain_login_button.png')
    if not domain_login_button:
        Logger().info("find domain_login_button fail")
        return
    pyautogui.click(domain_login_button[1])
    steps = {
        "add_domain": True,
        "login_domain": True
    }
    update_flag(steps)
    Logger().info("login success")
 def mode_status(self):
     mode = os.path.join(self.folder, 'mode')
     file_list=get_folder_items(mode)
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=mode,similaity=0.99)
         if res and tag=="normal":
             print("normal")
             return "normal"
         elif res and tag=="low":
             print('low')
             return 'low'
         elif res and tag=="critical":
             print('critical')
             return 'critical'
 def critical_action_status(self):
     critical_action = os.path.join(self.folder, 'critical_action')
     file_list=get_folder_items(critical_action)
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=critical_action,similaity=0.99)
         if res and tag=="nothing":
             print("nothing")
             return "nothing"
         elif res and tag=="shutdown":
             print('shutdown')
             return 'shutdown'
         elif res and tag=="sleep":
             print('sleep')
             return 'sleep'
 def check_power_action_type(self,parent_region,offset_w_h,):
     power_actions=os.path.join(self.folder, 'power_actions')
     file_list = get_folder_items(power_actions)
     new_region=(parent_region[0],parent_region[1],parent_region[2]+offset_w_h[0],parent_region[3]+offset_w_h[1])
     for file in file_list:
         tag = file.split(".")[0].split("_")[-1]
         res = get_position(file, base_dir=power_actions, similaity=0.99,region=new_region)
         if res and tag=="nothing":
             print("nothing")
             return "nothing"
         elif res and tag=="shutdown":
             print('shutdown')
             return 'shutdown'
         elif res and tag=="sleep":
             print('sleep')
             return 'sleep'
def leave_domain(status_code, user_name, password):
    if status_code == 0:
        return True
    SwitchThinProMode(switch_to='admin')
    open_window("active directory")
    ad = get_position('ad.png')
    if not ad:
        Logger().info("find ad fail")
        return
    authenticate_checked = get_position('authenticate_checked.png')
    if not authenticate_checked:
        Logger().info("find authenticate fail")
        return
    pyautogui.click(authenticate_checked[1])
    apply = get_position('apply.png')
    if not apply:
        Logger().info("find apply fail")
        return
    pyautogui.click(apply[1])
    if status_code == 1:
        reboot_dialog = get_position('reboot_dialog.png')
        if reboot_dialog:
            pyautogui.hotkey("enter")
            time.sleep(1)
            pyautogui.hotkey("enter")
            Logger().info("reboot to exit dialog")
            remove_flag()
            return
        apply_checked = get_position('apply_checked.png')
        if not apply_checked:
            Logger().info("find apply_checked fail")
            close_window()
            return
        else:
            Logger().info("apply_checked pass")
            close_window()
            return True

    elif status_code == 2:
        reboot_dialog = get_position('reboot_dialog.png')
        if reboot_dialog:
            pyautogui.hotkey("enter")
            Logger().info("reboot to exit dialog")

            dialog = get_position('dialog.png')
            if not dialog:
                Logger().info("find dialog fail")
                return
            user = get_position('user.png')
            if not user:
                Logger().info("find user fail")
                return

            pwd = get_position('pwd.png')
            if not user:
                Logger().info("find pwd fail")
                return
            pyautogui.click(user[1])
            pyautogui.typewrite(user_name)
            pyautogui.click(pwd[1])
            pyautogui.typewrite(password)
            ok = get_position('ok.png')
            if not user:
                Logger().info("find ok fail")
                close_window()
                return
            pyautogui.click(ok[1])

            while True:
                leave = get_position('leave.png')
                if leave:
                    Logger().info("leaving domain")
                else:
                    break
                time.sleep(1)
            pyautogui.hotkey("enter")
            return True
        else:
            dialog = get_position('dialog.png')
            if not dialog:
                Logger().info("find dialog fail")
                return
            user = get_position('user.png')
            if not user:
                Logger().info("find user fail")
                return

            pwd = get_position('pwd.png')
            if not user:
                Logger().info("find pwd fail")
                return
            pyautogui.click(user[1])
            pyautogui.typewrite(user_name)
            pyautogui.click(pwd[1])
            pyautogui.typewrite(password)
            ok = get_position('ok.png')
            if not user:
                Logger().info("find ok fail")
                close_window()
                return
            pyautogui.click(ok[1])

            while True:
                leave = get_position('leave.png')
                if leave:
                    Logger().info("leaving domain")
                else:
                    break
                time.sleep(1)
            pyautogui.hotkey("enter")

            apply_checked = get_position('apply_checked.png')
            if not apply_checked:
                Logger().info("find apply_checked fail")
                close_window()
                return
            else:
                Logger().info("apply_checked pass")
                remove_flag()
                close_window()
                return True
def add_domain(status_code, user_name, password, domain_name):
    if status_code == 0:
        Logger().info("setting nothing")
        return
    elif status_code != 1 and status_code != 2:
        Logger().info("invalid status code")
        return
    SwitchThinProMode(switch_to='admin')
    open_window("active directory")
    ad = get_position('ad.png')
    if not ad:
        Logger().info("find ad fail")
        return
    domain = get_position('domain_name.png')
    if not domain:
        Logger().info("find domain fail")
        return
    domain_txt_pos = (domain[1][0] + 200, domain[1][1])
    pyautogui.click(domain_txt_pos)
    for i in range(20):
        pyautogui.hotkey('backspace')
        time.sleep(0.5)
    pyautogui.typewrite(domain_name)
    authenticate = get_position('authenticate.png')
    if not authenticate:
        Logger().info("find authenticate fail")
        return
    pyautogui.click(authenticate[1])
    apply = get_position('apply.png')
    if not apply:
        Logger().info("find apply fail")
        return

    if status_code == 1:
        pyautogui.click(apply[1])

        while True:
            enter = get_position('enter_authenticate.png')
            if enter:
                Logger().info("entering domain")
            else:
                break
            time.sleep(1)

        apply_checked = get_position('apply_checked.png')
        if not apply_checked:
            Logger().info("find apply_checked fail")
            close_window()
            return
        else:
            Logger().info("apply_checked pass")
            close_window()
            return True

    elif status_code == 2:
        join_authenticate = get_position('join_authenticate.png')
        if not join_authenticate:
            Logger().info("find join_authenticate fail")
            return
        pyautogui.click(join_authenticate[1])
        pyautogui.click(apply[1])
        dialog = get_position('dialog.png')
        if not dialog:
            Logger().info("find dialog fail")
            return
        user = get_position('user.png')
        if not user:
            Logger().info("find user fail")
            return

        pwd = get_position('pwd.png')
        if not user:
            Logger().info("find pwd fail")
            return
        pyautogui.click(user[1])
        pyautogui.typewrite(user_name)
        pyautogui.click(pwd[1])
        pyautogui.typewrite(password)
        ok = get_position('ok.png')
        if not user:
            Logger().info("find ok fail")
            return
        pyautogui.click(ok[1])
        while True:
            enter = get_position('enter.png')
            if enter:
                Logger().info("entering domain")
            else:
                break
            time.sleep(1)

        apply_checked = get_position('apply_checked.png')
        if not apply_checked:
            Logger().info("find apply_checked fail")
            close_window()
            return
        else:
            Logger().info("apply_checked pass")
            close_window()
            return True