# Copyright 2026 Espressif Systems (Shanghai) Co., Ltd.
#
# 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 abc import ABC, abstractmethod
from typing import Any

from esp_viewer.core.protocol import Packet


class BaseFrontendAdapter(ABC):
    """
    Abstract base class for all frontend adapters (e.g., PySide, Web UI, CLI).
    It defines the contract that any UI layer must implement to receive
    events from the Engine, and provides methods to send commands back to the Engine.
    """

    def __init__(self):
        self.engine = None

    def bind_engine(self, engine):
        """
        Binds the adapter to the core EspViewerEngine.
        Called automatically by the engine upon registration.
        """
        self.engine = engine

    @abstractmethod
    def on_event_received(self, packet: Packet):
        """
        Called by the Engine's dispatch thread whenever a new event
        (e.g., plot data, log message, status change) is generated by the DeviceService.

        Concrete adapters must implement this to route the packet to their
        specific UI mechanism (e.g., emitting a PySide Signal, sending a WebSocket frame).

        Warning: This is executed in the Engine's dispatch thread context.
        """
        pass
