import os
from typing import Optional

from ._MBContext import MBContext
from ._MBMessage import MBMessage
from ._MBService import MBService


class MBEasyContext(MBContext):
    def __init__(
        self,
        name: str,
        hv_service: Optional[MBService] = None,
        comp_service: Optional[MBService] = None,
    ):
        self.hv_service = hv_service
        if self.hv_service is None:
            address = os.environ["HVIP"]
            hv_port = int(os.environ["HVPORT"])

            self.hv_service = MBService(
                address=address,
                port=hv_port,
            )

        self.comp_service = comp_service
        if self.comp_service is None:
            address = os.environ["HVIP"]
            comp_port = int(os.environ["HVCOMPPORT"])
            self.comp_service = MBService(
                address=address,
                port=comp_port,
            )

        super().__init__(
            self.comp_service,
            name,
        )

    def MB_EasySendFullMessage(
        self,
        dest_name,
        token,
        format=0,
        data=None,
        headers=None,
        timeout=-1,
        ttl=-1,
    ) -> MBMessage:

        destination = super().MB_LocateService(
            hv_service=self.hv_service,
            service_name=dest_name,
        )
        destination.name = dest_name

        return super().MB_SendFullMessage(
            destination=destination,
            token=token,
            format=format,
            data=data,
            headers=headers,
            timeout=timeout,
            ttl=ttl,
        )

    def MB_EasySendMessage(
        self,
        dest_name: str,
        token: int,
        format: int = 0,
        data: Optional[bytes] = None,
        timeout: int = -1
    ) -> MBMessage:

        return self.MB_EasySendFullMessage(
            dest_name=dest_name,
            token=token,
            format=format,
            data=data,
            timeout=timeout,
        )

    def MB_EasyGetMessage(self, timeout:Optional[int]=None) -> MBMessage:
        return super().MB_GetMessage(timeout=timeout)

    def MB_EasyReplyMessage(self, message: MBMessage) -> None:
        return super().MB_ReplyMessage(message=message)

    def MB_EasyWaitStart(self, exported_services=None, required_services=None) -> int:
        return super().MB_WaitStart(
            exported_services=exported_services,
            required_services=required_services,
        )

    def MB_EasyEvtSubscribe(self, subject: str) -> None:
        super().MB_EvtSubscribe(
            hv_service=self.hv_service,
            subject=subject,
        )

    def MB_EasyEvtUnsubscribe(self, subject: str) -> None:
        super().MB_EvtUnsubscribe(
            hv_service=self.hv_service,
            subject=subject,
        )

    def MB_EasyEvtSend(
        self,
        subject: str,
        type: str,
        xml: str,
        synchronous: bool = False,
        sourceid: int = 0,
        queue: str = None,
        timeout: int = -1,
    ):
        return self.MB_EasyEvtSendFull(
            subject=subject,
            type=type,
            headers=None,
            xml=xml,
            synchronous=synchronous,
            sourceid=sourceid,
            queue=queue,
            timeout=timeout,
            ttl=-1,
        )

    def MB_EasyEvtSendFull(
        self,
        subject: str,
        type: str,
        headers: Optional[bytes],
        xml: str,
        synchronous: bool = False,
        sourceid: int = 0,
        queue: str = None,
        timeout: int = -1,
        ttl: int = -1,
    ):
        return super().MB_EvtSendFull(
            hv_service=self.hv_service,
            headers=headers,
            subject=subject,
            type=type,
            xml=xml,
            synchronous=synchronous,
            sourceid=sourceid,
            queue=queue,
            timeout=timeout,
            ttl=ttl,
        )

    def MB_EasyFinalize(self) -> None:
        super().MB_Finalize()

    def MB_EasyRegisterService(
        self,
        service_type,
        service_name
    ):
        return super().MB_RegisterService(
            my_service=self.comp_service,
            hv_service=self.hv_service,
            service_type=service_type,
            service_name=service_name,
        )
