"""
Distribution operations mixin for pyPhotoAlbum
"""

from pyPhotoAlbum.decorators import ribbon_action
from pyPhotoAlbum.alignment import AlignmentManager
from pyPhotoAlbum.commands import AlignElementsCommand


class DistributionOperationsMixin:
    """Mixin providing element distribution and spacing operations"""

    def _get_selected_elements_list(self):
        """Get list of selected elements for distribution operations"""
        return list(self.gl_widget.selected_elements) if self.gl_widget.selected_elements else []

    def _execute_distribution(self, distribution_func, status_msg: str):
        """
        Execute a distribution operation with common boilerplate.

        Args:
            distribution_func: AlignmentManager method to call with elements
            status_msg: Status message format string (will receive element count)
        """
        elements = self._get_selected_elements_list()
        if not self.require_selection(min_count=3):  # type: ignore[attr-defined]
            return

        changes = distribution_func(elements)
        if changes:
            cmd = AlignElementsCommand(changes)
            self.project.history.execute(cmd)  # type: ignore[attr-defined]
            self.update_view()  # type: ignore[attr-defined]
            self.show_status(status_msg.format(len(elements)), 2000)  # type: ignore[attr-defined]

    @ribbon_action(
        label="Distribute H",
        tooltip="Distribute selected elements evenly horizontally",
        tab="Arrange",
        group="Distribute",
        requires_selection=True,
        min_selection=3,
    )
    def distribute_horizontally(self):
        """Distribute selected elements evenly horizontally"""
        self._execute_distribution(AlignmentManager.distribute_horizontally, "Distributed {} elements horizontally")

    @ribbon_action(
        label="Distribute V",
        tooltip="Distribute selected elements evenly vertically",
        tab="Arrange",
        group="Distribute",
        requires_selection=True,
        min_selection=3,
    )
    def distribute_vertically(self):
        """Distribute selected elements evenly vertically"""
        self._execute_distribution(AlignmentManager.distribute_vertically, "Distributed {} elements vertically")

    @ribbon_action(
        label="Space H",
        tooltip="Space selected elements equally horizontally",
        tab="Arrange",
        group="Distribute",
        requires_selection=True,
        min_selection=3,
    )
    def space_horizontally(self):
        """Space selected elements equally horizontally"""
        self._execute_distribution(AlignmentManager.space_horizontally, "Spaced {} elements horizontally")

    @ribbon_action(
        label="Space V",
        tooltip="Space selected elements equally vertically",
        tab="Arrange",
        group="Distribute",
        requires_selection=True,
        min_selection=3,
    )
    def space_vertically(self):
        """Space selected elements equally vertically"""
        self._execute_distribution(AlignmentManager.space_vertically, "Spaced {} elements vertically")
