from zope.interface import implements
from collective.media.interfaces import ICanContainMedia
from Products.CMFCore.utils import getToolByName

class MediaHandling(object):
    """
    Handling media actions
    """
    implements(ICanContainMedia)
    
    def __init__(self, context):
        """
           Initialize our adapter
        """
        self.context = context
    
    def getMedia(self):
        """
        Gets media on the folderish item
        """
        item = self.context
        results = []
        
        catalog = getToolByName(self.context, 'portal_catalog')
        plone_utils = getToolByName(self.context, 'plone_utils')
        path = '/'.join(item.getPhysicalPath())
        
        if item.portal_type == "Folder" or (item.restrictedTraverse('@@plone').isStructuralFolder()  and item.portal_type != "Topic"):
            results = catalog.searchResults(path = {'query' : path}, type = ['Image', 'Link'], sort_on = 'getObjPositionInParent')
        elif item.portal_type == "Topic":
            if item.limitNumber:
                results = catalog.searchResults(item.buildQuery())[:item.itemCount]
            else:
                results = catalog.searchResults(item.buildQuery())
        
        resultArray = []
        
        for res in results:
            if res.portal_type == "Image":
                resultArray.append(res)
            elif res.portal_type == "Link" and (res.remoteUrl.find("youtube.com") > -1 or res.remoteUrl.find("vimeo.com") > -1):
                resultArray.append(res)
        
        return resultArray
    
    def hasMedia(self):
        """
        Check if item has media
        """
        return len(self.getMedia()) > 0
    
    def getLeadMedia(self):
        """
        Get the lead media
        """
        media = self.getMedia()
        if len(media) > 0:
            return media[0]
        else:
            return None
    
    def markLeadMedia(self, item):
        """
        Mark item as being the lead media of the current folder. unMark any other that might have the same interface.
        """
    
    def unmarkLeadMedia(self, item):
        """
        Remove the marker interface from item if it exists.
        """
    