/**
 * This script is run everytime an album is generated.
 */
import se.datadosen.jalbum.JAFilter;
import se.datadosen.jalbum.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

engine.processBeanShellFile(new File(programDirectory, "includes/printing.bsh"));

//------------------------- Display message -------------------------
public void displayMessage(String msg)
{
    if( window!=null )
        window.messageBox(msg);
    else
        System.out.println(msg);
}

//------------------------- Check the version -------------------------
public void checkSkinVersion()
{
	String currentVersion = generator.substring( generator.indexOf(' ') + 1 );
	String minimumVersion = "5.0";

	if (currentVersion.compareTo(minimumVersion) < 0)
	    displayMessage( "This skin actually requires JAlbum " + minimumVersion + ". Please upgrade to the latest version at http://www.jalbum.net" );
}

//----------- Figure out if we can integrate with online printing service -----------
public void checkOnlinePrintSupport()
{
	showPrintingWarnings();
}

//----------- Installing the filters -----------

// AlbumBean.addFilter()'s second parameter guide:
//   1. Closeups and thumbnails, prescale
//   2. Closeups and thumbnails
//   3. Closeups only
//   4. Thumbnails only, prescale
//   5. Thumbnails only

public void installFilters()
{
	if( keepFiltersDefinedOutsideFMTabs==void || !keepFiltersDefinedOutsideFMTabs )
		engine.removeAllFilters();

	if( (includeZoomFilterOnThumbnails!=void && includeZoomFilterOnThumbnails) || (includeZoomFilterOnSlides!=void && includeZoomFilterOnSlides) )
	{
		if( includeZoomFilterOnThumbnails )
		{
			ZoomFilter filter = new ZoomFilter();

			if( !(zoomFilterPercentOnThumbnails instanceof String) )
				filter.setZoomPercent(zoomFilterPercentOnThumbnails);
			engine.addFilter( filter, prescaleZoomFilterOnThumbnails ? 4 : 5 );
		}

		if( includeZoomFilterOnSlides )
		{
			ZoomFilter filter = new ZoomFilter();

			if( !(zoomFilterPercentOnSlides instanceof String) )
				filter.setZoomPercent(zoomFilterPercentOnSlides);
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeFixedShapeFilterOnThumbnails!=void && includeFixedShapeFilterOnThumbnails) || (includeFixedShapeFilterOnSlides!=void && includeFixedShapeFilterOnSlides) )
	{
		if( includeFixedShapeFilterOnThumbnails )
		{
			FixedShapeFilter filter = new FixedShapeFilter();
			engine.addFilter( filter, prescaleFixedShapeFilterOnThumbnails ? 4 : 5 );
		}

		if( includeFixedShapeFilterOnSlides )
		{
			FixedShapeFilter filter = new FixedShapeFilter();
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeGrayScaleFilterOnThumbnails!=void && includeGrayScaleFilterOnThumbnails) || (includeGrayScaleFilterOnSlides!=void && includeGrayScaleFilterOnSlides) )
	{
		if( includeGrayScaleFilterOnThumbnails )
		{
			GrayscaleFilter filter = new GrayscaleFilter();
			engine.addFilter( filter, 5 );
		}

		if( includeGrayScaleFilterOnSlides )
		{
			GrayscaleFilter filter = new GrayscaleFilter();
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeLogoFilterOnThumbnails!=void && includeLogoFilterOnThumbnails) || (includeLogoFilterOnSlides!=void && includeLogoFilterOnSlides) )
	{
		if( includeLogoFilterOnThumbnails )
		{
			LogoFilter filter = new LogoFilter();

			try
			{
				new URL(logoFilterSourceOnThumbnails);
			}
			catch( MalformedURLException e )
			{
				logoFilterSourceOnThumbnails = new File(logoFilterSourceOnThumbnails).toURL().toString();
			}

			filter.setSrc(logoFilterSourceOnThumbnails);
			if( !(logoFilterMarginOnThumbnails instanceof String) )
				filter.setMargin(logoFilterMarginOnThumbnails);
			if( !(logoFilterHorizontalOffsetOnThumbnails instanceof String) )
				filter.setOffset(logoFilterHorizontalOffsetOnThumbnails);
			if( !(logoFilterVerticalOffsetOnThumbnails instanceof String) )
				filter.setVoffset(logoFilterVerticalOffsetOnThumbnails);
			filter.setAlign(logoFilterHorizontalAlignOnThumbnails);
			filter.setValign(logoFilterVerticalAlignOnThumbnails);
			engine.addFilter( filter, prescaleLogoFilterOnThumbnails ? 4 : 5 );
		}

		if( includeLogoFilterOnSlides )
		{
			LogoFilter filter = new LogoFilter();

			try
			{
				new URL(logoFilterSourceOnSlides);
			}
			catch( MalformedURLException e )
			{
				logoFilterSourceOnSlides = new File(logoFilterSourceOnSlides).toURL().toString();
			}

			filter.setSrc(logoFilterSourceOnSlides);
			if( !(logoFilterMarginOnSlides instanceof String) )
				filter.setMargin(logoFilterMarginOnSlides);
			if( !(logoFilterHorizontalOffsetOnSlides instanceof String) )
				filter.setOffset(logoFilterHorizontalOffsetOnSlides);
			if( !(logoFilterVerticalOffsetOnSlides instanceof String) )
				filter.setVoffset(logoFilterVerticalOffsetOnSlides);
			filter.setAlign(logoFilterHorizontalAlignOnSlides);
			filter.setValign(logoFilterVerticalAlignOnSlides);
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeTextFilterOnThumbnails!=void && includeTextFilterOnThumbnails) || (includeTextFilterOnSlides!=void && includeTextFilterOnSlides) )
	{
		if( includeTextFilterOnThumbnails )
		{
			TextFilter filter = new TextFilter();

			filter.setText(textFilterTextOnThumbnails);
			if( !(textFilterFontSizeOnThumbnails instanceof String) )
				filter.setSize(textFilterFontSizeOnThumbnails);
			filter.setFace(textFilterFontFaceOnThumbnails);
			filter.setStyle(textFilterFontStyleOnThumbnails);
			if( !textFilterColorOnThumbnails.trim().equals("") )
				filter.setColor(textFilterColorOnThumbnails);
			if( !(textFilterMarginOnThumbnails instanceof String) )
				filter.setMargin(textFilterMarginOnThumbnails);
			filter.setAlign(textFilterHorizontalAlignOnThumbnails);
			filter.setValign(textFilterVerticalAlignOnThumbnails);
			filter.setAntialias(textFilterAntiAliasOnThumbnails);
			if( !(textFilterHorizontalOffsetOnThumbnails instanceof String) )
				filter.setOffset(textFilterHorizontalOffsetOnThumbnails);
			if( !(textFilterVerticalOffsetOnThumbnails instanceof String) )
				filter.setVoffset(textFilterVerticalOffsetOnThumbnails);
			engine.addFilter( filter, prescaleTextFilterOnThumbnails ? 4 : 5 );
		}

		if( includeTextFilterOnSlides )
		{
			TextFilter filter = new TextFilter();

			filter.setText(textFilterTextOnSlides);
			if( !(textFilterFontSizeOnSlides instanceof String) )
				filter.setSize(textFilterFontSizeOnSlides);
			filter.setFace(textFilterFontFaceOnSlides);
			filter.setStyle(textFilterFontStyleOnSlides);
			if( !textFilterColorOnSlides.trim().equals("") )
				filter.setColor(textFilterColorOnSlides);
			if( !(textFilterMarginOnSlides instanceof String) )
				filter.setMargin(textFilterMarginOnSlides);
			filter.setAlign(textFilterHorizontalAlignOnSlides);
			filter.setValign(textFilterVerticalAlignOnSlides);
			filter.setAntialias(textFilterAntiAliasOnSlides);
			if( !(textFilterHorizontalOffsetOnSlides instanceof String) )
				filter.setOffset(textFilterHorizontalOffsetOnSlides);
			if( !(textFilterVerticalOffsetOnSlides instanceof String) )
				filter.setVoffset(textFilterVerticalOffsetOnSlides);
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeShadowTextFilterOnThumbnails!=void && includeShadowTextFilterOnThumbnails) || (includeShadowTextFilterOnSlides!=void && includeShadowTextFilterOnSlides) )
	{
		if( includeShadowTextFilterOnThumbnails )
		{
			ShadowTextFilter filter = new ShadowTextFilter();

			filter.setText(shadowTextFilterTextOnThumbnails);
			if( !(shadowTextFilterFontSizeOnThumbnails instanceof String) )
				filter.setSize(shadowTextFilterFontSizeOnThumbnails);
			filter.setFace(shadowTextFilterFontFaceOnThumbnails);
			filter.setStyle(shadowTextFilterFontStyleOnThumbnails);
			if( !shadowTextFilterTextColorOnThumbnails.trim().equals("") )
				filter.setColor(shadowTextFilterTextColorOnThumbnails);
			if( !(shadowTextFilterMarginOnThumbnails instanceof String) )
				filter.setMargin(shadowTextFilterMarginOnThumbnails);
			filter.setAlign(shadowTextFilterHorizontalAlignOnThumbnails);
			filter.setValign(shadowTextFilterVerticalAlignOnThumbnails);
			filter.setAntialias(shadowTextFilterAntiAliasOnThumbnails);
			if( !(shadowTextFilterHorizontalOffsetOnThumbnails instanceof String) )
				filter.setOffset(shadowTextFilterHorizontalOffsetOnThumbnails);
			if( !(shadowTextFilterVerticalOffsetOnThumbnails instanceof String) )
				filter.setVoffset(shadowTextFilterVerticalOffsetOnThumbnails);
			if( !shadowTextFilterShadowColorOnThumbnails.trim().equals("") )
				filter.setShadowColor(shadowTextFilterShadowColorOnThumbnails);
			if( !(shadowTextFilterShadowDistanceOnThumbnails instanceof String) )
				filter.setShadowDistance(shadowTextFilterShadowDistanceOnThumbnails);
			engine.addFilter( filter, prescaleShadowTextFilterOnThumbnails ? 4 : 5 );
		}

		if( includeShadowTextFilterOnSlides )
		{
			ShadowTextFilter filter = new ShadowTextFilter();

			filter.setText(shadowTextFilterTextOnSlides);
			if( !(shadowTextFilterFontSizeOnSlides instanceof String) )
				filter.setSize(shadowTextFilterFontSizeOnSlides);
			filter.setFace(shadowTextFilterFontFaceOnSlides);
			filter.setStyle(shadowTextFilterFontStyleOnSlides);
			if( !shadowTextFilterTextColorOnSlides.trim().equals("") )
				filter.setColor(shadowTextFilterTextColorOnSlides);
			if( !(shadowTextFilterMarginOnSlides instanceof String) )
				filter.setMargin(shadowTextFilterMarginOnSlides);
			filter.setAlign(shadowTextFilterHorizontalAlignOnSlides);
			filter.setValign(shadowTextFilterVerticalAlignOnSlides);
			filter.setAntialias(shadowTextFilterAntiAliasOnSlides);
			if( !(shadowTextFilterHorizontalOffsetOnSlides instanceof String) )
				filter.setOffset(shadowTextFilterHorizontalOffsetOnSlides);
			if( !(shadowTextFilterVerticalOffsetOnSlides instanceof String) )
				filter.setVoffset(shadowTextFilterVerticalOffsetOnSlides);
			if( !shadowTextFilterShadowColorOnSlides.trim().equals("") )
				filter.setShadowColor(shadowTextFilterShadowColorOnSlides);
			if( !(shadowTextFilterShadowDistanceOnSlides instanceof String) )
				filter.setShadowDistance(shadowTextFilterShadowDistanceOnSlides);
			engine.addFilter( filter, 3 );
		}
	}

	if( (includeWatermarkTextFilterOnThumbnails!=void && includeWatermarkTextFilterOnThumbnails) || (includeWatermarkTextFilterOnSlides!=void && includeWatermarkTextFilterOnSlides) )
	{
		if( includeWatermarkTextFilterOnThumbnails )
		{
			WatermarkFilter filter = new WatermarkFilter();

			filter.setText(watermarkTextFilterTextOnThumbnails);
			if( !(watermarkTextFilterFontSizeOnThumbnails instanceof String) )
				filter.setSize(watermarkTextFilterFontSizeOnThumbnails);
			filter.setFace(watermarkTextFilterFontFaceOnThumbnails);
			filter.setStyle(watermarkTextFilterFontStyleOnThumbnails);
			if( !watermarkTextFilterColorOnThumbnails.trim().equals("") )
				filter.setColor(watermarkTextFilterColorOnThumbnails);
			if( !(watermarkTextFilterMarginOnThumbnails instanceof String) )
				filter.setMargin(watermarkTextFilterMarginOnThumbnails);
			filter.setAlign(watermarkTextFilterHorizontalAlignOnThumbnails);
			filter.setValign(watermarkTextFilterVerticalAlignOnThumbnails);
			filter.setAntialias(watermarkTextFilterAntiAliasOnThumbnails);
			if( !(watermarkTextFilterHorizontalOffsetOnThumbnails instanceof String) )
				filter.setOffset(watermarkTextFilterHorizontalOffsetOnThumbnails);
			if( !(watermarkTextFilterVerticalOffsetOnThumbnails instanceof String) )
				filter.setVoffset(watermarkTextFilterVerticalOffsetOnThumbnails);
			if( !(watermarkTextFilterStrengthPercentOnThumbnails instanceof String) )
				filter.setStrengthPercent(watermarkTextFilterStrengthPercentOnThumbnails);
			engine.addFilter( filter, prescaleWatermarkTextFilterOnThumbnails ? 4 : 5 );
		}

		if( includeWatermarkTextFilterOnSlides )
		{
			WatermarkFilter filter = new WatermarkFilter();

			filter.setText(watermarkTextFilterTextOnSlides);
			if( !(watermarkTextFilterFontSizeOnSlides instanceof String) )
				filter.setSize(watermarkTextFilterFontSizeOnSlides);
			filter.setFace(watermarkTextFilterFontFaceOnSlides);
			filter.setStyle(watermarkTextFilterFontStyleOnSlides);
			if( !watermarkTextFilterColorOnSlides.trim().equals("") )
				filter.setColor(watermarkTextFilterColorOnSlides);
			if( !(watermarkTextFilterMarginOnSlides instanceof String) )
				filter.setMargin(watermarkTextFilterMarginOnSlides);
			filter.setAlign(watermarkTextFilterHorizontalAlignOnSlides);
			filter.setValign(watermarkTextFilterVerticalAlignOnSlides);
			filter.setAntialias(watermarkTextFilterAntiAliasOnSlides);
			if( !(watermarkTextFilterHorizontalOffsetOnSlides instanceof String) )
				filter.setOffset(watermarkTextFilterHorizontalOffsetOnSlides);
			if( !(watermarkTextFilterVerticalOffsetOnSlides instanceof String) )
				filter.setVoffset(watermarkTextFilterVerticalOffsetOnSlides);
			if( !(watermarkTextFilterStrengthPercentOnSlides instanceof String) )
				filter.setStrengthPercent(watermarkTextFilterStrengthPercentOnSlides);
			engine.addFilter( filter, 3 );
		}
	}
}

