/*******************************************************************************
 * 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.report.designer.internal.ui.views.attributes.widget;

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

import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.AttributeConstant;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.AttributeValueConstant;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.ColumnHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ListingHandle;
import org.eclipse.birt.report.model.api.MasterPageHandle;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.SimpleMasterPageHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TextDataHandle;
import org.eclipse.birt.report.model.api.TextItemHandle;
import org.eclipse.swt.SWT;

/**
 * PropertyDescriptorFactory provides static method to get PropertyDescriptor
 * instance.
 */
public class PropertyDescriptorFactory
{

	/**
	 * Property list,these properties all need a TextPropertyDescriptor.
	 */
	private static List textConstants = Arrays.asList( new String[]{
			AttributeConstant.NAME,
			ModuleHandle.AUTHOR_PROP,
			ModuleHandle.CREATED_BY_PROP,
			ModuleHandle.TITLE_PROP,
			ModuleHandle.EVENT_HANDLER_CLASS_PROP,
			ReportDesignHandle.DISPLAY_NAME_PROP,
			ReportDesignHandle.ICON_FILE_PROP,
			CellHandle.COL_SPAN_PROP,
			CellHandle.ROW_SPAN_PROP,
			ListingHandle.PAGE_BREAK_INTERVAL_PROP,
			DataSetHandle.DATA_SOURCE_PROP,
			ScalarParameterHandle.DATA_TYPE_PROP,
			ScalarParameterHandle.CONTROL_TYPE_PROP,
	} );

	/**
	 * Property list,these properties all need a MarignPropertyDescriptor.
	 */
	private static List marginConstants = Arrays.asList( new String[]{
			StyleHandle.MARGIN_TOP_PROP,
			StyleHandle.MARGIN_LEFT_PROP,
			StyleHandle.MARGIN_BOTTOM_PROP,
			StyleHandle.MARGIN_RIGHT_PROP
	} );
	/**
	 * Property list,these properties all need a TextPropertyDescriptor with
	 * SWT.MULTI and SWT.WRAP style.
	 */
	private static List multiTextConstants = Arrays.asList( new String[]{
			ModuleHandle.DESCRIPTION_PROP,
			DesignElementHandle.COMMENTS_PROP,
			ImageHandle.ALT_TEXT_PROP
	} );

	/**
	 * Property list,these properties all need a UnitPropertyDescriptor.
	 */
	private static List unitConstants = Arrays.asList( new String[]{
			AttributeConstant.HEIGHT,
			AttributeConstant.WIDTH,
			AttributeConstant.PADDING_TOP,
			AttributeConstant.PADDING_BOTTOM,
			AttributeConstant.PADDING_LEFT,
			AttributeConstant.PADDING_RIGHT,
			ColumnHandle.WIDTH_PROP,
			RowHandle.HEIGHT_PROP,
			MasterPageHandle.WIDTH_PROP,
			MasterPageHandle.HEIGHT_PROP,
			SimpleMasterPageHandle.HEADER_HEIGHT_PROP,
			SimpleMasterPageHandle.FOOTER_HEIGHT_PROP
	} );

	/**
	 * Property list,these properties all need a ComboPropertyDescriptor.
	 */
	private static List comboConstants = Arrays.asList( new String[]{
			AttributeConstant.FONT_FAMILY,
			AttributeConstant.FONT_SIZE,
			AttributeConstant.TEXT_FORMAT,
			StyleHandle.VERTICAL_ALIGN_PROP,
			AttributeConstant.HORIZONTAL_ALIGN,
			MasterPageHandle.ORIENTATION_PROP,
			MasterPageHandle.TYPE_PROP,
			StyleHandle.PAGE_BREAK_BEFORE_PROP,
			StyleHandle.PAGE_BREAK_AFTER_PROP,
			StyleHandle.PAGE_BREAK_INSIDE_PROP,
			ModuleHandle.UNITS_PROP,
			StyleHandle.DATE_TIME_FORMAT_PROP,
			StyleHandle.NUMBER_FORMAT_PROP,
			StyleHandle.STRING_FORMAT_PROP,
			CellHandle.DROP_PROP,
			ReportDesignHandle.LAYOUT_PREFERENCE_PROP,
	} );

