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

from ascript.android.screen import FindColors

pngs = ["/shimen/师门-任务按钮", "使用按钮", "/shimen/师门任务1", "普陀山", "购买按钮", "上交按钮", "/shimen/师门01", "摆摊-购买"]


def shimenrenwu():
    while True:

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

        res_huodong = common.huodong()
        print("打开活动")
        if res_huodong:
            sleep(1)
            res_shimen = common.zhaorenwu("/shimen/师门任务.png")
            if res_shimen is not None:
                print("找到师门任务")
                res_wancheng = common.zhaowancheng_text(res_shimen)
                if res_wancheng is not None:
                    print("------------------------师门任务已完成")
                    common.closeAllbtn()
                    sleep(2)
                    return

                res_canjia = common.zhaocanjia_text(res_shimen)
                if res_canjia is not None:
                    common.findandclick(res_canjia)
                    print("点击参加，师门任务")
                    sleep(1)
                    res_qwc = zhaobtn(["/shimen/师门-去完成", "/shimen/师门-继续任务"])
                    if res_qwc is not None:
                        sleep(1)
                        common.findandclick(res_qwc)
                        while True:
                            print("判断状态1")
                            if common.isfire():
                                print("----------战斗中----------")
                                sleep(60)
                                continue

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

                            res_qctb = zhaobtn(["请出题吧", "师傅请"])
                            if res_qctb is not None:
                                common.findandclick(res_qctb)
                                sleep(1)
                                while True:
                                    print("选择最后一个选项")
                                    common.findandclick({"rect": [730, 517, 978, 559]})
                                    sleep(1)
                                    res_zsjg = FindColors.find(
                                        "810,526,#F4DBBE|782,551,#F2D19E|924,526,#F4DBBE|930,554,#F2CF96",
                                        rect=[687, 510, 1016, 616])
                                    if res_zsjg is None:
                                        break

                                    sleep(1)

                            res_rwwc = common.zhaotu("/shimen/师门任务完成.png")
                            if res_rwwc is not None:
                                sleep(1)
                                print("师门任务完成")
                                common.findandclick({"rect": [418, 560, 612, 609]})
                                common.closeAllbtn()
                                return

                            # 点击任务
                            dianjirenwu()
                            sleep(4)

        else:
            common.closeAllbtn()

        sleep(1)


def dianjirenwu():
    res_shimen = common.zhaotu("/shimen/师门.png")
    if res_shimen is None:
        print("没找到-任务列表-师门任务")
        return
    else:
        common.findandclick({"rect": [806, 245, 1018, 323]})


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)
