package com.github.dynamo.backlog.tasks.music;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import com.github.dynamo.backlog.BackLogProcessor;
import com.github.dynamo.backlog.tasks.core.FindDownloadableImageExecutor;
import com.github.dynamo.core.manager.ErrorManager;
import com.github.dynamo.manager.DownloadableManager;
import com.github.dynamo.manager.MusicManager;
import com.github.dynamo.model.music.MusicAlbum;
import com.github.dynamo.music.TheAudioDb;
import com.github.dynamo.webapps.googleimages.GoogleImages;
import com.github.mozvip.hclient.core.WebResource;
import com.github.mozvip.theaudiodb.model.AudioDbResponse;

public class FindMusicAlbumImageExecutor extends FindDownloadableImageExecutor<MusicAlbum> {

	public FindMusicAlbumImageExecutor( FindMusicAlbumImageTask task ) {
		super(task);
	}
	
	@Override
	public void onImageFound( Path localImage ) {
		BackLogProcessor.getInstance().schedule( new SetAlbumImageTask( task.getDownloadable(), localImage), false);
	}

	@Override
	public boolean downloadImageTo(Path localImage) {
		
		MusicAlbum album = task.getDownloadable();
		
		if (album.isDownloaded()) {
			Path folderImage = album.getFolder().resolve("folder.jpg");
			if (Files.exists( folderImage )) {
				try {
					return DownloadableManager.downloadImage( album, folderImage );
				} catch (IOException e) {
					ErrorManager.getInstance().reportThrowable( e );
				}		
			}
		}
		
		if (album.getTadbAlbumId() != null && album.getTadbAlbumId() > 0) {
			
			try {
				AudioDbResponse response = TheAudioDb.getInstance().getAlbum( album.getTadbAlbumId() );
				return DownloadableManager.downloadImage( album, response.getAlbum().get(0).getStrAlbumThumb(), null );
			} catch (IOException e) {
				ErrorManager.getInstance().reportThrowable( e );
			}
			
		}

		// google images search
		String albumName = MusicManager.getAlbumName(  album.getName() );
		
		String[] searchStrings = new String[] { String.format("%s \"%s\"", album.getArtistName(), albumName), String.format("%s+%s+album", album.getArtistName(), albumName) };
		for (String string : searchStrings) {
			WebResource googleResult = GoogleImages.findImage( string, 1.0f );
			if (googleResult != null) {
				try {
					return DownloadableManager.downloadImage(localImage, googleResult.getUrl(), googleResult.getReferer());
				} catch (IOException e) {
					ErrorManager.getInstance().reportThrowable( e );
				}
			}
		}
		
		return false;
	}

}

@@ -127,24 +127,72 @@
 le.Path;%0D%0A%0D%0A
+import org.apache.commons.lang3.StringUtils;%0D%0A%0D%0A
 import com.g
@@ -1718,24 +1718,151 @@
 bumId() );%0D%0A
+%09%09%09%09String strAlbumThumb = response.getAlbum().get(0).getStrAlbumThumb();%0D%0A%09%09%09%09if (StringUtils.isNotBlank( strAlbumThumb)) %7B%0D%0A%09
 %09%09%09%09return D
@@ -1906,39 +1906,9 @@
 um, 
-response.getAlbum().get(0).getS
+s
 trAl
@@ -1915,18 +1915,16 @@
 bumThumb
-()
 , null )
@@ -1922,24 +1922,31 @@
 b, null );%0D%0A
+%09%09%09%09%7D%0D%0A
 %09%09%09%7D catch (

Fix Music album image downloading