// ----------------------------------------------------------------------------
// Create the playback utility object
// ----------------------------------------------------------------------------
//
EXPORT_C void CMPXAlbumArtUtility::ExtractAlbumArtL(const CMPXMedia& aMedia,
                                            MMPXAlbumArtUtilityObserver& aObs,
                                            const TSize& aSize, 
                                            TDisplayMode aDisplayMode /*= EColor64K*/)
    {
    MPX_FUNC_EX( "CMPXAlbumArtUtility::ExtractAlbumArtL" );

    if ( iCurrentOp != EIdle )
        {
        User::Leave( KErrNotReady );
        }

    if ( aMedia.IsSupported( KMPXMediaMusicAlbumArtFileName ) )
        {
        if ( aMedia.ValueText( KMPXMediaMusicAlbumArtFileName ).Length() == 0)
            {
            User::Leave( KErrNotFound );
            }
        }

    if ( aMedia.IsSupported( KMPXMediaGeneralUri ) &&
         aMedia.IsSupported( KMPXMediaMusicAlbumArtFileName ))
        {
        if ( aMedia.ValueText( KMPXMediaGeneralUri ).CompareF( 
             aMedia.ValueText( KMPXMediaMusicAlbumArtFileName ) ) == 0 )
            { // embedded album art            
            iObs = &aObs;
            delete iAlbumArt;
            iAlbumArt = NULL;
            iAlbumArt = iImageUtil->ExtractL( aMedia.ValueText( KMPXMediaMusicAlbumArtFileName ) );
            if ( iAlbumArt )
                {
                iCurrentOp = EExtractAlbumArtL;
                iObs->ExtractAlbumArtStarted();
                SetActive();
                iImageUtil->Decode( iStatus, *iAlbumArt, *iBitmap, aSize, aDisplayMode );                
                }
            else
                {
                User::Leave( KErrUnderflow );
                }            
            }
        else
            {
            iCurrentOp = EExtractAlbumArtL;
            // TO-DO: if we save externalize bitmap, we only need to internalize bitmap here.
            iObs->ExtractAlbumArtStarted();
            SetActive();
            iImageUtil->Decode( iStatus, aMedia.ValueText( 
                KMPXMediaMusicAlbumArtFileName ), *iBitmap, aSize, aDisplayMode );
            }    
        }
    else
        {
        User::Leave( KErrNotFound );
        }
    }
