/*******************************************************************************
 * Copyright (c) 2007 Symbian Software Limited and others.
 * 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:
 * Bala Torati (Symbian) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui.templateengine;

import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardNode;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.cdt.ui.templateengine.pages.UIWizardPage;


/**
 * Wizard Node 
 */
class WizardNode implements IWizardNode {
	private IWizard wizard;
	private Template template;
	private TemplateListSelectionPage parentPage;

	/**
	 * Constructor.
	 * @param parentPage
	 * @param template
	 */
	public WizardNode(TemplateListSelectionPage parentPage, Template template) {
		this.parentPage = parentPage;
		this.template = template;
	}
	
	public void dispose() {
		if (wizard != null) {
			wizard.dispose();
			wizard = null;
		}
	}
	
	/**
	 * Returns the Template
	 */
	public Template getTemplate() {
		return template;
	}

	public Point getExtent() {
		return new Point(-1, -1);
	}
	
	/**
	 * Returns the Wizard.
	 */
	public IWizard getWizard() {
		if (wizard != null) {
			return wizard;
		}
		wizard = new Wizard() {
			{
				setWindowTitle(template.getLabel());
			}
			
			private boolean finishPressed;
			
			public void addPages() {
				IWizardPage[] wpages = null;
				try {
					wpages = parentPage.getPagesAfterTemplateSelection();
				} catch (Exception e) {
				}
				for(int i=0; i<wpages.length; i++) {
					addPage(wpages[i]);
				}
				
				Map/*<String, UIWizardPage>*/ pages = template.getUIPages();
				for(Iterator i = template.getPagesOrderVector().iterator(); i.hasNext(); ){
					String id = (String) i.next();
					addPage((UIWizardPage) pages.get(id));
				}

				wpages = parentPage.getPagesAfterTemplatePages();
				for(int i=0; i<wpages.length; i++) {
					addPage(wpages[i]);
				}
			}
			
			public boolean performFinish() {
				Map/*<String, String>*/ valueStore = template.getValueStore();
				finishPressed = true;
				getContainer().updateButtons();
				IWizardPage[] wpages = getPages();
				for(int i=0; i<wpages.length; i++) {
					IWizardPage page = wpages[i];
					if (page instanceof UIWizardPage)
						valueStore.putAll(((UIWizardPage) page).getPageData());
				}
				template.getValueStore().putAll(parentPage.getDataInPreviousPages());
				return true;
			}
			
			public boolean canFinish(){
				return !finishPressed && super.canFinish();
			}

			public void createPageControls(Composite pageContainer) {
				super.createPageControls(pageContainer);
				parentPage.adjustTemplateValues(template);
				IWizardPage[] wpages = getPages();
				for(int i=0; i<wpages.length; i++) {
					IWizardPage page = wpages[i];
					if (page instanceof UIWizardPage)
					((UIWizardPage) page).getComposite().getUIElement().setValues(template.getValueStore());
				}
			}

			public Image getDefaultPageImage() {
				return parentPage.getImage();
			}
		};
		return wizard;
	}

	public boolean isContentCreated() {
		return wizard != null;
	}
}