	/**
	 * Property list,these properties all need a SpinnerPropertyDescriptor.
	 */
	private static List spinnerConstants = Arrays.asList( new String[]{
			MasterPageHandle.LEFT_MARGIN_PROP,
			MasterPageHandle.RIGHT_MARGIN_PROP,
			MasterPageHandle.TOP_MARGIN_PROP,
			MasterPageHandle.BOTTOM_MARGIN_PROP,
	} );

	/**
	 * Property list,these properties all need a CheckPropertyDescriptor.
	 */
	private static List checkConstants = Arrays.asList( new String[]{
			SimpleMasterPageHandle.SHOW_HEADER_ON_FIRST_PROP,
			SimpleMasterPageHandle.SHOW_FOOTER_ON_LAST_PROP,
			SimpleMasterPageHandle.FLOATING_FOOTER,
			StyleHandle.CAN_SHRINK_PROP,
			ColumnHandle.SUPPRESS_DUPLICATES_PROP,
	} );

	/**
	 * Property list,these properties all need a ColorPropertyDescriptor.
	 */
	private static List colorConstants = Arrays.asList( new String[]{
			AttributeConstant.BACKGROUND_COLOR, AttributeConstant.FONT_COLOR,
	} );

	/**
	 * Property list,these properties all need a SimpleComboPropertyDescriptor.
	 */
	private static List simpleComboConstants = Arrays.asList( new String[]{
			ReportItemHandle.DATA_SET_PROP, StyleHandle.MASTER_PAGE_PROP,
	} );

	/**
	 * Property list,these properties all need a FontSizePropertyDescriptor.
	 */
	private static List fontSizeConstants = Arrays.asList( new String[]{
		AttributeConstant.FONT_SIZE
	} );

