
package org.eclipse.birt.test.performance;

import java.util.HashMap;
import java.util.List;

import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IRenderTask;
import org.eclipse.birt.report.engine.api.IReportDocument;
import org.eclipse.birt.report.engine.api.RenderOptionBase;
import org.eclipse.birt.test.performance.parser.ReportDesignNode;
import org.eclipse.birt.test.performance.parser.Utility;

/**
 * RenderThread - represent a thread to perform engine Render task
 */
public class RenderThread extends BaseTaskThread
{

	// 0 for render all, and 1 for render a specific page.
	private int pageOption;

	/**
	 * Constructor
	 * 
	 * @param threadName -
	 *            thread identifier, use to identify this thread
	 * @param semaphore -
	 *            semaphore instance, use to call back thread monitor
	 * @param pageOption -
	 *            0 for render all, 1 for render pageN.
	 */
	public RenderThread( String threadName, int id, ExecutionContext context,
			Semaphore semaphore, int pageOption )
	{
		super( threadName, id, context, semaphore );
		this.pageOption = pageOption;
	}

	public void run( )
	{
		String basedir = Constants.BASE_PATH;

		String locale = this.testcase.getLocale( );
		String format = this.testcase.getFormat( );

		try
		{
			long beginTime;
			long endTime;

			List inputs = this.testcase.getReports( );
			for ( int i = 0; i < inputs.size( ); i++ )
			{
				// execution, task begin
				ReportDesignNode reportdesign = (ReportDesignNode) inputs.get( i );

				String reportfile = reportdesign.getName( );
				String docType = reportdesign.getDocType( );
				int repeat = reportdesign.getRepeat( );

				System.out.println( "Begin, Thread[name=\"" + this.getName( ) + "\", id=\"" + this.id + "\"] report[" + reportfile + "]" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

				beginTime = System.currentTimeMillis( );

				for ( int counter = 0; counter < repeat; counter++ )
				{
					String inputFile = null;
					if ( docType.equalsIgnoreCase( "file" ) )
					{
						inputFile = basedir
							+ Constants.RUN_PATH
							+ Utility.getReportName( reportfile ) + "0" //id = 0, 1st doc
							+ Constants.DOC_EXT;
					}
					else // folder type doc
					{
						inputFile = basedir
							+ Constants.RUN_PATH
							+ Utility.getReportName( reportfile ) + "0" // id = 0, 1st doc
							+ "/";
					}
					IReportDocument document = engine.openReportDocument( inputFile );
					IRenderTask task = engine.createRenderTask( document );
					task.setLocale( Utility.getLocale( locale ) );

					IRenderOption options = null;
					
					if ( "pdf".equalsIgnoreCase( format ) ) //$NON-NLS-1$
					{
						options = new RenderOptionBase( );
						String outputFile = ""; //$NON-NLS-1$

						if ( this.pageOption == 0 ) // render all
						{
							outputFile = basedir
									+ Constants.RENDER_PATH
									+ Utility.getReportName( reportfile )
									+ "(all)" //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
									+ id + Constants.PDF_EXT;
						}
						else
						// page N
						{
							outputFile = basedir
									+ Constants.RENDER_PATH
									+ Utility.getReportName( reportfile )
									+ "(" + reportdesign.getPageN( ) + ")" //$NON-NLS-1$ //$NON-NLS-2$
									+ id + Constants.PDF_EXT;
						}

						options.setOutputFileName( outputFile );
					}
					else if ( "html".equalsIgnoreCase( format ) ) //$NON-NLS-1$
					{
						options = new HTMLRenderOption( );
						String outputFile = ""; //$NON-NLS-1$
						if ( pageOption == 0 ) // render all
						{
							outputFile = basedir
									+ Constants.RENDER_PATH
									+ Utility.getReportName( reportfile )
									+ "(all)" //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
									+ id + Constants.HTML_EXT;
						}
						else
						// page N
						{
							outputFile = basedir
									+ Constants.RENDER_PATH
									+ Utility.getReportName( reportfile )
									+ "(" + reportdesign.getPageN( ) + ")" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
									+ id + Constants.HTML_EXT;
						}
						options.setOutputFileName( outputFile );

						HTMLRenderContext renderContext = new HTMLRenderContext( );
						renderContext.setImageDirectory( "image" ); //$NON-NLS-1$
						HashMap appContext = new HashMap( );
						appContext.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
								renderContext );
						task.setAppContext( appContext );
					}
					else
					{
						System.err.println( "Format: " + format + " not supported." ); //$NON-NLS-1$//$NON-NLS-2$
						return;
					}

					options.setOutputFormat( format );
					options.getOutputSetting( )
							.put( HTMLRenderOption.URL_ENCODING,
									testcase.getEncoding( ) );

					task.setRenderOption( options );

					if ( pageOption == 0 )
					{
						// render all
						task.setPageRange( "all" ); //$NON-NLS-1$
						task.render( );
					}
					else
					{
						// render a specific page.
						task.setPageNumber( reportdesign.getPageN( ) );
						task.render( );
					}
					task.close( );
				}

				// execution, task end
				endTime = System.currentTimeMillis( );

				// record single average task time
				long timespan = ( endTime - beginTime ) / repeat;

				this.testcase.getCaseLogger( ).logResult( this.getName( ),
						reportdesign,
						this.id,
						timespan );

				System.out.println( "End, Thread[name=\"" + this.getName( ) + "\", id=\"" + this.id + "\"] report[" + reportfile + "] timespan[" + timespan + "]" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}

			this.semaphore.release( );
		}
		catch ( Exception e )
		{
			e.printStackTrace( );
			System.exit( -1 );
		}
	}

}