import pathlib

from .config import config
from .exception import config_exception

class app_config(config):
    def __defaults() -> dict:
        return {
            "database_uri": "sqlite:///database.db",
            "users_file": "users.json",
            "covers_dir": "covers/",
            "thumbnails_dimension": "400",
            "google_api_key": "",
            "google_cx": ""
        }

    def __init__(self):
        super().__init__(app_config.__defaults())

    @property
    def google_api_key(self) -> str | None:
        api_key = self._get("google_api_key")

        if len(api_key.strip()) == 0:
            return None
        
        return api_key

    @property
    def google_cx(self) -> str | None:
        cx = self._get("google_cx")

        if len(cx.strip()) == "0":
            return None

        return cx

    @property
    def database_uri(self) -> str:
        return self._get("database_uri")

    @property
    def users_file(self) -> str:
        return self._get("users_file")

    @property
    def covers_dir(self) -> str:
        return self._get("covers_dir")

    @property
    def users_path(self) -> pathlib.Path:
        return pathlib.Path(self.users_file)

    @property
    def thumbnails_dimension(self) -> int:
        try:
            return int(self._get("thumbnails_dimension"))
        
        except:
            raise config_exception("Thumbnails dimension must be integer.")

    @property
    def covers_path(self) -> pathlib.Path:
        return pathlib.Path(self.covers_dir)

