/*
 * 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.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.ObservablesManager;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.util.Localized;
import com.planimod.core.ApplicationSetting;
import com.planimod.core.PlanimodManagers;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.theme.Resources;

/**
 * This dialog is used to change the Planimod parameters.
 * 
 * @author Patrik Dufresne
 * 
 */
public class GeneralPreferencePage extends PreferencePage {

	private WritableValue customerName = new WritableValue(null, String.class);

	private Text customerNameText;

	private DataBindingContext dbc;

	private Localized localized = Localized.load(GeneralPreferencePage.class);

	/**
	 * The Planimod manager used to change the paramaters.
	 */
	private PlanimodManagers managers;

	private ObservablesManager om;

	/**
	 * Create a new preference page.
	 * 
	 * @param managers
	 *            the Planimod manager
	 */
	public GeneralPreferencePage(PlanimodManagers managers) {
		super();
		if (managers == null) {
			throw new IllegalArgumentException();
		}
		this.managers = managers;
		setTitle(this.localized.get("PlanimodPreferenceDialog.title")); //$NON-NLS-1$
		setImageDescriptor(Resources
				.getImageDescriptor(Resources.ICON_PARAMETERS_16));
	}

	/**
	 * Used to bind the widgets
	 */
	protected void bindValues() {
		// Get initial data from managers
		try {
			this.customerName.setValue(this.managers
					.getApplicationSettingManager().getByName(
							ApplicationSetting.CUSTOMER_NAME));
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
		}

		// Bind the widgets
		this.dbc.bindValue(
				WidgetProperties.text(SWT.FocusOut).observe(
						this.customerNameText), this.customerName);

	}

	@Override
	protected Control createContents(Composite parent) {

		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(2, false));
		comp.setLayoutData(new GridData(GridData.FILL_BOTH));

		// Customer name
		Label label = new Label(comp, SWT.WRAP);
		label.setText(this.localized
				.get("PlanimodPreferenceDialog.customerName.notice")); //$NON-NLS-1$
		GridData data = new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1);
		data.widthHint = 150;
		label.setLayoutData(data);
		label = new Label(comp, SWT.NONE);
		label.setText(this.localized
				.get("PlanimodPreferenceDialog.customerName")); //$NON-NLS-1$
		this.customerNameText = new Text(comp, SWT.BORDER);
		this.customerNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, false));

		/*
		 * Bind widgets
		 */
		this.dbc = new DataBindingContext();
		this.om = new ObservablesManager();
		this.om.runAndCollect(new Runnable() {
			@Override
			public void run() {
				bindValues();
			}
		});
		this.om.addObservablesFromContext(this.dbc, true, true);

		return comp;
	}

	/**
	 * Return the customer name
	 * 
	 * @return
	 */
	public String getCustomerName() {
		if (this.customerName.getValue() instanceof String) {
			return (String) this.customerName.getValue();
		}
		return null;
	}

	/**
	 * This implementation save the parameters to the managers.
	 */
	@Override
	public boolean performOk() {
		try {
			String value = (String) (this.customerName.getValue() instanceof String ? this.customerName
					.getValue() : ""); //$NON-NLS-1$
			this.managers.getApplicationSettingManager().setByName(
					ApplicationSetting.CUSTOMER_NAME, value); //$NON-NLS-1$
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
		}
		return true;
	}

	/**
	 * Sets the customer name.
	 * 
	 * @param value
	 */
	public void setCustomerName(String value) {
		this.customerName.setValue(value);
	}

}