//------------------------- Cleanup tasks -------------------------
public void setupCleanupTasks()
{
    if( window==null )
        return;

	window.addJAlbumListener(new JAlbumAdapter() {
		public void albumCreationFinished(JAlbumEvent e) {
			removeExtraResources("CacheKey_UsedBorders", "/res/borders", true);
			removeExtraResources("CacheKey_UsedIconSets", "/res/actions", true);
			removeExtraResources("CacheKey_UsedStyles", "/res/styles", true);
			removeExtraResources("CacheKey_UsedFullStyles", "/res/styles", false);

			window.removeJAlbumListener(this);
		}

		public void	skinChanged(JAlbumEvent e) {
			window.removeJAlbumListener(this);
		}
	});
}

public void removeExtraResources(String usedResourcesKey, String resourceDirectory, boolean isDirectory)
{
	if( !album.containsKey(usedResourcesKey) )
		return;

	File[] resources = new File(rootOutputDirectory, resourceDirectory).listFiles();
	List usedResources = (List) album.get(usedResourcesKey);

	for( int i=0; i<resources.length; i++ )
	{
		if( isDirectory==resources[i].isDirectory() && !usedResources.contains(resources[i].getName()) )
			deleteDirectory( resources[i] );
	}
}

public void deleteDirectory(File dir)
{
	if( !dir.isDirectory() )
	{
		dir.delete();
		return;
	}

	File[] children = dir.listFiles();

	for( int i=0; i<children.length; i++ )
		deleteDirectory(children[i]);

	dir.delete();
}


//------------------------- Main -------------------------
checkSkinVersion();
checkOnlinePrintSupport();
installFilters();
setupCleanupTasks();
