/**
 * Copyright(C) 2019 Patrik Dufresne Service Logiciel <info@patrikdufresne.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.planimod.ui.views.admin;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jface.action.Action;

import com.patrikdufresne.util.Localized;
import com.planimod.ui.databinding.MotifBean;
import com.planimod.ui.theme.Resources;

/**
 * This action is used to remove a chart from a set of charts. Notice: this action does not persists the modification.
 * 
 * @author Karl St-Cyr
 * 
 */
public class RemoveMotifEntry extends Action {

    /**
     * Selected Motif.
     */
    public static final String MOTIF = "motif";//$NON-NLS-1$

    /**
     * Motif property.
     */
    public static final String MOTIFS = "motifs"; //$NON-NLS-1$

    /**
     * Used for localization.
     */
    private Localized localized = Localized.load(AddMotifEntry.class);

    /**
     * Point chart property name.
     */
    private MotifBean motif;

    /**
     * The current collection of charts
     */
    private Set<MotifBean> motifs;

    /**
     * Create a new action.
     */
    public RemoveMotifEntry() {
        setText(this.localized.get("RemoveMotifEntry.text")); //$NON-NLS-1$
        setImageDescriptor(Resources.getImageDescriptor(Resources.ICON_LIST_REMOVE_16));
        setEnabled(canRun());
        setToolTipText(this.localized.get("RemoveMotifEntry.tooltip")); //$NON-NLS-1$
    }

    /**
     * Check if this action can be run.
     * 
     * @return True if the action can be run.
     */
    protected boolean canRun() {
        return motif != null && motifs != null && motifs.contains(motif);
    }

    /**
     * Return the chart object to be deleted.
     * 
     * @return the chart object or null if not set.
     */
    public MotifBean getMotif() {
        return motif;
    }

    /**
     * Return the point chart to be updated or null if not set.
     * 
     * @return Set<MotifBean>
     */
    public Set<MotifBean> getMotifs() {
        return motifs;
    }

    /**
     * This implementation remove the object from the list and fire a change property.
     */
    @Override
    public void run() {

        if (!canRun()) {
            return;
        }
        if (this.motifs == null) {
            this.motifs = new HashSet<MotifBean>();
            this.motifs.add(new MotifBean());
        }
        // Remove the object from the current list
        Set<MotifBean> oldList = this.motifs;
        this.motifs = new HashSet<MotifBean>(this.motifs);
        if (this.motifs.remove(this.motif)) {
            // Fire a property change
            firePropertyChange(MOTIFS, oldList, this.motifs);
        }

    }

    /**
     * Sets the chart to be deleted.
     * 
     * @param motif
     *            the chart object
     */
    public void setMotif(MotifBean motif) {
        if (this.motif != motif) {
            firePropertyChange(MOTIF, this.motif, this.motif = motif);
            setEnabled(canRun());
        }
    }

    /**
     * Sets the point chart object to be updated or null if not set.
     * 
     * @param set
     *            <MotifBean>
     */
    public void setMotifs(Set<MotifBean> motifs) {
        if (this.motifs != motifs) {
            firePropertyChange(MOTIF, this.motifs, this.motifs = motifs);
            setEnabled(canRun());
        }
    }

}
