"""Fake vendor SDK wrapper interface for {{ interface_name }}.

Simulated SDK wrapper for testing without the vendor library.

Generated by: madsci new comm --template comm/sdk
Template version: {{ template_version }}
"""

import logging
import time
from typing import Any, Optional

logger = logging.getLogger(__name__)


class {{ interface_name | pascal_case }}FakeInterface:
    """Fake SDK wrapper interface for testing {{ interface_name }} without the vendor SDK.

    Simulates SDK behavior with configurable function return values and
    property storage.

    Example:
        >>> interface = {{ interface_name | pascal_case }}FakeInterface()
        >>> interface.set_function_response("measure", 42.0)
        >>> interface.load_library()
        >>> interface.connect()
        >>> result = interface.call_function("measure", channel=1)
        >>> assert result == 42.0
    """

    def __init__(
        self,
        library_path: Optional[str] = None,
        simulated_latency: float = 0.01,
        **kwargs: Any,
    ) -> None:
        """Initialize the fake SDK wrapper interface.

        Args:
            library_path: Simulated library path.
            simulated_latency: Artificial delay for each operation in seconds.
            **kwargs: Additional configuration (ignored).
        """
        self._library_path = library_path
        self._simulated_latency = simulated_latency
        self._is_loaded = False
        self._is_connected = False
        self._function_responses: dict[str, Any] = {}
        self._properties: dict[str, Any] = {}
        self._call_history: list[dict[str, Any]] = []
        self._operation_count = 0

    @property
    def is_loaded(self) -> bool:
        """Check if the SDK library has been loaded."""
        return self._is_loaded

    @property
    def is_connected(self) -> bool:
        """Check if the SDK is connected."""
        return self._is_connected

    @property
    def operation_count(self) -> int:
        """Get the total number of SDK operations performed."""
        return self._operation_count

    @property
    def call_history(self) -> list[dict[str, Any]]:
        """Get a copy of the function call history."""
        return list(self._call_history)

    def set_function_response(self, func_name: str, response: Any) -> None:
        """Configure the return value for a simulated SDK function.

        Args:
            func_name: Name of the SDK function.
            response: Value to return when the function is called.
        """
        self._function_responses[func_name] = response

    def load_library(self) -> None:
        """Simulate loading the vendor SDK library."""
        time.sleep(self._simulated_latency)
        self._is_loaded = True
        logger.info(f"[FAKE] Loaded vendor SDK from {self._library_path}")

    def connect(self) -> None:
        """Simulate connecting to the instrument via SDK.

        Raises:
            RuntimeError: If the SDK library has not been loaded.
        """
        if not self._is_loaded:
            raise RuntimeError("SDK not loaded. Call load_library() first.")
        time.sleep(self._simulated_latency)
        self._is_connected = True
        logger.info("[FAKE] Connected to instrument via SDK")

    def disconnect(self) -> None:
        """Simulate disconnecting from the instrument."""
        if self._is_connected:
            self._is_connected = False
            logger.info("[FAKE] Disconnected from instrument via SDK")

    def call_function(self, func_name: str, **kwargs: Any) -> Any:
        """Simulate calling a function on the vendor SDK.

        Args:
            func_name: Name of the SDK function.
            **kwargs: Arguments to pass.

        Returns:
            The configured response, or None if not configured.

        Raises:
            RuntimeError: If not connected.
        """
        if not self._is_connected:
            raise RuntimeError("Not connected. Call connect() first.")
        time.sleep(self._simulated_latency)
        self._operation_count += 1
        self._call_history.append({
            "function": func_name,
            "kwargs": kwargs,
        })
        return self._function_responses.get(func_name)

    def get_property(self, name: str) -> Any:
        """Simulate getting a property from the SDK.

        Args:
            name: Property name to read.

        Returns:
            The property value, or None if not set.

        Raises:
            RuntimeError: If not connected.
        """
        if not self._is_connected:
            raise RuntimeError("Not connected. Call connect() first.")
        self._operation_count += 1
        return self._properties.get(name)

    def set_property(self, name: str, value: Any) -> None:
        """Simulate setting a property on the SDK.

        Args:
            name: Property name to set.
            value: Value to assign.

        Raises:
            RuntimeError: If not connected.
        """
        if not self._is_connected:
            raise RuntimeError("Not connected. Call connect() first.")
        self._operation_count += 1
        self._properties[name] = value

    def reset(self) -> None:
        """Reset all internal state for test isolation."""
        self._function_responses = {}
        self._properties = {}
        self._call_history = []
        self._operation_count = 0
