import time
from . import common
# 导入系统资源模块
from ascript.android.system import R
# 导入图色检索模块
from ascript.android.screen import FindImages

from ascript.android.screen import FindColors

pngs = ["秘境任务", "进入战斗"]


def mijingxiangyao():
    while True:

        if common.isfire():
            print("----------战斗中----------")
            sleep(60)
            continue
        res_likai = common.zhaotu("离开.png")
        if res_likai is not None:
            print("找到离开，就在秘境里")
            sleep(1)
            renwuneirong()
        else:
            res_huodong = common.huodong()
            if res_huodong:
                print("打开活动")
                sleep(1)
                res_mjxy = common.zhaorenwu("秘境降妖.png")
                if res_mjxy is not None:
                    res_wancheng = common.zhaowancheng_text(res_mjxy)
                    if res_wancheng is not None:
                        print("秘境降妖任务完成,进行下一项")
                        common.closeAllbtn()
                        sleep(2)
                        return

                    res_canjia = common.zhaocanjia_text(res_mjxy)
                    if res_canjia is not None:
                        common.findandclick(res_canjia)
                        print("点击参加，秘境降妖")
                        sleep(2)

                        res_mjrw = zhaobtn(pngs)
                        if res_mjrw is not None:
                            common.findandclick(res_mjrw)
                            sleep(3)

                            res_jxtz = common.zhaotu("继续挑战.png")
                            if res_jxtz is not None:
                                common.findandclick(res_jxtz)
                                sleep(3)

                                renwuneirong()

        return
        sleep(1)


def renwuneirong():
    while True:
        if common.isfire():
            print("----------战斗中----------")
            sleep(60)
            continue
        res_zdsb = common.zhaotu("战斗失败.png")
        if res_zdsb is not None:
            print("找到战斗失败")
            common.findandclick(res_zdsb)
            sleep(1)
            res_lk = common.zhaotu("离开.png")
            if res_lk is not None:
                print("找到离开")
                common.findandclick(res_lk)
                sleep(1)
                print("战斗失败-离开秘境，进行下一项")
                return

        res_tyll=common.zhaotu("汤圆来了.png")
        if res_tyll is not None:
            common.findandclick({"rect":[815,266,844,305]})
            sleep(3)

        res_btns = zhaobtn(pngs)
        if res_btns is not None:
            common.findandclick(res_btns)
            sleep(3)

        dianjirenwu()
        sleep(3)


def dianjirenwu():
    res_shimen = common.zhaotu("秘境降妖-关卡.png")
    if res_shimen is None:
        print("没找到-任务列表-秘境降妖-关卡")
        return
    else:
        common.findandclick({"rect": [793, 265, 1016, 341]})


def zhaobtn(pngs):
    # 找各种任务按钮
    images = [R.img(png + ".png") for png in pngs]
    return FindImages.find(images, rect=[172, 96, 1005, 714], confidence=0.95)


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