from __future__ import annotations

from clarabot_nano_common.server.base.config import ServerConfig


class DocumentServerConfig(ServerConfig):
    WOPI_HOST_TRANSLATE_CONST = "https://wopi.clarabot.com/"

    def __init__(self, config):
        ServerConfig.__init__(self, config)
        # NOTE: these are for convenience as they cannot change runtime (local).
        self.cool_wopi_port = config["local"]["cool_wopi_port"]
        self.cool_server_port = config["local"]["cool_server_port"]
        # more convenience
        self.wopi_host = f"http://127.0.0.1:{self.cool_wopi_port}/"

    def translate_to_wopi_host(self, url: str) -> str:
        if not url.startswith(self.WOPI_HOST_TRANSLATE_CONST):
            raise Exception("URL does not start with WOPI constant for translation")
        return url.replace(self.WOPI_HOST_TRANSLATE_CONST, self.wopi_host, 1)

    def translate_from_wopi_host(self, url: str) -> str:
        if not url.startswith(self.wopi_host):
            raise Exception("URL does not start with WOPI api host for translation")
        return url.replace(self.wopi_host, self.WOPI_HOST_TRANSLATE_CONST, 1)

    def _matches(self, other: DocumentServerConfig) -> bool:
        return ServerConfig._matches(self, other)
