from abc import ABCMeta, abstractmethod
from functools import lru_cache

import ujson
from aiocache import Cache, cached  # type: ignore


class ARecognizerRepository(metaclass=ABCMeta):
    @abstractmethod
    async def images_dogs(self) -> dict:
        pass

    @abstractmethod
    async def images_cats(self) -> dict:
        pass

    @abstractmethod
    def labels_dogs(self) -> dict:
        pass

    @abstractmethod
    def labels_cats(self) -> dict:
        pass


class RecognizerRepository(ARecognizerRepository):
    def __init__(self):
        pass

    @cached(ttl=60, cache=Cache.MEMORY)
    async def images_dogs(self) -> dict:
        with open("server/modules/recognizer/repository/meta/images.json") as f:  # noqa: ASYNC230
            return ujson.loads(f.read())["dog"]

    @cached(ttl=60, cache=Cache.MEMORY)
    async def images_cats(self) -> dict:
        with open("server/modules/recognizer/repository/meta/images.json") as f:  # noqa: ASYNC230
            return ujson.loads(f.read())["cat"]

    @lru_cache
    def labels_cats(self) -> dict:
        with open("server/modules/recognizer/repository/meta/labels_cats.json") as f:  # noqa: ASYNC230
            data_labels = f.read()
            return ujson.loads(data_labels)

    @lru_cache
    def labels_dogs(self) -> dict:
        with open("server/modules/recognizer/repository/meta/labels_dogs.json") as f:  # noqa: ASYNC230
            data_labels = f.read()
            return ujson.loads(data_labels)
