/*******************************************************************************
 * Copyright (c) 2005,2008 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.report.designer.data.ui.dataset;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.birt.report.designer.data.ui.util.Utility;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.JointDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSetParameterHandle;
import org.eclipse.birt.report.model.api.ReportElementHandle;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.DataSetParameter;
import org.eclipse.birt.report.model.api.metadata.IChoice;

/**
 * Utility class for datasetParameterPage.
 * @author Administrator
 *
 */
final class ParameterPageUtil
{

	static IChoice[] dataTypes = DEUtil.getMetaDataDictionary( )
			.getStructure( DataSetParameter.STRUCT_NAME )
			.getMember( DataSetParameter.DATA_TYPE_MEMBER )
			.getAllowedChoices( )
			.getChoices( );

	static String[] directions = new String[]{
			Messages.getString( "label.input" ), //$NON-NLS-1$
			Messages.getString( "label.output" ), //$NON-NLS-1$
			Messages.getString( "label.inputOutput" ) //$NON-NLS-1$
	};

	static String[] cellLabels = new String[]{
			Messages.getString( "dataset.editor.title.name" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.dataType" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.direction" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.defaultValue" ), //$NON-NLS-1$
	};

	static String[] dialogLabels = new String[]{
			Messages.getString( "dataset.editor.inputDialog.name" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.dataType" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.direction" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.defaultValue" ), //$NON-NLS-1$
			Messages.getString( "DataSetParameterPage.cell.linkToSalarParameter" ) //$NON-NLS-1$
	};
	static String[] odaCellLabels = new String[]{
			Messages.getString( "dataset.editor.title.name" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.nativeName" ),//$NON-NLS-1$
			Messages.getString( "dataset.editor.title.dataType" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.direction" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.title.defaultValue" ), //$NON-NLS-1$
			Messages.getString( "DataSetParameterPage.editor.title.linkToSalarParameter" ) //$NON-NLS-1$
	};

	static String[] odaDialogLabels = new String[]{
			Messages.getString( "dataset.editor.inputDialog.name" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.nativeName" ),//$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.dataType" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.direction" ), //$NON-NLS-1$
			Messages.getString( "dataset.editor.inputDialog.defaultValue" ), //$NON-NLS-1$
			Messages.getString( "DataSetParameterPage.cell.linkToSalarParameter" ) //$NON-NLS-1$
	};
	
	private static String UNLINKED_REPORT_PARAM = Messages.getString( "DataSetParametersPage.reportParam.None" );//$NON-NLS-1$

	static boolean isJointDataSetHandle( DataSetHandle handle )
	{

		if ( handle instanceof JointDataSetHandle )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	static boolean isOdaDataSetHandle( DataSetHandle handle )
	{
		if ( handle instanceof OdaDataSetHandle )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	static String getTypeDisplayName( String typeName )
	{
		String displayName = dataTypes[0].getDisplayName( );
		for ( int i = 0; i < dataTypes.length; i++ )
		{
			if ( dataTypes[i].getName( ).equals( typeName ) )
				return dataTypes[i].getDisplayName( );
		}
		return displayName;
	}

	static String getTypeName( String typeDisplayName )
	{
		String name = dataTypes[0].getName( );
		for ( int i = 0; i < dataTypes.length; i++ )
		{
			if ( dataTypes[i].getDisplayName( ).equals( typeDisplayName ) )
				return dataTypes[i].getName( );
		}
		return name;
	}
	

	static String[] getDataTypeDisplayNames( )
	{
		String[] dataTypeDisplayNames = new String[dataTypes.length];
		for ( int i = 0; i < dataTypes.length; i++ )
		{
			dataTypeDisplayNames[i] = dataTypes[i].getDisplayName( );
		}
		Arrays.sort( dataTypeDisplayNames );
		return dataTypeDisplayNames;
	}
	
	/**
	 * Gets an array of all the available linked report parameters.
	 * 
	 * @param structureHandle
	 * @return
	 */
	static String[] getLinkedReportParameterNames(
			OdaDataSetParameterHandle structureHandle )
	{
		List parameterList = Utility.getAllParameters( );
		List nameList = new ArrayList( );
		nameList.add( UNLINKED_REPORT_PARAM );

		ScalarParameterHandle scalarParam = Utility.getScalarParameter( structureHandle.getParamName( ) );

		for ( int i = 0; i < parameterList.size( ); i++ )
		{
			ReportElementHandle handle = (ReportElementHandle) parameterList.get( i );
			if ( handle instanceof ScalarParameterHandle
					&& !( DesignChoiceConstants.SCALAR_PARAM_TYPE_MULTI_VALUE.equals( ( (ScalarParameterHandle) handle ).getParamType( ) ) ) )
			{
				nameList.add( handle.getQualifiedName( ) );
			}
		}
		String[] paramName = new String[nameList.size( )];
		for ( int i = 0; i < nameList.size( ); i++ )
		{
			paramName[i] = nameList.get( i ).toString( );
		}

		return paramName;
	}

}
