/***********************************************************************
 * 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.engine.layout.html;

import java.util.Collection;

import org.eclipse.birt.report.engine.content.IBandContent;
import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IElement;
import org.eclipse.birt.report.engine.content.IGroupContent;
import org.eclipse.birt.report.engine.content.IListContent;
import org.eclipse.birt.report.engine.content.IStyle;
import org.eclipse.birt.report.engine.content.ITableContent;
import org.eclipse.birt.report.engine.emitter.IContentEmitter;
import org.eclipse.birt.report.engine.executor.IReportItemExecutor;
import org.eclipse.birt.report.engine.internal.executor.dom.DOMReportItemExecutor;
import org.w3c.dom.css.CSSValue;

public class HTMLListingBandLM extends HTMLBlockStackingLM
{

	public HTMLListingBandLM( HTMLLayoutManagerFactory factory )
	{
		super( factory );
	}

	public int getType( )
	{
		return LAYOUT_MANAGER_LIST_BAND;
	}

	boolean repeatHeader;

	public void initialize( HTMLAbstractLM parent, IContent content,
			IReportItemExecutor executor, IContentEmitter emitter )
	{
		super.initialize( parent, content, executor, emitter );
		repeatHeader = false;
		intializeHeaderContent( );
	}

	public void close( )
	{
		super.close( );
		if ( repeatHeader )
		{
			assert executor instanceof DOMReportItemExecutor;
			executor.close( );
		}
	}

	private void intializeHeaderContent( )
	{
		assert content != null;
		IElement pContent = content.getParent( );
		assert pContent != null;
		assert content instanceof IBandContent;

		int type = ( (IBandContent) content ).getBandType( );
		repeatHeader = false;
		if ( type == IBandContent.BAND_HEADER
				|| type == IBandContent.BAND_GROUP_HEADER )
		{
			if ( pContent instanceof IGroupContent )
			{
				IGroupContent groupContent = (IGroupContent) pContent;
				repeatHeader = groupContent.isHeaderRepeat( );
			}
			else if ( pContent instanceof IListContent )
			{
				IListContent list = (IListContent) pContent;
				repeatHeader = list.isHeaderRepeat( );
			}
			else if ( pContent instanceof ITableContent )
			{
				ITableContent table = (ITableContent) pContent;
				repeatHeader = table.isHeaderRepeat( );
			}
		}

		if ( repeatHeader )
		{
			Collection children = content.getChildren( );
			if ( children == null || children.isEmpty( ) )
			{
				// fill the contents
				execute( content, executor );
				if ( !pContent.getChildren( ).contains( content ) )
				{
					pContent.getChildren( ).add( content );
				}
			}
			executor = new DOMReportItemExecutor( content );
			executor.execute( );
		}
	}
	
	protected boolean allowPageBreak( )
	{
		IBandContent band = (IBandContent) content;
		int type = band.getBandType( );
		if ( type == IBandContent.BAND_HEADER )
		{
			if(IStyle.SOFT_VALUE.equals( content.getStyle( ).getProperty( IStyle.STYLE_PAGE_BREAK_BEFORE ) ))
			{
				return true;
			}
			IElement listContent = band.getParent( );
			if ( listContent instanceof IListContent )
			{
				return !( (IListContent) listContent ).isHeaderRepeat( );
			}
		}
		else if ( type == IBandContent.BAND_GROUP_HEADER )
		{
			if(IStyle.SOFT_VALUE.equals( content.getStyle( ).getProperty( IStyle.STYLE_PAGE_BREAK_BEFORE ) ))
			{
				return true;
			}
			IElement groupContent = band.getParent( );
			if ( groupContent instanceof IGroupContent )
			{
				return !( (IGroupContent) groupContent ).isHeaderRepeat( );
			}
		}
		return true;
	}

	protected boolean needPageBreakBefore( )
	{
		if ( super.needPageBreakBefore( ) )
		{
			return true;
		}
		int bandType = ( (IBandContent) content ).getBandType( );
		if ( bandType == IBandContent.BAND_GROUP_HEADER )
		{
			IStyle style = content.getStyle( );
			CSSValue pageBreak = style
					.getProperty( IStyle.STYLE_PAGE_BREAK_BEFORE );
			if ( IStyle.SOFT_VALUE.equals( pageBreak ) )
			{
				style.setProperty( IStyle.STYLE_PAGE_BREAK_BEFORE,
						IStyle.AUTO_VALUE );
				return true;
			}
		}
		return false;
	}

}
