/*
 * ARX Data Anonymization Tool
 * Copyright 2012 - 2023 Fabian Prasser and contributors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.deidentifier.arx.gui.view.impl.wizard;

import org.deidentifier.arx.gui.resources.Resources;
import org.deidentifier.arx.gui.view.impl.wizard.HierarchyWizardModelGrouping.HierarchyWizardGroupingGroup;
import org.deidentifier.arx.gui.view.impl.wizard.HierarchyWizardModelGrouping.HierarchyWizardGroupingInterval;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

/**
 * The editor's menu.
 *
 * @author Fabian Prasser
 * @param <T>
 */
public class HierarchyWizardEditorMenu<T> {

    /** Var. */
    private final Composite                       composite;

    /** Var. */
    private final Menu                            menu;

    /** Var. */
    private final MenuItem                        addBefore;

    /** Var. */
    private final MenuItem                        addAfter;

    /** Var. */
    private final MenuItem                        mergeUp;

    /** Var. */
    private final MenuItem                        mergeDown;

    /** Var. */
    private final MenuItem                        remove;

    /** Var. */
    private final MenuItem                        addRight;

    /** Var. */
    private final HierarchyWizardModelGrouping<T> model;

    /**
     * Creates a new instance.
     *
     * @param composite
     * @param model
     */
    public HierarchyWizardEditorMenu(final Composite composite, 
                                     final HierarchyWizardModelGrouping<T> model) {
        
        this.model = model;
        this.composite = composite;
        
        this.menu = new Menu(composite);
        this.remove = new MenuItem(menu, SWT.NONE);
        this.remove.setText(Resources.getMessage("HierarchyWizardEditorMenu.0")); //$NON-NLS-1$
        
        new MenuItem(menu, SWT.SEPARATOR);
        
        this.addBefore = new MenuItem(menu, SWT.NONE);
        this.addBefore.setText(Resources.getMessage("HierarchyWizardEditorMenu.1")); //$NON-NLS-1$
        
        this.addAfter = new MenuItem(menu, SWT.NONE);
        this.addAfter.setText(Resources.getMessage("HierarchyWizardEditorMenu.2")); //$NON-NLS-1$

        new MenuItem(menu, SWT.SEPARATOR);
        
        this.mergeDown = new MenuItem(menu, SWT.NONE);
        this.mergeDown.setText(Resources.getMessage("HierarchyWizardEditorMenu.3")); //$NON-NLS-1$
        
        this.mergeUp = new MenuItem(menu, SWT.NONE);
        this.mergeUp.setText(Resources.getMessage("HierarchyWizardEditorMenu.4")); //$NON-NLS-1$
        
        new MenuItem(menu, SWT.SEPARATOR);
        
        this.addRight = new MenuItem(menu, SWT.NONE);
        this.addRight.setText(Resources.getMessage("HierarchyWizardEditorMenu.5")); //$NON-NLS-1$        
    }
    
    /**
     * Add selection listener
     * @param listener
     */
    public void addAddAfterListener(SelectionListener listener) {
        this.addAfter.addSelectionListener(listener);
    }
    

    /**
     * Add selection listener
     * @param listener
     */
    public void addAddBeforeListener(SelectionListener listener) {
        this.addBefore.addSelectionListener(listener);
    }

    /**
     * Add selection listener
     * @param listener
     */
    public void addAddRightListener(SelectionListener listener) {
        this.addRight.addSelectionListener(listener);
    }

    /**
     * Add selection listener
     * @param listener
     */
    public void addMergeDownListener(SelectionListener listener) {
        this.mergeDown.addSelectionListener(listener);
    }

    /**
     * Add selection listener
     * @param listener
     */
    public void addMergeUpListener(SelectionListener listener) {
        this.mergeUp.addSelectionListener(listener);
    }
    
    /**
     * Add selection listener
     * @param listener
     */
    public void addRemoveListener(SelectionListener listener) {
        this.remove.addSelectionListener(listener);
    }

    /**
     * 
     *
     * @param x
     * @param y
     */
    @SuppressWarnings("unchecked")
    public void show(int x, int y){
        
        if (model.getSelectedElement() == null){
            return;
        }
        
        if (model.getSelectedElement() instanceof HierarchyWizardGroupingInterval){
            
            HierarchyWizardGroupingInterval<T> interval = 
                    (HierarchyWizardGroupingInterval<T>)model.getSelectedElement();
            
            if (model.getIntervals().size()==1) {
                this.remove.setEnabled(false);
            } else if (model.isFirst(interval) || model.isLast(interval)){
                this.remove.setEnabled(true);
            } else {
                this.remove.setEnabled(false);
            }
            
            if (model.isFirst(interval)) {
                this.addBefore.setEnabled(true);
                this.mergeDown.setEnabled(false);
            } else {
                this.addBefore.setEnabled(false);
                this.mergeDown.setEnabled(true);
            }
            if (model.isLast(interval)) {
                this.addAfter.setEnabled(true);
                this.mergeUp.setEnabled(false);
            } else {
                this.addAfter.setEnabled(false);
                this.mergeUp.setEnabled(true);
            }
                
            this.addRight.setEnabled(true);
            
        } else if (model.getSelectedElement() instanceof HierarchyWizardGroupingGroup){
            if (model.isShowIntervals()){
                this.remove.setEnabled(true); 
            } else {
                if (model.getModelGroups().size() == 1 &&
                    model.getModelGroups().get(0).size() == 1) {
                    this.remove.setEnabled(false);
                } else {
                    this.remove.setEnabled(true);

                }
            }
                
            this.addBefore.setEnabled(true);
            this.addAfter.setEnabled(true);
            this.addRight.setEnabled(true);
            this.mergeUp.setEnabled(false);
            this.mergeDown.setEnabled(false);
        }
        
        menu.setLocation(composite.toDisplay(x, y));
        menu.setVisible(true);
    }
}
