from typing import (
    Any,
    Callable,
)


class CacheManager(object):
    def __init__(
        self,
        sector_config,  # type: dict
    ):
        # type: (...) -> None

        self.sector_config = sector_config
        self.cached_objects = {}
        self.cache_key = "POSID={}|DEVICETYPE={}"

    def get_cached_object(
        self,
        sector,                             # type: str
        device_type,                        # type: str
        pos_id,                             # type: str
        method_to_obtain_cache,             # type: Callable[[Any], Any]
        method_to_obtain_cache_params,      # type: tuple
    ):
        # type: (...) -> str

        sector_key = sector.upper() if sector else sector
        device_type_key = device_type.upper()
        if sector_key in self.sector_config and device_type_key in self.sector_config[sector_key]:
            return self.sector_config[sector_key][device_type_key]
        cache_key = self.cache_key.format(pos_id, device_type_key)
        if cache_key not in self.cached_objects:
            object_to_cache = method_to_obtain_cache(*method_to_obtain_cache_params)
            self.cached_objects[cache_key] = object_to_cache

        return self.cached_objects[cache_key]
