from . import common
import time
import random
import threading
import re

shuangbeidianshu_fw = [663, 618, 809, 652]
shuangbeidianshu_ylq = [432, 616, 549, 650]

shuangbeidianshu = 0


def lingqushuangbei():
    print("开始领取双倍")
    a = 0
    while True:
        if a > 10:
            print("找不到挂机入口，程序停止")
            return
        res_guaji1 = common.zhaotu("挂机1.png")
        res_guaji = common.zhaotu("挂机.png")
        res_guaji_last = res_guaji or res_guaji1
        if res_guaji_last is not None:
            lingqu(res_guaji_last)
            print("双倍领取完成，开始捉鬼")
            return
        else:
            if common.isfire():
                # 战斗中，打开展示所有图标
                res_suoyoutubiao = common.zhaotu("所有图标.png")
                if res_suoyoutubiao is None:
                    print("未找到目标图片：所有图标.png")
                    a += 1
                    common.closeAllbtn()
                    continue
                else:
                    common.findandclick(res_suoyoutubiao)
                    sleep(1)
                    continue


def lingqu(zuobiao):
    global shuangbeidianshu
    sleep(1)
    common.findandclick(zuobiao)
    # 找是不是有双倍点数
    sleep(1)
    xunhuancishu = 0
    print("领取双倍")
    res_lingqu = common.zhaotu("领取.png")
    res_dlq_text = common.shizi(shuangbeidianshu_fw)
    res_ylq_text = common.shizi(shuangbeidianshu_ylq)
    ylq_num = 0
    if res_ylq_text is not None:
        res_ylq = qushuzi(res_ylq_text)
        ylq_num = int(res_ylq)

    dlq_num = 0
    if res_dlq_text is not None:
        res_dlq = qushuzi(res_dlq_text)
        dlq_num = int(res_dlq)
        print(f"待领取双倍点数：", dlq_num)
        print(f"已领取双倍点数：", ylq_num)

        # 计算剩余可领取点数
        remaining_points = min(dlq_num, 1000 - ylq_num)
        if remaining_points > 0:
            # 计算点击次数
            xunhuancishu = remaining_points // 100
            if remaining_points % 100 != 0:
                xunhuancishu += 1

            common.shuangbei = True
            for i in range(xunhuancishu):
                print(f"领取双倍 100 点, 次数 {i + 1}/{xunhuancishu}")
                common.findandclick(res_lingqu)
                sleep(0.5)

    if res_ylq_text is None:
        print("--------------------------双倍点数识别失败-停止抓鬼")
        common.shuangbei = False
        common.zhuoguiisover = True
        common.closeAllbtn()
        return
    else:
        shuangbeidianshu = ylq_num
        if ylq_num >= 4:
            print(f"正常领取结束，开始任务", ylq_num)
            common.shuangbei = True
            common.closeAllbtn()
            sleep(1)
            return
        else:
            print(f"双倍已消耗完/点数不足-停止抓鬼---", ylq_num)
            common.shuangbei = False
            common.zhuoguiisover = True
            common.closeAllbtn()
            sleep(1)
            return


def qushuzi(text):
    # 使用正则表达式提取数字
    match = re.search(r"\d+", text)  # 匹配连续的数字
    if match:
        num_str = match.group()  # 提取匹配的数字部分
        num = int(num_str)
        print(f"提取的数字：", num)
        return num
    else:
        print("未找到有效数字")
        return -1


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