"""
Asset path resolution mixin for components that need to resolve asset paths.
"""

import os
from typing import Optional


class AssetPathMixin:
    """
    Mixin providing asset path resolution functionality.

    Requires access to self.project (typically via ApplicationStateMixin).
    """

    def resolve_asset_path(self, asset_path: str) -> Optional[str]:
        """
        Resolve a relative asset path to an absolute path.

        Args:
            asset_path: Relative path (e.g., "assets/photo.jpg") or absolute path

        Returns:
            Absolute path if the asset exists, None otherwise
        """
        if not asset_path:
            return None

        # Handle absolute paths
        if os.path.isabs(asset_path):
            if os.path.exists(asset_path):
                return asset_path
            return None

        # Resolve relative path using project folder
        project_folder = self._get_project_folder()
        if project_folder:
            full_path = os.path.join(project_folder, asset_path)
            if os.path.exists(full_path):
                return full_path

        return None

    def get_asset_full_path(self, relative_path: str) -> Optional[str]:
        """
        Get the full path for a relative asset path (without existence check).

        Args:
            relative_path: Relative path from project folder

        Returns:
            Full absolute path, or None if no project folder
        """
        project_folder = self._get_project_folder()
        if project_folder and relative_path:
            return os.path.join(project_folder, relative_path)
        return None

    def _get_project_folder(self) -> Optional[str]:
        """
        Get the current project folder.

        Override this method if the project is accessed differently.
        Default implementation uses self.project.folder_path.
        """
        if hasattr(self, "project") and self.project:
            return getattr(self.project, "folder_path", None)
        return None
