/*
 * ARX Data Anonymization Tool
 * Copyright 2012 - 2024 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.common.table;

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.layer.IUniqueIndexLayer;
import org.eclipse.nebula.widgets.nattable.search.config.DefaultSearchBindings;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.selection.action.SelectCellAction;
import org.eclipse.nebula.widgets.nattable.selection.config.DefaultMoveSelectionConfiguration;
import org.eclipse.nebula.widgets.nattable.selection.config.DefaultSelectionBindings;
import org.eclipse.nebula.widgets.nattable.selection.config.DefaultSelectionStyleConfiguration;
import org.eclipse.nebula.widgets.nattable.tickupdate.config.DefaultTickUpdateConfiguration;
import org.eclipse.nebula.widgets.nattable.ui.action.IMouseAction;
import org.eclipse.nebula.widgets.nattable.ui.binding.UiBindingRegistry;
import org.eclipse.nebula.widgets.nattable.ui.matcher.MouseEventMatcher;
import org.eclipse.nebula.widgets.nattable.viewport.action.ViewportSelectColumnAction;
import org.eclipse.nebula.widgets.nattable.viewport.action.ViewportSelectRowAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;

/**
 * A selection layer for table views.
 *
 * @author Fabian Prasser
 */
public class LayerSelection extends SelectionLayer implements CTComponent {

    /**  Config */
    private final CTConfiguration config;
    
    /**
     * Creates a new instance.
     *
     * @param underlyingLayer
     * @param config
     */
    public LayerSelection(IUniqueIndexLayer underlyingLayer, CTConfiguration config) {
        super(underlyingLayer, false);
        this.config = config;
        addConfiguration(new DefaultSelectionStyleConfiguration());
        addConfiguration(new DefaultSelectionBindings(){
            /** Override some default behavior */
            protected void configureBodyMouseClickBindings(UiBindingRegistry uiBindingRegistry) {
                IMouseAction action = new SelectCellAction() {
                    @Override
                    public void run(NatTable natTable, MouseEvent event) {
                        super.run(natTable, event);
                        // Fixes redrawing bug on MacOS
                        natTable.redraw();
                    }
                };
                uiBindingRegistry.registerMouseDownBinding(MouseEventMatcher.bodyLeftClick(SWT.NONE), action);
            }
            /** Override some default behavior */
            protected void configureBodyMouseDragMode(UiBindingRegistry uiBindingRegistry) {
                // Ignore
            }
            /** Override some default behavior */
            protected void configureColumnHeaderMouseClickBindings(UiBindingRegistry uiBindingRegistry) {
                uiBindingRegistry.registerSingleClickBinding(MouseEventMatcher.columnHeaderLeftClick(SWT.NONE), new ViewportSelectColumnAction(false, false));
            }
            /** Override some default behavior */
            protected void configureRowHeaderMouseClickBindings(UiBindingRegistry uiBindingRegistry) {
                uiBindingRegistry.registerMouseDownBinding(MouseEventMatcher.rowHeaderLeftClick(SWT.NONE), new ViewportSelectRowAction(false, false));
            }
        });
        addConfiguration(new DefaultSearchBindings());
        addConfiguration(new DefaultTickUpdateConfiguration());
        addConfiguration(new DefaultMoveSelectionConfiguration());
    }

    @Override
    public CTConfiguration getConfig() {
        return config;
    }

    @Override
    public boolean isCellPositionSelected(int columnPosition, int rowPosition) {
        return getConfig().isCellSelectionEnabled() && super.isCellPositionSelected(columnPosition, rowPosition);
    }

    @Override
    public boolean isColumnPositionFullySelected(int columnPosition) {
        return getConfig().isColumnSelectionEnabled() && super.isColumnPositionFullySelected(columnPosition);
    }

    @Override
    public boolean isColumnPositionSelected(int columnPosition) {
        return false;
    }

    @Override
    public boolean isRowPositionFullySelected(int rowPosition) {
        return getConfig().isRowSelectionEnabled() && super.isRowPositionFullySelected(rowPosition);
    }

    @Override
    public boolean isRowPositionSelected(int rowPosition) {
        return false;
    }
}