/**
 * 
 */
package com.planimod.ui.views.nonavailabilities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;
import com.patrikdufresne.managers.ManagedObject;
import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.util.Localized;
import com.planimod.core.Employee;
import com.planimod.core.NonAvailability;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.TimeRanges;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.theme.Resources;
import com.planimod.ui.views.AbstractAddAction;

/**
 * Action to create a new non-availability event and add it to the database.
 * 
 * @author dufresne
 */
public class AddNonAvailabilityAction extends AbstractAddAction {

    /**
     * Property name of an action's date (value <code>"date"</code>). The date
     * property is used sets a default time range.
     */
    public static final String DATE = "date"; //$NON-NLS-1$

    /**
     * Define the date state to update.
     */
    private Date date;

    /**
     * Temporary variable to store the selected employee value.
     */
    private Employee employee;

    /**
     * Temporary variable to store the end date.
     */
    private Date end;

    /**
     * Temporary variable to store the start date.
     */
    private Date start;

    /**
     * Temporary variable to store the summary date.
     */
    private String summary;

    /**
     * Temporary variable to store the visibility.
     */
    private boolean visible;

    private PlanimodManagers managers;

    /**
     * Create a new action.
     * 
     * @param shellProvider a shell provider in case the action need to display
     * a message box.
     */
    public AddNonAvailabilityAction(PlanimodManagers managers,
            IShellProvider shellProvider) {
        super(managers, shellProvider);
        this.managers = managers;
        setText(Localized.get(AddNonAvailabilityAction.class,
                "AddNonAvailabilitiesAction.text")); //$NON-NLS-1$
        setToolTipText(Localized.get(AddNonAvailabilityAction.class,
                "AddNonAvailabilitiesAction.tooltipText")); //$NON-NLS-1$
        setImageDescriptor(Resources.getImageDescriptor(Resources.ICON_LIST_ADD_16));
    }

    /**
     * This implementation always return true.
     * 
     * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#canCreateObject()
     */
    @Override
    protected boolean canCreateObject() {
        return true;
    }

    /**
     * This implementation create non-availability event object.
     * 
     * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#createObjects()
     */
    @Override
    protected List<? extends ManagedObject> createObjects() {
        List<ManagedObject> list = new ArrayList<ManagedObject>();
        // Create the event
        NonAvailability event = new NonAvailability();
        event.setEmployee(this.employee);
        event.setSummary(this.summary);
        event.setStartDate(this.start);
        event.setEndDate(this.end);
        event.setVisible(this.visible);
        list.add(event);
        return list;
    }

    /**
     * Returns the current date state of null if not define.
     * 
     * @return the date state or null.
     */
    public Date getDate() {
        return this.date;
    }

    @Override
    public void run() {
        // Get the girst day of week value
        int firstDayOfWeek = Calendar.SUNDAY;
        try {
            firstDayOfWeek =
                    this.managers.getApplicationSettingManager().getFirstDayOfWeek();
        } catch (ManagerException e) {
            PlanimodPolicy.showException(e);
        }
        NonAvailabilityDialog dlg =
                new NonAvailabilityDialog(getShellProvider().getShell(),
                        getManagers(), NonAvailabilityDialog.NEW);
        Date defaultDate = this.date == null
                ? new Date()
                : this.date;
        dlg.setStartDate(TimeRanges.getWeekStart(defaultDate, firstDayOfWeek));
        dlg.setEndDate(TimeRanges.getWeekEnd(defaultDate, firstDayOfWeek));
        dlg.setEmployee(null);
        if (dlg.open() != Window.OK) {
            return;
        }
        this.start = dlg.getStartDate();
        this.end = dlg.getEndDate();
        this.summary = dlg.getSummary();
        this.employee = dlg.getEmployee();
        this.visible = dlg.getVisible();
        // TODO Do extra validation
        // e.g.: employee must not be null
        // start should be before end
        super.run();
    }

    /**
     * Sets a default date value.
     * 
     * @param date the date value or null
     */
    public void setDate(Date date) {
        if ((this.date == null && date != null) || (this.date != null && date == null)
                || (this.date != null && date != null && !date.equals(this.date))) {
            Date oldValue = this.date;
            this.date = date;
            firePropertyChange(DATE, oldValue, this.date);
        }
    }
}
