from . import common

# 导入系统资源模块

from ascript.android.system import R

# 导入动作模块

from ascript.android import action

# 导入图色检索模块

from ascript.android.screen import FindImages

# 文字识别

from ascript.android.screen import Ocr
import time


def tuichuzudui():
    num = 0

    while True:
        if num > 10:
            print("退出队伍失败")
            return

        res_duiwu = common.zhaotu("队伍.png")
        res_duiwu1 = common.zhaotu("队伍1.png")
        res_duiwu2 = common.zhaotu("队伍2.png")
        res_last = res_duiwu or res_duiwu1 or res_duiwu2
        if res_last is not None:
            common.findandclick(res_last)
            sleep(1)
            rec_duiwu_tc = common.zhaotu("队伍-退出队伍.png")
            if rec_duiwu_tc is None:
                print("未找到目标图片：队伍-退出队伍.png")
                res_cjdw = common.zhaotu("创建队伍.png")
                if res_cjdw is None:
                    print("异常情况")
                    num += 1
                    common.closeAllbtn()
                else:
                    print("-------找到创建队伍，没有在队伍中------")
                    
                    return
            else:
                sleep(1)
                common.findandclick(rec_duiwu_tc)
                common.closeAllbtn()
                print("退出队伍完成")
                if common.isfire():
                    sleep(60)

                return

        else:
            print("没找到队伍按钮")
            if common.isfire():
                print("战斗中，打开所有按钮")
                res_suoyoutubiao = common.zhaotu("所有图标.png")
                if res_suoyoutubiao is None:
                    print("退出组队-异常情况")
                    num += 1
                    common.closeAllbtn()
                else:
                    # 找到所有活动图标，点击
                    common.findandclick(res_suoyoutubiao)
                    print("展开所有图标")
                    continue
            else:
                common.closeAllbtn()

        sleep(0.5)


def shifouzaiduiwu():
    print("开始检测是否在队伍中")
    num = 0
    while True:
        if num > 10:
            return False

        res_duiwu = common.zhaotu("队伍.png")
        res_duiwu1 = common.zhaotu("队伍1.png")
        res_duiwu2 = common.zhaotu("队伍2.png")
        res_last = res_duiwu or res_duiwu1 or res_duiwu2

        if res_last is not None:
            common.findandclick(res_last)
            sleep(1)
            common.findandclick(res_last)

            rec_duiwu_tc = common.zhaotu("队伍-退出队伍.png")
            if rec_duiwu_tc is None:
                print("未找到目标图片：队伍-退出队伍.png")
                res_cjdw = common.zhaotu("创建队伍.png")
                if res_cjdw is not None:
                    print("-------等待创建队伍，没有在队伍中，退出队伍完成------")
                    common.closeAllbtn()
                    return False
                else:
                    print("!!!异常情况!!!")
                    print("-------检测是否在队伍中出现异常------")
                    common.closeAllbtn()
                    num += 1
                    continue

            else:
                # common.findandclick(rec_duiwu_tc)
                # common.closeAllbtn()
                print("退出队伍-已找到，正在队伍中")
                return True

        else:
            print("没找到队伍按钮")
            if common.isfire():
                print("战斗中，打开所有按钮")
                res_suoyoutubiao = common.zhaotu("所有图标.png")
                if res_suoyoutubiao is None:
                    print("未找到目标图片：所有图标.png")
                    # 异常情况
                    num += 1
                    common.closeAllbtn()
                else:
                    # 找到所有活动图标，点击
                    common.findandclick(res_suoyoutubiao)
            else:
                common.closeAllbtn()

        sleep(1)

    if common.zhuoguiisover is True:
        print("捉鬼完成，不需要检测是否在队伍中--定时任务结束")
    else:
        threading.Timer(180, shifouzaiduiwu).start()


def renwumingc():
    num = 0
    while True:
        if num > 10:
            print("任务名称识别失败")
            return None

        res_duiwu = common.zhaotu("队伍.png")
        res_duiwu1 = common.zhaotu("队伍1.png")
        res_duiwu2 = common.zhaotu("队伍2.png")
        res_last = res_duiwu or res_duiwu1 or res_duiwu2
        if res_last is not None:
            common.findandclick(res_last)
            sleep(0.5)
            res_rwmc = common.shizi([378, 144, 502, 188])
            common.closeAllbtn()
            return res_rwmc
        else:
            print("没找到队伍按钮")
            if common.isfire():
                print("战斗中，打开所有按钮")
                res_suoyoutubiao = common.zhaotu("所有图标.png")
                if res_suoyoutubiao is None:
                    res_back = common.zhaotu("返回.png")
                    # 异常情况
                    if res_back is not None:
                        # 找到所有返回
                        # common.findandclick(res_back)
                        continue
                    else:
                        num += 1
                        common.closeAllbtn()
                else:
                    # 找到所有活动图标，点击
                    common.findandclick(res_suoyoutubiao)

    sleep(0.5)

# 人物闲置时间
xianzhicishu = 0
# 人物是否闲置
renwuxianzhi = False
def shifoukaping():
    global xianzhicishu, renwuxianzhi  # 声明全局变量

    print(f"人物闲置时间(s):", xianzhicishu)
    res_iszd = common.isfire_wfstatus  # 假设 common.isfire() 是外部方法
    if res_iszd is True:
        # 战斗中
        xianzhicishu = 0
    else:
        xianzhicishu += 70

    print(f"----------------------------人物闲置k:", xianzhicishu)
    if xianzhicishu > 200:
        print("-----------------------------卡屏或者长时间没有操作---------------------------------")
        renwuxianzhi = True
    else:
        renwuxianzhi = False

    return renwuxianzhi

def sleep(num):
    time.sleep(num)