	/**
	 * Gets PropertyDescriptor instance correspond to the given property.
	 * 
	 * @param property
	 *            Property ID(cannot be null).
	 * @return A sub-class instance of PropertyDescriptor
	 */
	public static IPropertyDescriptor getPropertyDescriptor(
			String elementName, String property )
	{
		assert property != null;

		PropertyProcessor processor = new PropertyProcessor( elementName,
				property );
		if ( fontSizeConstants.contains( property ) )
		{
			return new FontSizePropertyDescriptor( processor );
		}
		if ( simpleComboConstants.contains( property ) )
		{
			return new SimpleComboPropertyDescriptor( processor );
		}
		if ( textConstants.contains( property ) )
		{
			TextPropertyDescriptor desc = new TextPropertyDescriptor( processor );
			if ( ModuleHandle.CREATED_BY_PROP.equals( property )
					|| DataSetHandle.DATA_SOURCE_PROP.equals( property )
					|| ScalarParameterHandle.CONTROL_TYPE_PROP.equals( property )
					|| ScalarParameterHandle.DATA_TYPE_PROP.equals( property ) 
					|| ReportDesignHandle.ICON_FILE_PROP.equals(property))
			{
				desc.setStyle( SWT.BORDER | SWT.READ_ONLY );
			}

			return desc;
		}
		if ( marginConstants.contains( property ) )
		{
			return new MarignPropertyDescriptor( processor );
		}
		if ( multiTextConstants.contains( property ) )
		{
			TextPropertyDescriptor descriptor = new TextPropertyDescriptor( processor );
			descriptor.setStyle( SWT.MULTI
					| SWT.WRAP
					| SWT.BORDER
					| SWT.H_SCROLL
					| SWT.V_SCROLL );

			return descriptor;
		}
		if ( unitConstants.contains( property ) )
		{
			return new UnitPropertyDescriptor( processor );
		}
		if ( colorConstants.contains( property ) )
		{
			return new ColorPropertyDescriptor( processor );
		}
		if ( spinnerConstants.contains( property ) )
		{
			return new SpinnerPropertyDescriptor( processor );
		}
		if ( checkConstants.contains( property ) )
		{
			return new CheckPropertyDescriptor( processor );
		}
		if ( comboConstants.contains( property ) )
		{
			if ( property.equals( AttributeConstant.HORIZONTAL_ALIGN ) )
			{
				processor = new PropertyProcessor( elementName,
						StyleHandle.TEXT_ALIGN_PROP );
			}
			return new ComboPropertyDescriptor( processor );
		}
		if ( property.equals( AttributeConstant.FONT_WIDTH ) )
		{
			TogglePropertyDescriptor descriptor = new TogglePropertyDescriptor( processor );
			descriptor.setAllowedValues( AttributeValueConstant.FONT_BOLD_NORMAL,
					AttributeValueConstant.FONT_BOLD );
			return descriptor;
		}
		if ( property.equals( AttributeConstant.FONT_STYLE ) )
		{
			TogglePropertyDescriptor descriptor = new TogglePropertyDescriptor( processor );
			descriptor.setAllowedValues( AttributeValueConstant.FONT_BOLD_NORMAL,
					AttributeValueConstant.FONT_ITALIC );
			return descriptor;
		}
		if ( property.equals( AttributeConstant.TEXT_UNDERLINE ) )
		{
			TogglePropertyDescriptor descriptor = new TogglePropertyDescriptor( processor );
			descriptor.setAllowedValues( AttributeValueConstant.TEXT_UNDERLINE_NORMAL,
					AttributeValueConstant.TEXT_UNDERLINE );
			return descriptor;
		}
		if ( property.equals( AttributeConstant.TEXT_LINE_THROUGH ) )
		{
			TogglePropertyDescriptor descriptor = new TogglePropertyDescriptor( processor );
			descriptor.setAllowedValues( AttributeValueConstant.TEXT_LINE_THROUGH_NORMAL,
					AttributeValueConstant.TEXT_LINE_THROUGH );
			return descriptor;
		}
		if ( property.equals( AttributeConstant.BORDER_LEFT_STYLE ) )
		{
			TogglePropertyDescriptor descriptor = new TogglePropertyDescriptor( processor );
			return descriptor;
		}
		if ( property.equals( AttributeConstant.TEXT_ALIGN ) )
		{
			TextAlignPropertyDescriptor descriptor = new TextAlignPropertyDescriptor( processor );
			return descriptor;
		}
		if ( property.equals( TextItemHandle.CONTENT_RESOURCE_KEY_PROP )
				|| property.equals( LabelHandle.TEXT_ID_PROP )
				|| property.equals( ScalarParameterHandle.PROMPT_TEXT_ID_PROP ) )
		{
			return new ResourceKeyDescriptor( processor );
		}
		if ( ReportItemHandle.BOOKMARK_PROP.equals( property )
				|| ReportItemHandle.TOC_PROP.equals( property )
				|| TextDataHandle.VALUE_EXPR_PROP.equals( property ) )
		{
			return new ExpressionPropertyDescriptor( processor );
		}
		if ( property.equals( ModuleHandle.INCLUDE_RESOURCE_PROP ) )
		{
			// return new ResourceFileDescriptor( processor );
			return new TextPropertyDescriptor( processor );
		}
		if ( property.equals( ReportItemHandle.STYLE_PROP )
				|| property.equals( ModuleHandle.THEME_PROP )
				|| property.equals( StyleHandle.MASTER_PAGE_PROP ) )
		{
			SimpleComboPropertyDescriptor descriptor = new SimpleComboPropertyDescriptor( processor );
			// descriptor.addStyle( SWT.READ_ONLY );
			return descriptor;
		}
		return null;
	}
}