# ========================================================================== #
#                                                                            #
#    KVMD - The main PiKVM daemon.                                           #
#                                                                            #
#    Copyright (C) 2020  Maxim Devaev <mdevaev@gmail.com>                    #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
# ========================================================================== #


import aiohttp

from typing import Final

from ... import htclient

from ...yamlconf import Section
from ...yamlconf import Option

from ...validators.basic import valid_bool
from ...validators.basic import valid_number
from ...validators.net import valid_url

from ..errors import NbdRemoteError
from ..types import NbdImage

from . import BaseNbdRemote


# =====
class NbdHttpRemote(BaseNbdRemote):
    def __init__(self, c: Section) -> None:
        super().__init__(c)

        self.__url:           Final[str]   = c.url
        self.__verify:        Final[bool]  = c.verify
        self.__user:          Final[str]   = c.user
        self.__passwd:        Final[str]   = c.passwd
        self.__timeout:       Final[float] = c.timeout

        self.__session: (aiohttp.ClientSession | None) = None

    # =====

    @classmethod
    def get_schemes(cls) -> set[str]:
        return set(["http", "https"])

    @classmethod
    def get_options(cls) -> dict[str, Option]:
        return {
            "url":     Option("", type=valid_url.mk(protos=cls.get_schemes())),
            "verify":  Option(True, type=valid_bool),
            "user":    Option(""),
            "passwd":  Option(""),
            "timeout": Option(3.0, type=valid_number.mk(min=1.0, max=30.0, type=float)),
            **BaseNbdRemote.get_options(),
        }

    # =====

    def get_timeout(self) -> float:
        return self.__timeout

    async def _do_probe(self) -> NbdImage:
        async with self.__make_session() as session:
            return (await self.__probe(session))

    async def _do_ensure(self) -> NbdImage:
        if self.__session is None:
            self.__session = self.__make_session()
        return (await self.__probe(self.__session))

    async def _do_close(self) -> None:
        if self.__session is not None:
            try:
                await self.__session.close()
            finally:
                self.__session = None

    def __make_session(self) -> aiohttp.ClientSession:
        return aiohttp.ClientSession(
            headers={aiohttp.hdrs.USER_AGENT: htclient.make_user_agent("KVMD-NBD")},
            connector=aiohttp.TCPConnector(ssl=self.__verify),
            auth=(aiohttp.BasicAuth(self.__user, self.__passwd) if self.__user else None),
            timeout=aiohttp.ClientTimeout(total=self.__timeout),

            # Don't ask for compression: https://github.com/aio-libs/aiohttp/issues/5513
            skip_auto_headers=frozenset([aiohttp.hdrs.ACCEPT_ENCODING]),
        )

    async def __probe(self, session: aiohttp.ClientSession) -> NbdImage:
        async with session.head(self.__url) as resp:
            htclient.raise_not_200(resp)

            proto = resp.request_info.url.scheme.upper()
            name = htclient.get_filename(resp)

            cl = resp.content_length
            if not isinstance(cl, int) or cl < 0:
                raise NbdRemoteError(f"Invalid Content-Length: {cl}")

            try:
                mod_ts = htclient.get_mtime(resp)
            except Exception:
                mod_ts = 0

            return NbdImage(
                url=self.__url,
                proto=proto,
                name=name,
                size=cl,
                mod_ts=mod_ts,
                rw=False,
            )

    async def _on_read(self, offset: int, size: int) -> bytes:
        assert offset >= 0
        assert size > 0
        assert self.__session is not None

        async with self.__session.get(
            url=self.__url,
            # HTTP Range включает края: bytes=0-499 запрашивает 500 байт.
            #  - https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Range
            headers={aiohttp.hdrs.RANGE: f"bytes={offset}-{offset + size - 1}"},
        ) as resp:

            resp.raise_for_status()  # 206 partial is OK here
            return (await resp.read())[:size]

    async def _on_write(self, offset: int, data: bytes) -> None:
        _ = offset
        _ = data
        raise RuntimeError("WRITE should not be called for HTTP")
