from PySide6.QtWidgets import QWidget, QVBoxLayout

from clochette.application.service.provider.dto.ProviderDTO import ProviderDTO
from clochette.application.service.provider.dto.ProviderType import ProviderType
from clochette.framework.qt.Signal import InSolidSignal
from clochette.presentation.widget.QDynamicWidgetContainer import QDynamicWidgetContainer


class QAuthenticationSourceView(QWidget):
    """Panel that displays provider-specific authentication UI based on selected provider type"""

    set_selected_provider_type: InSolidSignal[ProviderType]
    set_providers: InSolidSignal[list[ProviderDTO]]

    _container: QDynamicWidgetContainer[ProviderType]

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

        self.set_selected_provider_type = InSolidSignal(self._set_selected_provider_type)
        self.set_providers = InSolidSignal(self._set_providers)

        layout = QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(layout)

        self._container = QDynamicWidgetContainer[ProviderType]()
        layout.addWidget(self._container)

        layout.addStretch()

    def _set_providers(self, providers: list[ProviderDTO]) -> None:
        widgets = {provider.provider_type: provider.authentication_component.view() for provider in providers}
        self._container.set_widgets(widgets)

    def _set_selected_provider_type(self, provider_type: ProviderType) -> None:
        self._container.show_widget(provider_type)

    def get_selected_provider_type(self) -> ProviderType:
        return self._container.current_key()
