"""Mock LED client — simulates an LED strip with configurable latency for testing."""

import asyncio
from datetime import datetime, timezone
from typing import List, Optional, Tuple, Union

import numpy as np

from wled_controller.core.devices.led_client import DeviceHealth, LEDClient
from wled_controller.utils import get_logger

logger = get_logger(__name__)


class MockClient(LEDClient):
    """LED client that simulates an LED strip without real hardware.

    Useful for load testing, development, and CI environments.
    """

    def __init__(
        self,
        url: str = "",
        led_count: int = 0,
        send_latency_ms: int = 0,
        rgbw: bool = False,
        **kwargs,
    ):
        self._led_count = led_count
        self._latency = send_latency_ms / 1000.0  # convert to seconds
        self._rgbw = rgbw
        self._connected = False

    async def connect(self) -> bool:
        self._connected = True
        logger.info(
            f"Mock device connected ({self._led_count} LEDs, "
            f"{'RGBW' if self._rgbw else 'RGB'}, "
            f"{int(self._latency * 1000)}ms latency)"
        )
        return True

    async def close(self) -> None:
        self._connected = False
        logger.info("Mock device disconnected")

    @property
    def is_connected(self) -> bool:
        return self._connected

    async def send_pixels(
        self,
        pixels: Union[List[Tuple[int, int, int]], np.ndarray],
        brightness: int = 255,
    ) -> bool:
        if not self._connected:
            return False
        if self._latency > 0:
            await asyncio.sleep(self._latency)
        return True

    @classmethod
    async def check_health(
        cls,
        url: str,
        http_client,
        prev_health: Optional[DeviceHealth] = None,
    ) -> DeviceHealth:
        return DeviceHealth(
            online=True,
            latency_ms=0.0,
            last_checked=datetime.now(timezone.utc),
        )