// ----------------------------------------------------------------------------------------------------------
// Filter out media in aMediaArray which match aFilter
// ----------------------------------------------------------------------------------------------------------
//
void CTestCollectionPlugin::FilterMediaArray(CMPXMediaArray& aMediaArray, CMPXFilter* aFilter)
{
    if(aFilter )
    {
        TArray<TMPXAttribute> filterAttr = aFilter->Attributes();
        TInt arrCnt = aMediaArray.Count();
        for(TInt i = arrCnt-1; i >= 0; i--) // Remove from the back
        {
            CMPXMedia* media = aMediaArray[i];
            for(TInt ii = 0; ii < filterAttr.Count(); ii++)
            {
                TMPXAttribute attr = filterAttr[ii];
                if( media->IsSupported( attr ) )
                {
                    TBool match = EFalse;
                    if(attr == KMPXMediaGeneralId)
                    {
                        TInt filterId = *aFilter->Value<TInt>( attr );
                        TInt mediaId = *media->Value<TInt>( attr );
                        if(filterId == mediaId)
                            match = ETrue;
                    }
                    else if(attr == KMPXMediaGeneralTitle || attr == KMPXMediaGeneralUri)
                    {
                        const TDesC& filterText = aFilter->ValueText( attr );
                        const TDesC& mediaText = media->ValueText( attr );
                        if(filterText == mediaText)
                            match = ETrue;
                    }
                    if( match )
                    {
                        aMediaArray.Remove( i );
                        break;
                    }
                }
            }
        }
    }
}
// ---------------------------------------------------------------------------
// From MMPXPlaybackCallback
// Handle media
// ---------------------------------------------------------------------------
//
void CAiPlayerPluginEngine::HandleMediaL( const CMPXMedia& aMedia, 
        TInt aError )
    {
    //MPX_DEBUG1("CAiPlayerPluginEngine::HandleMediaL");
    if ( KErrNone == aError )
        {
        delete iUri;
        iUri = NULL;
        if (aMedia.IsSupported(KMPXMediaGeneralUri))
            {
            TParsePtrC filePath(aMedia.ValueText(KMPXMediaGeneralUri) );
            iUri = filePath.FullName().AllocL();
            }
        
		delete iTitle;
		iTitle = NULL;
        if ( aMedia.IsSupported( KMPXMediaGeneralTitle ) )
            {
            iTitle = ( aMedia.ValueText( KMPXMediaGeneralTitle ) ).AllocL();
            }
        else if ( aMedia.IsSupported( KMPXMediaGeneralUri ) )
            {
            TParsePtrC filePath( aMedia.ValueText( KMPXMediaGeneralUri ) );
            iTitle = (filePath.Name()).AllocL();
            }
		delete iArtist;
		iArtist = NULL;
		iArtist = ( aMedia.ValueText( KMPXMediaMusicArtist ) ).AllocL();
		
		iObserver->TrackInfoChanged( *iTitle, *iArtist );
        
		if (!iSkipping)
            {
            if (iExtractingAlbumArt)
                {
                //iAlbumArtUtil->CancelRequest();
                iExtractingAlbumArt=EFalse;
                }
            /*
            if ( aMedia.IsSupported( KMPXMediaMusicAlbumArtFileName ) )
                {
                delete iMedia;
                iMedia=NULL;
                iMedia = CMPXMedia::NewL( aMedia );
             
                TRAPD(err,iAlbumArtUtil->ExtractAlbumArtL(
                        *iMedia,
                        *this,
                        TSize(70,70)));
                
                if (err != KErrNone)
                    {
                    iObserver->AlbumArtChanged(NULL);
                    }
                }
            else
                {
                iObserver->AlbumArtChanged(NULL);
                }
                */
            }
		else
		    {
		    iObserver->AlbumArtChanged(NULL);
		    }
        }
    }
// ---------------------------------------------------------------------------
// Retrieve a media object based on file path
// ---------------------------------------------------------------------------
//
CMPXMedia* CMPXCollectionHelperImp::GetL( const TDesC& aFile,
                                          const TArray<TMPXAttribute>& aAttrs,
                                          TMPXGeneralCategory aItemCat )
    {
    MPX_FUNC("CMPXCollectionHelperImp::GetL");
    MPX_DEBUG2("aFile %S", &aFile);
#ifdef ABSTRACTAUDIOALBUM_INCLUDED
    if (aItemCat != EMPXSong && aItemCat != EMPXPlaylist && aItemCat != EMPXAbstractAlbum)
#else
    if (aItemCat != EMPXSong && aItemCat != EMPXPlaylist)
#endif
        {
        User::Leave(KErrArgument);
        }

    // Playlists synced through MTP contains .pla extension and there
    // is no pla playlist plugin. We can no longer reply on harvester
    // utility's IsPlaylistL to determine if this is a playlist.
    // For now, the default collection to get this playlist is
    // EMPXCollectionPluginMusic for playlists. This will be revised
    // later when there is a generic way of resolving collections aside
    // from using file extension or UID. Same for virtual playlists.
    //
    // Find the collection Id from harvester for songs or playlists
    // scanned from the file system
    TInt col(iMusicCollectionId.iUid);
    if (aItemCat == EMPXSong || iHvsUtility->IsPlaylistL(aFile))
        {
        col = iHvsUtility->FindCollectionIdL( aFile );
        }

    // Search criteria
    //
    CMPXMedia* search = CMPXMedia::NewL();
    CleanupStack::PushL( search );
    search->SetTObjectValueL( KMPXMediaGeneralType, EMPXItem );
    search->SetTObjectValueL( KMPXMediaGeneralCategory, aItemCat );
    search->SetTObjectValueL<TUid>( KMPXMediaGeneralCollectionId, TUid::Uid(col) );
    search->SetTextValueL( KMPXMediaGeneralUri, aFile );

    // Search in synch mode
    //
    CMPXMedia* result = iCollectionUtil->Collection().FindAllL( *search, aAttrs );
    CleanupStack::PopAndDestroy( search );
    CleanupStack::PushL(result);

    const CMPXMediaArray* ary(NULL);
    if( result->IsSupported( KMPXMediaArrayContents ) )
      {
      ary = result->Value<CMPXMediaArray>(KMPXMediaArrayContents);
      if( !ary )
          {
          User::Leave(KErrNoMemory);
          }
      }

    CMPXMedia* foundMedia(NULL);

    if ( ary && ary->Count() )
        {
        foundMedia = CMPXMedia::NewL(*ary->AtL(0));
        CleanupStack::PushL(foundMedia);
        foundMedia->SetTObjectValueL<TUid>( KMPXMediaGeneralCollectionId, TUid::Uid(col) );
        CleanupStack::Pop(foundMedia);
        }
    else
        {
        User::Leave(KErrNotFound);    
        }
    
    CleanupStack::PopAndDestroy(result);
    return foundMedia;
    }
