from enum import Enum

from PySide6.QtWidgets import QWidget, QVBoxLayout

from clochette.framework.qt.Signal import InHollowSignal
from clochette.presentation.settings.calendar.list.QCalendarListComponent import (
    QCalendarListComponent,
)
from clochette.presentation.settings.calendar.tabs.QCalendarConfigurationComponent import (
    QCalendarConfigurationComponent,
)
from clochette.presentation.widget.QDynamicWidgetContainer import (
    QDynamicWidgetContainer,
)
from clochette.presentation.widget.QTitle import QTitle
from clochette.presentation.widget.QWidgetLayout import QWidgetLayout


class CalendarView(Enum):
    """Enum for the different calendar setting views"""

    LIST = "list"
    CONFIGURATION = "configuration"


class QCalendarSettingsView(QWidget):
    """Simple view widget for calendar settings with list and configuration panels"""

    show_calendar_list: InHollowSignal
    add_calendar: InHollowSignal
    edit_calendar: InHollowSignal

    _container: QDynamicWidgetContainer[CalendarView]
    _calendar_list_component: QCalendarListComponent

    def __init__(
        self,
        calendar_configuration_component: QCalendarConfigurationComponent,
        calendar_list_component: QCalendarListComponent,
    ):
        super().__init__()

        self.show_calendar_list = InHollowSignal(self._show_calendar_list)
        self.add_calendar = InHollowSignal(self._add_calendar)
        self.edit_calendar = InHollowSignal(self._edit_calendar)

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

        self._container = QDynamicWidgetContainer[CalendarView]()
        main_layout.addWidget(self._container)

        # calendar list
        calendar_widget = QVBoxLayout()
        calendar_widget.addWidget(
            QTitle(
                self.tr("Calendars Source"),
                self.tr("Tell Clochette how to access your calendars"),
            )
        )

        self._calendar_list_component = calendar_list_component
        calendar_widget.addWidget(self._calendar_list_component.view())
        calendar_widget.addStretch()

        cal_list_widget = QWidget()
        cal_list_widget.setContentsMargins(0, 0, 0, 0)
        cal_list_widget.setLayout(calendar_widget)
        self._container.add_widget(CalendarView.LIST, cal_list_widget)

        # unified configuration panel (authentication + settings)
        configuration_widget = QWidgetLayout(QVBoxLayout())
        configuration_widget.add_widget(calendar_configuration_component.view())
        self._container.add_widget(CalendarView.CONFIGURATION, configuration_widget)

        main_layout.addStretch()

    def _show_calendar_list(self):
        """Show the calendar list view"""
        self._container.show_widget(CalendarView.LIST)

    def _add_calendar(self):
        """Show the add calendar configuration view"""
        self._container.show_widget(CalendarView.CONFIGURATION)

    def _edit_calendar(self):
        """Show the edit calendar configuration view"""
        self._container.show_widget(CalendarView.CONFIGURATION)
