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

from ascript.android.screen import FindColors

# 导入动作模块
from ascript.android import action


def shanghuichushou(shcswp):
    print("开始商会出售")
    res_last = zhaobtn(["包裹", "包裹01"])
    if res_last is not None:
        common.findandclick(res_last)
        print("点击包裹")
        sleep(1)
        num = 0
        while True:
            res_kcswp = zhaobtn(shcswp)
            if res_kcswp is not None:
                common.findandclick(res_kcswp)
                sleep(1)
                res_gd = common.zhaotu("更多.png")
                if res_gd is not None:
                    common.findandclick(res_gd)
                    sleep(1)

                res_shcs = zhaobtn(["商会出售", "商会出售01"])
                if res_shcs is not None:
                    common.findandclick(res_shcs)
                    sleep(1)
                    res_man = common.zhaotu("满.png")
                    if res_man is not None:
                        common.findandclick(res_man)
                        sleep(1)
                    res_chushou = common.zhaotu("出售.png")
                    if res_chushou is not None:
                        common.findandclick(res_chushou)
                        sleep(4)
                        num += 1
                if num >= 5:
                    res_bbzl = common.zhaotu("背包-整理.png")
                    if res_bbzl is not None:
                        common.findandclick(res_bbzl)
                        sleep(5)
                        num = 0
            else:
                print("没有找到商会出售的道具，进行下一项")
                common.closeAllbtn()
                return
            sleep(1)
    else:
        print("没有找到包裹，进行下一项")


def xiahua():
    print("开始下滑")
    action.slide(749, 555, 749, 241, 2000)
    sleep(1)


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

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