/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui.views.admin;

import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.HyperlinkSettings;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.patrikdufresne.ui.ViewPartPage;
import com.patrikdufresne.util.Localized;
import com.planimod.core.PlanimodManagers;
import com.planimod.ui.preference.PlanimodPreferences;
import com.planimod.ui.views.admin.position.PositionPageViewPart;
import com.planimod.ui.views.admin.product.ProductPageViewPart;
import com.planimod.ui.views.admin.section.SectionPageViewPart;
import com.planimod.ui.views.admin.team.TeamPageViewPart;

/**
 * This view part is the main page displayed in the administration view. It
 * provide an easy way to display other page like the position page or the
 * product page.
 * 
 * @author Patrik Dufresne
 * 
 */
public class AdminPageViewPart extends ViewPartPage {
	/**
	 * The part id.
	 */
	public static final String ID = "com.planimod.ui.view.admin.AdminIntroViewPart"; //$NON-NLS-1$

	/**
	 * Used for localization.
	 */
	protected Localized localized = Localized.load(AdminPageViewPart.class);

	/**
	 * Create a new view part.
	 * 
	 * @param toolkit
	 *            the form toolkit to use by this view part.
	 */
	public AdminPageViewPart() {
		super(ID);
		setTitle(this.localized.get("AdminViewPart.title")); //$NON-NLS-1$
	}

	/**
	 * This implementation will create control using the form toolkit.
	 */
	@Override
	public void activate(Composite parent) {

		// Create the form toolkit
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());
		toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(
				HyperlinkSettings.UNDERLINE_HOVER);

		// The administration view is build using the Rich Client Platform.
		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 3;
		layout.makeColumnsEqualWidth = true;
		Form form = toolkit.createForm(parent);
		form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		form.getBody().setLayout(layout);
		form.setText(this.localized.get("AdminViewPart.title")); //$NON-NLS-1$

		createSection(form.getBody(), toolkit,
				this.localized.get("AdminViewPart.section.position.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.position.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.position.link"), //$NON-NLS-1$
				PositionPageViewPart.ID);

		createSection(form.getBody(), toolkit,
				this.localized.get("AdminViewPart.section.product.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.product.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.product.link"), //$NON-NLS-1$
				ProductPageViewPart.ID);

		createSection(form.getBody(), toolkit,
				this.localized.get("AdminViewPart.section.section.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.section.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.section.link"), //$NON-NLS-1$
				SectionPageViewPart.ID);

		createSection(form.getBody(), toolkit,
				this.localized.get("AdminViewPart.section.team.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.team.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.team.link"), //$NON-NLS-1$
				TeamPageViewPart.ID);

		createSection(form.getBody(),
				toolkit,
				this.localized.get("AdminViewPart.section.preferences.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.preferences.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.preferences.link"), //$NON-NLS-1$
				new HyperlinkAdapter() {
					@Override
					public void linkActivated(HyperlinkEvent e) {
						PreferenceManager mgr = new PreferenceManager();
						mgr.addToRoot(new GeneralPreferenceNode(getSite()
								.getService(PlanimodManagers.class)));
						mgr.addToRoot(new AppearancePreferenceNode());
						PreferenceDialog dlg = new PreferenceDialog(getSite()
								.getShell(), mgr);
						dlg.setPreferenceStore(PlanimodPreferences
								.getPreferenceStore());
						dlg.open();
					}
				});

		createSection(form.getBody(), toolkit,
				this.localized.get("AdminViewPart.section.about.title"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.about.message"), //$NON-NLS-1$
				this.localized.get("AdminViewPart.section.about.link"), //$NON-NLS-1$
				new HyperlinkAdapter() {
					@Override
					public void linkActivated(HyperlinkEvent e) {
						AboutDialog dlg = new AboutDialog(getSite().getShell());
						dlg.open();
					}
				});

	}

	/**
	 * Create a section in the view part
	 * 
	 * @param parent
	 *            the composite parent
	 * @param toolkit
	 *            the tool kit
	 * @param title
	 *            the section title
	 * @param description
	 *            the section description
	 * @param link
	 *            the link name
	 * @param partId
	 *            the part id to activate
	 * @return the Section
	 */
	private Section createSection(Composite parent, FormToolkit toolkit,
			String title, String description, String link, final String partId) {
		return createSection(parent, toolkit, title, description, link,
				new HyperlinkAdapter() {
					@Override
					public void linkActivated(HyperlinkEvent e) {
						getSite().getParentPart().activateView(partId);
					}
				});
	}

	private Section createSection(Composite parent, FormToolkit toolkit,
			String title, String description, String link,
			IHyperlinkListener listener) {
		Section section = toolkit.createSection(parent,
				ExpandableComposite.TITLE_BAR);
		section.setText(title);
		section.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));

		// Create the content of the section
		Composite sectionClient = toolkit.createComposite(section);
		section.setClient(sectionClient);
		sectionClient.setLayout(new TableWrapLayout());

		FormText text = new FormText(sectionClient, SWT.WRAP | SWT.READ_ONLY);
		text.setText(description, false, false);

		// Create link to print cumulative results
		Hyperlink hyperLink = toolkit.createHyperlink(sectionClient, link,
				SWT.WRAP);
		hyperLink.addHyperlinkListener(listener);
		return section;
	}

}
