/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.test.performance.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class TestCaseNode
{

	String name = null;

	private List reportdesigns = new ArrayList( );

	// thread count
	int threads = 0;

	// e.g 1,2,1-N, All
	String report = "All"; //$NON-NLS-1$

	// report locale
	String locale = "en_US";//$NON-NLS-1$

	// report encoding
	String encoding = "UTF-8";//$NON-NLS-1$

	// "pdf" or "html"
	String format = null;

	String[] tasks = null;

	CaseLogger logger = new CaseLogger( );

	public String toString( )
	{
		StringBuffer sb = new StringBuffer( );
		sb.append( "name: " + name );
		sb.append( '\n' );
		sb.append( "report: " + report );
		sb.append( '\n' );
		sb.append( "report files: " + reportdesigns );
		sb.append( '\n' );
		sb.append( "encoding: " + encoding );
		sb.append( '\n' );
		sb.append( "locale: " + locale );
		sb.append( '\n' );
		sb.append( "format: " + format );
		sb.append( '\n' );
		sb.append( "tasks: " + tasks );
		sb.append( '\n' );

		return sb.toString( );
	}

	/**
	 * A list of ReportDesignNode
	 * 
	 * @return
	 */

	public List getReports( )
	{
		return this.reportdesigns;
	}

	public CaseLogger getCaseLogger( )
	{
		return this.logger;
	}

	public void build( )
	{

		if ( Pattern.matches( "[\\d]+[,\\d]*", report ) ) //$NON-NLS-1$
		{
			// "1,2,3"

			String[] ids = report.split( "," ); //$NON-NLS-1$
			for ( int i = 0; i < ids.length; i++ )
			{
				String id = ids[i];

				ReportDesignNode node = Configuration.getInstance( )
						.getReportDesign( id );
				if ( node == null )
				{
					System.err.println( "Testcase:"
							+ this.name + " Can not find report id=\"" + id
							+ "\"" );
				}
				else
				{
					this.reportdesigns.add( node );
				}
			}
		}
		else if ( Pattern.matches( "[A|a]ll", report ) ) //$NON-NLS-1$
		{
			this.reportdesigns.add( Configuration.getInstance( )
					.getReportDesigns( )
					.values( ) );
		}
		else if ( Pattern.matches( "[\\d]+-[\\d]+", report ) ) //$NON-NLS-1$
		{
			String ids[] = report.split( "-" );
			int from = Integer.parseInt( ids[0] );
			int to = Integer.parseInt( ids[1] );

			if ( from > to )
			{
				System.err.println( "Testcase:"
						+ this.name + "error range of report + \"" + report
						+ "\", from:" + from + " lager than to:" + to );
				System.out.println( "Use All instead" );
				this.reportdesigns.add( Configuration.getInstance( )
						.getReportDesigns( )
						.values( ) );
			}

			int seed = from;
			while ( seed <= to )
			{
				ReportDesignNode node = Configuration.getInstance( )
						.getReportDesign( String.valueOf( seed ) );

				if ( node == null )
				{
					System.err.println( "Testcase:"
							+ this.name + "Can not find report id=\"" + seed
							+ "\"" );
				}
				else
				{
					this.reportdesigns.add( node );
				}

				++seed;
			}
		}
		else
		{
			assert false;
		}

	}

	public String getEncoding( )
	{
		return encoding;
	}

	public String getFormat( )
	{
		return format;
	}

	public String getLocale( )
	{
		return locale;
	}

	public void setName( String name )
	{
		this.name = name;
	}

	public String getName( )
	{
		return name;
	}

	public int getThreads( )
	{
		return threads;
	}

	public String[] getTasks( )
	{
		return tasks;
	}

	public void setEncoding( String encoding )
	{
		this.encoding = encoding;
	}

	public void setFormat( String format )
	{
		this.format = format;
	}

	public void setLocale( String locale )
	{
		this.locale = locale;
	}

	public void setReport( String report )
	{
		this.report = report;
	}

	public void setThreads( int threads )
	{
		this.threads = threads;
	}

	public void setTasks( String[] tasks )
	{
		this.tasks = tasks;
	}

	public String getReport( )
	{
		return report;
	}

}