import time
import random
import threading

from . import common
from . import lingqushuangbei
from . import duiwu

# 导入系统资源模块

from ascript.android.system import R

# 导入动作模块

from ascript.android import action

# 导入图色检索模块

from ascript.android.screen import FindImages

from ascript.android.screen import FindColors

# 文字识别

from ascript.android.screen import Ocr


def check_shuangbei():
    lingqushuangbei.lingqushuangbei()  # 执行双倍点数检测
    print(f"----------------------------5分钟检测一次双倍点数：", lingqushuangbei.shuangbeidianshu)
    timeer = threading.Timer(300, check_shuangbei)
    timeer.start()
    if common.zhuoguiisover is True:
        print("捉鬼完成，检测双倍点数线程关闭")
        timeer.cancel()
        return


def zhuogui():
    check_shuangbei()
    if common.shuangbei is False or common.zhuoguiisover is True:
        print("双倍领取失败，双倍不足，捉鬼完成")
        duiwu.tuichuzudui()
        return

    if common.shuangbei:
        print("开始捉鬼")
        # 先执行退出队伍
        while True:
            if common.zhuoguiisover is True and common.shuangbei is False:
                print("-------------------------检查到捉鬼完成,退出队伍，进行下一项")
                duiwu.tuichuzudui()
                return

            # zhaotu("战斗-取消.png")
            res_zdsb = common.zhaotu("战斗失败.png")
            if res_zdsb is not None:
                print("找到战斗失败")
                common.findandclick(res_zdsb)
                sleep(1)
                duiwu.tuichuzudui()
                continue

            print("判断状态1")
            if common.isfire():
                print("----------战斗中----------")
                sleep(60)
                continue
            sleep(10)
            print("判断状态2")
            if common.isfire():
                print("----------再次判断战斗中----------")
                sleep(60)
                continue

            res_huodong = common.huodong()
            if res_huodong:
                sleep(1)
                res_gui = common.zhaorenwu("鬼图标2.png")
                if res_gui is None:
                    print("未找到目标图片：鬼图标.png")
                    # 翻页
                    common.closeAllbtn()
                    continue
                else:
                    sleep(1)
                    res_canjia = common.zhaocanjia_text(res_gui)
                    res_wancheng = common.zhaowancheng(res_gui)
                    if res_wancheng is not None:
                        print("捉鬼任务完成，退出队伍")
                        common.closeAllbtn()
                        sleep(2)
                        duiwu.tuichuzudui()
                        common.zhuoguiisover = True
                        return

                    if res_canjia is None:
                        print("未找到目标图片：参加.png")
                        if common.isfire():
                            print("正在战斗，等待战斗完成")
                            sleep(60)
                            continue
                        else:
                            print("异常情况")
                            common.closeAllbtn()
                            continue
                    else:
                        sleep(1)
                        print("点击参加")
                        common.findandclick(res_canjia)
                        sleep(1)
                        res_zdpp = common.zhaotu("自动匹配.png")
                        res_qxpp = common.zhaotu("取消匹配.png")
                        if res_qxpp is not None:
                            print("正在匹配，等待匹配完成")
                            sleep(20)
                            common.closeAllbtn()
                            continue

                        if res_zdpp is None:
                            print("未找到目标图片：自动匹配.png")
                            print("判断状态3")
                            sleep(10)
                            if common.isfire():
                                print("1战斗中-等待战斗结束")
                                sleep(60)
                                continue
                            else:
                                print("判断状态4")
                                sleep(10)
                                if common.isfire():
                                    print("2战斗中-等待战斗结束")
                                    sleep(60)
                                    continue
                                else:
                                    print("点了参加，也没自动匹配，也没在战斗中-退出队伍")
                                    duiwu.tuichuzudui()
                                    print("退出组队后，重新捉鬼")

                        else:
                            common.findandclick(res_zdpp)
                            print("---------捉鬼开始了---------")
                            common.closeAllbtn()
                            sleep(2)
                            common.closeAllbtn()
                            sleep(60)

            sleep(1)


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