"""
Dialog operations mixin for pyPhotoAlbum

Provides common functionality for creating and managing dialogs.
"""

from typing import Optional, Any, Callable
from PyQt6.QtWidgets import QDialog


class DialogMixin:
    """
    Mixin providing dialog creation and management capabilities.

    This mixin separates dialog UI concerns from business logic,
    making it easier to create, test, and maintain complex dialogs.
    """

    def create_dialog(self, dialog_class: type, title: Optional[str] = None, **kwargs) -> Optional[Any]:
        """
        Create and show a dialog, handling the result.

        Args:
            dialog_class: Dialog class to instantiate
            title: Optional title override
            **kwargs: Additional arguments passed to dialog constructor

        Returns:
            Dialog result if accepted, None if rejected
        """
        # Create dialog instance
        dialog = dialog_class(parent=self, **kwargs)

        # Set title if provided
        if title:
            dialog.setWindowTitle(title)

        # Show dialog and handle result
        if dialog.exec() == QDialog.DialogCode.Accepted:
            # Check if dialog has a get_values method
            if hasattr(dialog, "get_values"):
                return dialog.get_values()
            return True

        return None

    def show_dialog(self, dialog_class: type, on_accept: Optional[Callable] = None, **kwargs) -> bool:
        """
        Show a dialog and execute callback on acceptance.

        Args:
            dialog_class: Dialog class to instantiate
            on_accept: Callback to execute if dialog is accepted.
                      Will receive dialog result as parameter.
            **kwargs: Additional arguments passed to dialog constructor

        Returns:
            True if dialog was accepted, False otherwise
        """
        result = self.create_dialog(dialog_class, **kwargs)

        if result is not None and on_accept:
            on_accept(result)
            return True

        return result is not None
