import lzma
import pickle
from functools import cached_property

from mower import __rootdir__
from mower.utils import typealias as tp
from mower.utils.image import loadimg
from mower.utils.path import get_path
from mower.utils.singleton import singleton

with lzma.open(f"{__rootdir__}/models/avatar.pkl", "rb") as f:
    avatar = pickle.load(f)

with lzma.open(f"{__rootdir__}/models/portrait.pkl", "rb") as f:
    portrait = pickle.load(f)

with lzma.open(f"{__rootdir__}/models/navigation.pkl", "rb") as f:
    navigation = pickle.load(f)

with lzma.open(f"{__rootdir__}/models/shop.pkl", "rb") as f:
    shop = pickle.load(f)


@singleton
class Digtal:
    @cached_property
    def secret_front(self) -> dict[str, tp.GrayImage]:
        data = {}
        for i in get_path("@install/mower/resources/digtal/secret_front").iterdir():
            try:
                data[int(i.stem)] = loadimg.__wrapped__(i, True)
            except ValueError:
                data[i.stem] = loadimg.__wrapped__(i, True)
        return data

    @cached_property
    def riic_base_digits(self) -> dict[str, tp.GrayImage]:
        data = {}
        for i in get_path("@install/mower/resources/digtal/riic_base_digits").iterdir():
            data[int(i.stem)] = loadimg.__wrapped__(i, True)
        return data

    @cached_property
    def noto_sans(self) -> dict[str, tp.GrayImage]:
        data = {}
        for i in get_path("@install/mower/resources/digtal/noto_sans").iterdir():
            data[int(i.stem)] = loadimg.__wrapped__(i, True)
        return data
