# __init__.py 为初始化加载文件

from . import common

from . import zhuogui
from . import lingqushuangbei
from . import duiwu
from . import shimen
from . import baotu
from . import mijing
from . import yabiao
from . import zhixian
import time

import random
import json

from ascript.android.ui import WebWindow
# 导入系统资源模块

from ascript.android.system import R

# 导入动作模块

from ascript.android import action

# 导入节点检索模块

from ascript.android import node

# 导入图色检索模块

from ascript.android import screen

from ascript.android.node import Selector

from ascript.android.screen import FindColors

from ascript.android.screen import FindImages

from ascript.android.screen import Ocr
import re

from ascript.android import system

shcswp = ["百炼精铁", "制造书-女衣", "制造书-宝珠", "制造书-枪", "制造书-鞋"]

print("程序开启，打开游戏")


# # 根据包名启动,推荐使用
# # com.netease.my
# res_qidong = system.open("com.netease.my")
# print(res_qidong)
# if res_qidong is None:
#         # time.sleep(10)
#         # action.click(0, 0)  # 在随机点点击
#     common.denglu()

# common.alllive_task()

# zhuogui.zhuogui()

# shimen.shimenrenwu()

# baotu.baoturenwu()

# baotu.wabaotu()

# mijing.mijingxiangyao()

# yabiao.yabiaorenwu()

# zhixian.shanghuichushou(shcswp)

renwuliebiao = []
shanghuichushou_lb = []
shiyongwupin_lb = []
diuqiwupin_lb = []
baitanchushou_lb = []

def tunnel(k, v):
    try:
        # 尝试将 v 解析为字典
        v_dict = json.loads(v)
        print(f"解析后的字典---", v_dict, type(v_dict))

        # 遍历字典中的每个键和值
        for key, value_list in v_dict.items():
            print(f"任务-{key} 的值：", value_list)
            if key == "任务列表":
                renwuliebiao.extend(remove_suffix(value_list))
            if key == "商会出售":
                shanghuichushou_lb.extend(remove_suffix(value_list))
            if key == "使用物品":
                shiyongwupin_lb.extend(remove_suffix(value_list))
            if key == "丢弃物品":
                diuqiwupin_lb.extend(remove_suffix(value_list))
            if key == "出售物品":
                baitanchushou_lb.extend(remove_suffix(value_list))

        # 执行所有任务
        execute_all_tasks()

    except json.JSONDecodeError as e:
        print(f"解析 JSON 失败: {e}")

def remove_suffix(items):
    return [item.split('-')[0] for item in items]

def execute_all_tasks():
    # 按顺序执行任务
    if "捉鬼任务" in renwuliebiao:
        print("执行任务: 捉鬼任务")
        common.start_alllive_task_timer(70)
        sleep(3)
        zhuogui.zhuogui()

    print("捉鬼完成，调整全局检测线程时间----120")
    common.start_alllive_task_timer(120)
    if "师门任务" in renwuliebiao:
        print("执行任务: 师门任务")
        shimen.shimenrenwu()

    if "自动挖宝" in renwuliebiao:
        print("执行任务: 自动挖宝")
        baotu.baoturenwu()
        baotu.wabaotu()

    if "秘境降妖" in renwuliebiao:
        print("执行任务: 秘境降妖")
        mijing.mijingxiangyao()

    if "普通押镖" in renwuliebiao:
        print("执行任务: 普通押镖")
        yabiao.yabiaorenwu()

    # 其他任务按顺序执行
    print("执行商会出售任务")
    zhixian.shanghuichushou(shanghuichushou_lb)

    print("执行使用物品任务")
    # 这里可以添加使用物品的具体逻辑
    # 例如：shiyongwupin(shiyongwupin_lb)

    print("执行丢弃物品任务")
    # 这里可以添加丢弃物品的具体逻辑
    # 例如：diuqiwupin(diuqiwupin_lb)

    print("执行出售物品任务")
    # 这里可以添加出售物品的具体逻辑
    # 例如：baitanchushou(baitanchushou_lb)

# 构建一个WebWindow 显示‘/res/ui/a.html’ 通信通道为tunnel 函数
w = WebWindow(R.ui('index.html'), tunnel)
w.show()

print("程序结束")
def sleep(num):
    time.sleep(num)