// ---------------------------------------------------------------------------
// From MMPXPlaybackCallback
// Handle media
// ---------------------------------------------------------------------------
//
void CAiPlayerPluginEngine::HandleMediaL( const CMPXMedia& aMedia, 
        TInt aError )
    {
    if ( KErrNone == aError )
        {
        delete iUri;
        iUri = NULL;
        if (aMedia.IsSupported(KMPXMediaGeneralUri))
            {
            TParsePtrC filePath(aMedia.ValueText(KMPXMediaGeneralUri) );
            iUri = filePath.FullName().AllocL();
            }
        
		delete iTitle;
		iTitle = NULL;
        if ( aMedia.IsSupported( KMPXMediaGeneralTitle ) )
            {
            iTitle = ( aMedia.ValueText( KMPXMediaGeneralTitle ) ).AllocL();
            }
        else if ( aMedia.IsSupported( KMPXMediaGeneralDuration))
        {
            RDebug::Print(_L("duration found"));
            TInt val=aMedia.ValueTObjectL<TInt>( KMPXMediaGeneralDuration );
            RDebug::Print(_L("val %d"),val);
            iDuration=val/KMPXOneSecInMilliSecs;
        }
        else if ( aMedia.IsSupported( KMPXMediaGeneralUri ) )
            {
            TParsePtrC filePath( aMedia.ValueText( KMPXMediaGeneralUri ) );
            iTitle = (filePath.Name()).AllocL();
            }
		delete iArtist;
		iArtist = NULL;
		if ( aMedia.IsSupported( KMPXMediaMusicArtist ) )
		    {
		    iArtist = ( aMedia.ValueText( KMPXMediaMusicArtist ) ).AllocL();
		    }

		iObserver->TrackInfoChanged(iTitle ? *iTitle : KNullDesC(), iArtist ? *iArtist : KNullDesC());

		if (!iSkipping)
            {
            if (iExtractingAlbumArt)
                {
                if (iAlbumArtUtil){iAlbumArtUtil->CancelRequest();}
                iExtractingAlbumArt=EFalse;
                }
            
            if ( aMedia.IsSupported( KMPXMediaMusicAlbumArtFileName ) )
                {
                delete iMedia;
                iMedia=NULL;
                iMedia = CMPXMedia::NewL( aMedia );
                TRAPD(err,iAlbumArtUtil->ExtractAlbumArtL(
                        *iMedia,
                        *this,
                        KAlbumArtSize,
                        EFalse));
                
                if (err != KErrNone)
                    {
                    iObserver->AlbumArtChanged(NULL);
                    }
                }
            else
                {
                iObserver->AlbumArtChanged(NULL);
                }

            }
		else
		    {
		    iObserver->AlbumArtChanged(NULL);
		    }
        }
    }