import logging
from typing import Optional

from pyttermost.api import APIClient
from pyttermost.cache import Cache
from slidge import BaseGateway, FormField, global_config
from slixmpp import JID


class Gateway(BaseGateway):
    REGISTRATION_INSTRUCTIONS = (
        "Enter mattermost credentials. Get your MMAUTH_TOKEN on the web interface,"
        " using the dev tools of your browser (it's a cookie)."
    )
    REGISTRATION_FIELDS = [
        FormField(var="url", label="Mattermost server URL", required=True),
        FormField(var="token", label="MMAUTH_TOKEN", required=True),
        FormField(var="basepath", label="Base path", value="/api/v4", required=True),
        FormField(
            var="basepath_ws",
            label="Websocket base path",
            value="/websocket",
            required=True,
        ),
        FormField(
            var="strict_ssl",
            label="Strict SSL verification",
            value="1",
            required=False,
            type="boolean",
        ),
    ]

    ROSTER_GROUP = "Mattermost"

    COMPONENT_NAME = "Mattermost (slidge)"
    COMPONENT_TYPE = "mattermost"

    COMPONENT_AVATAR = (
        "https://play-lh.googleusercontent.com/"
        "aX7JaAPkmnkeThK4kgb_HHlBnswXF0sPyNI8I8LNmEMMo1vDvMx32tCzgPMsyEXXzZRc"
    )

    GROUPS = True

    PREFERENCES = BaseGateway.PREFERENCES + [
        FormField(
            var="prefer_nickname",
            label="Display users' custom nicknames instead of their full names",
            value="1",
            required=False,
            type="boolean",
        )
    ]

    def __init__(self):
        super().__init__()
        self.cache = Cache(global_config.HOME_DIR / "mm_client_cache_v2.sqlite")
        if not logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.getLogger("httpx").setLevel(logging.WARNING)

    async def validate(
        self, user_jid: JID, registration_form: dict[str, Optional[str]]
    ):
        mm_client = APIClient(
            registration_form["url"].rstrip("/") or "",  # type:ignore[union-attr]
            self.cache,
            token=registration_form["token"],
        )
        try:
            await mm_client.login()
        except Exception as e:
            raise ValueError("Could not authenticate: %s - %s", e, e.args)
        if mm_client.me is None:
            raise ValueError("Could not authenticate")
