# Copyright 2025-2026 Dimensional Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import Any, TypeVar

import dimos.core.colors as colors

T = TypeVar("T")

from typing import (
    TYPE_CHECKING,
    TypeVar,
)

from dimos.core.stream import In, Out, Stream, Transport
from dimos.msgs.protocol import DimosMsg
from dimos.protocol.pubsub.jpeg_shm import JpegSharedMemory
from dimos.protocol.pubsub.lcmpubsub import LCM, JpegLCM, PickleLCM, Topic as LCMTopic
from dimos.protocol.pubsub.rospubsub import DimosROS, ROSTopic
from dimos.protocol.pubsub.shmpubsub import BytesSharedMemory, PickleSharedMemory

if TYPE_CHECKING:
    from collections.abc import Callable

T = TypeVar("T")  # type: ignore[misc]


class PubSubTransport(Transport[T]):
    topic: Any

    def __init__(self, topic: Any) -> None:
        self.topic = topic

    def __str__(self) -> str:
        return (
            colors.green(f"{self.__class__.__name__}(")
            + colors.blue(self.topic)
            + colors.green(")")
        )


class pLCMTransport(PubSubTransport[T]):
    _started: bool = False

    def __init__(self, topic: str, **kwargs) -> None:  # type: ignore[no-untyped-def]
        super().__init__(topic)
        self.lcm = PickleLCM(**kwargs)

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (pLCMTransport, (self.topic,))

    def broadcast(self, _: Out[T] | None, msg: T) -> None:
        if not self._started:
            self.start()

        self.lcm.publish(self.topic, msg)

    def subscribe(
        self, callback: Callable[[T], Any], selfstream: Stream[T] | None = None
    ) -> Callable[[], None]:
        if not self._started:
            self.start()
        return self.lcm.subscribe(self.topic, lambda msg, topic: callback(msg))

    def start(self) -> None:
        self.lcm.start()
        self._started = True

    def stop(self) -> None:
        self.lcm.stop()
        self._started = False


class LCMTransport(PubSubTransport[T]):
    _started: bool = False

    def __init__(self, topic: str, type: type, **kwargs) -> None:  # type: ignore[no-untyped-def]
        super().__init__(LCMTopic(topic, type))
        if not hasattr(self, "lcm"):
            self.lcm = LCM(**kwargs)

    def start(self) -> None:
        self.lcm.start()
        self._started = True

    def stop(self) -> None:
        self.lcm.stop()
        self._started = False

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (LCMTransport, (self.topic.topic, self.topic.lcm_type))

    def broadcast(self, _, msg) -> None:  # type: ignore[no-untyped-def]
        if not self._started:
            self.start()

        self.lcm.publish(self.topic, msg)

    def subscribe(self, callback: Callable[[T], None], selfstream: In[T] = None) -> None:  # type: ignore[assignment, override]
        if not self._started:
            self.start()
        return self.lcm.subscribe(self.topic, lambda msg, topic: callback(msg))  # type: ignore[return-value]


class JpegLcmTransport(LCMTransport):  # type: ignore[type-arg]
    def __init__(self, topic: str, type: type, **kwargs) -> None:  # type: ignore[no-untyped-def]
        self.lcm = JpegLCM(**kwargs)  # type: ignore[assignment]
        super().__init__(topic, type)

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (JpegLcmTransport, (self.topic.topic, self.topic.lcm_type))

    def start(self) -> None:
        self.lcm.start()
        self._started = True

    def stop(self) -> None:
        self.lcm.stop()
        self._started = False


class pSHMTransport(PubSubTransport[T]):
    _started: bool = False

    def __init__(self, topic: str, **kwargs) -> None:  # type: ignore[no-untyped-def]
        super().__init__(topic)
        self.shm = PickleSharedMemory(**kwargs)

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (pSHMTransport, (self.topic,))

    def broadcast(self, _, msg) -> None:  # type: ignore[no-untyped-def]
        if not self._started:
            self.start()

        self.shm.publish(self.topic, msg)

    def subscribe(self, callback: Callable[[T], None], selfstream: In[T] = None) -> None:  # type: ignore[assignment, override]
        if not self._started:
            self.start()
        return self.shm.subscribe(self.topic, lambda msg, topic: callback(msg))  # type: ignore[return-value]

    def start(self) -> None:
        self.shm.start()
        self._started = True

    def stop(self) -> None:
        self.shm.stop()
        self._started = False


class SHMTransport(PubSubTransport[T]):
    _started: bool = False

    def __init__(self, topic: str, **kwargs) -> None:  # type: ignore[no-untyped-def]
        super().__init__(topic)
        self.shm = BytesSharedMemory(**kwargs)

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (SHMTransport, (self.topic,))

    def broadcast(self, _, msg) -> None:  # type: ignore[no-untyped-def]
        if not self._started:
            self.start()

        self.shm.publish(self.topic, msg)

    def subscribe(self, callback: Callable[[T], None], selfstream: In[T] | None = None) -> None:  # type: ignore[override]
        if not self._started:
            self.start()
        return self.shm.subscribe(self.topic, lambda msg, topic: callback(msg))  # type: ignore[arg-type, return-value]

    def start(self) -> None:
        self.shm.start()
        self._started = True

    def stop(self) -> None:
        self.shm.stop()
        self._started = False


class JpegShmTransport(PubSubTransport[T]):
    _started: bool = False

    def __init__(self, topic: str, quality: int = 75, **kwargs) -> None:  # type: ignore[no-untyped-def]
        super().__init__(topic)
        self.shm = JpegSharedMemory(quality=quality, **kwargs)
        self.quality = quality

    def __reduce__(self):  # type: ignore[no-untyped-def]
        return (JpegShmTransport, (self.topic, self.quality))

    def broadcast(self, _, msg) -> None:  # type: ignore[no-untyped-def]
        if not self._started:
            self.start()

        self.shm.publish(self.topic, msg)

    def subscribe(self, callback: Callable[[T], None], selfstream: In[T] | None = None) -> None:  # type: ignore[override]
        if not self._started:
            self.start()
        return self.shm.subscribe(self.topic, lambda msg, topic: callback(msg))  # type: ignore[arg-type, return-value]

    def start(self) -> None:
        self.shm.start()
        self._started = True

    def stop(self) -> None:
        self.shm.stop()
        self._started = False


class ROSTransport(PubSubTransport[DimosMsg]):
    _ros: DimosROS | None = None

    def __init__(self, topic: str, msg_type: type[DimosMsg], **kwargs: Any) -> None:
        super().__init__(ROSTopic(topic, msg_type))
        self._kwargs = kwargs

    def __reduce__(self) -> tuple[Any, ...]:
        return (ROSTransport, (self.topic.topic, self.topic.msg_type))

    def broadcast(self, _: Out[DimosMsg], msg: DimosMsg) -> None:
        if self._ros is None:
            self.start()
            assert self._ros is not None  # for type narrowing
        self._ros.publish(self.topic, msg)

    def subscribe(
        self, callback: Callable[[DimosMsg], Any], selfstream: Stream[DimosMsg] | None = None
    ) -> Callable[[], None]:
        if self._ros is None:
            self.start()
            assert self._ros is not None  # for type narrowing
        return self._ros.subscribe(self.topic, lambda msg, topic: callback(msg))

    def start(self) -> None:
        if self._ros is None:
            self._ros = DimosROS(**self._kwargs)
            self._ros.start()

    def stop(self) -> None:
        if self._ros is not None:
            self._ros.stop()
            self._ros = None


class ZenohTransport(PubSubTransport[T]): ...
