#  Copyright (C) 2024
#  ABM, Moscow
#
#  UNPUBLISHED PROPRIETARY MATERIAL.
#  ALL RIGHTS RESERVED.
#
#  Authors: Vasya Svintsov <v.svintsov@techokert.ru>

from dataclasses import dataclass

from http_tools import AbmServiceConnector
from init_helpers.dict_to_dataclass import get_converter


class NetrisProviderConnector:
    @dataclass
    class Config(AbmServiceConnector.Config):
        snapshot_ssl: bool = True

    @dataclass
    class Context(AbmServiceConnector.Context):
        project_name: str

    def __init__(self, config: Config, context: Context) -> None:
        self.config = config
        self.context = context
        self._connector = AbmServiceConnector(config, context)

    async def get_camera_outer_id_to_inner_id(self, outer_ids: list[str]) -> dict[str, int]:
        camera_outer_id_to_inner_id = await self._connector.post(
            '/camera_matching/outer_id_to_inner_id/get', {'ids': outer_ids}, headers=self._prepare_headers(),
        )
        return get_converter(dict[str, int])(camera_outer_id_to_inner_id)

    async def get_camera_inner_id_to_outer_id(self, inner_ids: list[int]) -> dict[int, str]:
        camera_inner_id_to_outer_id = await self._connector.post(
            '/camera_matching/inner_id_to_outer_id/get', {'ids': inner_ids}, headers=self._prepare_headers(),
        )
        return get_converter(dict[int, str])(camera_inner_id_to_outer_id)

    async def get_snapshot_for_outer_camera(self, id: str) -> bytes:
        async with self._connector._connector._semaphore:
            response = await self.context.session.get(self._connector._connector._get_url('/snapshot/outer_camera/get'),
                                                      params={"id": id},
                                                      headers=self._prepare_headers(),
                                                      ssl=self.config.snapshot_ssl,
                                                      timeout=self.config.timeout_sec,
                                                      allow_redirects=True)
            snapshot = self._connector._parse_answer(
                await self._connector._connector._requester._process_response(response))

        assert isinstance(snapshot, bytes)
        return snapshot

    async def get_snapshot_for_inner_camera(self, id: int) -> bytes:
        async with self._connector._connector._semaphore:
            response = await self.context.session.get(self._connector._connector._get_url('/snapshot/inner_camera/get'),
                                                      params={"id": id},
                                                      headers=self._prepare_headers(),
                                                      ssl=self.config.snapshot_ssl,
                                                      timeout=self.config.timeout_sec,
                                                      allow_redirects=True)
            snapshot = self._connector._parse_answer(
                await self._connector._connector._requester._process_response(response))

        assert isinstance(snapshot, bytes)
        return snapshot

    def _prepare_headers(self) -> dict[str, str]:
        return {"server_name": self.context.project_name}
