import logging
from io import StringIO
from typing import Tuple, Optional

from clarabot_nano_common.common.error import log_escape_str
from clarabot_nano_common.common.handler.base import NanoHandlerError
from clarabot_nano_common.common.protocol.errors import NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST

from clarabot_nano.common.request.server_document import DocumentWsWriteRequest
from clarabot_nano.server.document.config import DocumentServerConfig
from clarabot_nano.server.document.handler.base import (
    AbstractDocumentSessionNanoServerHandler,
    DocumentServerHandleHelper,
)


class DocumentWsWriteHandler(AbstractDocumentSessionNanoServerHandler):
    REJECTS_ANONYMOUS_IDENTITY = False  # Further authorization is by room membership and group flags.
    EXECUTION_STATE_CAN_CONFLICT = False  # cacheable and request_key are constant, no conflict is possible
    HANDLER_KEY = DocumentWsWriteRequest.TYPE_KEY
    SUCCESS_LOG_LEVEL = logging.DEBUG  # do not log this normally

    def __init__(
        self, helper: DocumentServerHandleHelper, extracted_config: DocumentServerConfig, context: dict, payload: dict
    ):
        AbstractDocumentSessionNanoServerHandler.__init__(self, helper, extracted_config, context, payload)
        self.messages = self.payload.get(DocumentWsWriteRequest.MESSAGES)
        if not isinstance(self.messages, list):
            raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Invalid messages", self)

    async def _create_request_key(self, secure: bool = False) -> Tuple[bool, Optional[list], int]:
        return self._secure_request_key(False, None, 1) if secure else False, None, 1

    async def _execute_initial(self) -> dict:
        d = await AbstractDocumentSessionNanoServerHandler._execute_initial(self)
        try:
            await self.document_session.write(self.messages)
        except Exception as e:
            raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Failed to process messages", self) from e
        return d

    async def _execute_part(self, part: int, s_hmac_start_part: Optional[int]) -> dict:  # pragma: no cover
        raise NanoHandlerError(NANO_REQUEST_ERROR_HANDLER_BAD_REQUEST, "Part request is invalid", self)

    def _log_message(self) -> str:
        return f"Write document session {log_escape_str(self.session)}"

    def build_error_message(self, buf: StringIO) -> None:
        buf.write("Write document session ")
        buf.write(log_escape_str(self.payload.get(DocumentWsWriteRequest.SESSION)))
