import lzma
import pickle
from functools import cache, wraps

from mower.utils import typealias as tp
from mower.utils.image import load_static_dir, loadimg
from mower.utils.path import get_path

data_func = []


def load_data(func):
    @cache
    @wraps(func)
    def wrapper():
        return func()

    data_func.append(wrapper)
    return wrapper


def clear_data():
    for func in data_func:
        func.cache_clear()


@load_data
def avatar():
    with lzma.open(get_path("@install/mower/static/avatar.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def portrait():
    with lzma.open(get_path("@install/mower/static/portrait.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def shop():
    with lzma.open(get_path("@install/mower/static/shop.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def secret_front() -> dict[str, tp.GrayImage]:
    return load_static_dir("digits/secret_front")


@load_data
def riic_base() -> dict[str, tp.GrayImage]:
    return load_static_dir("digits/riic_base_digits")


@load_data
def noto_sans() -> dict[str, tp.GrayImage]:
    return load_static_dir("digits/noto_sans")


@load_data
def consume():
    with lzma.open(get_path("@install/mower/static/CONSUME.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def material():
    with lzma.open(get_path("@install/mower/static/MATERIAL.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def normal():
    with lzma.open(get_path("@install/mower/static/NORMAL.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def recruit():
    with lzma.open(get_path("@install/mower/static/recruit.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def recruit_result():
    with lzma.open(get_path("@install/mower/static/recruit_result.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def levels():
    with lzma.open(get_path("@install/mower/static/levels.pkl"), "rb") as f:
        return pickle.load(f)


@load_data
def operator_room():
    with lzma.open(get_path("@install/mower/static/operator_room.model"), "rb") as f:
        return pickle.load(f)


@load_data
def drop_data():
    result = {}
    for i in get_path("@install/ui/public/depot").iterdir():
        if "信物" in i.stem:
            continue
        supplement = "罗德岛物资补给"
        if i.stem.startswith(supplement) and i.stem != supplement:
            continue
        result[i.stem] = loadimg(i, True)
    return result
