# Copyright (c) 2018 Ultimaker B.V.
# Uranium is released under the terms of the LGPLv3 or higher.
# Modified by BigRep GmbH 2024

from UM.Signal import Signal
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin

from cura.CuraApplication import CuraApplication

from .BigRepConnectOutputDeviceManager import BigRepConnectOutputDeviceManager

class BigRepConnectOutputDevicePlugin(OutputDevicePlugin):
    """This plugin handles the discovery and networking for BigRep printers via CONNECT"""

    discoveredDevicesChanged = Signal()
    """Signal emitted when the list of discovered devices changed. Used by printer action in this plugin."""

    def __init__(self):
        super().__init__()

        self._connect_output_device_manager = BigRepConnectOutputDeviceManager()

        # Adjust the output device if the active machine changes
        CuraApplication.getInstance().globalContainerStackChanged.connect(self.refreshConnections)

    def start(self):
        self._connect_output_device_manager.start()

    def stop(self):
        self._connect_output_device_manager.stop()

    def refreshConnections(self) -> None:
        self._connect_output_device_manager.refreshConnections()
