/*
 * 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.menu;

import org.deidentifier.arx.gui.Controller;
import org.deidentifier.arx.gui.resources.Resources;
import org.deidentifier.arx.gui.view.SWTUtil;
import org.deidentifier.arx.gui.view.def.IDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * An about dialog.
 *
 * @author Fabian Prasser
 */
public class DialogDebug extends TitleAreaDialog implements IDialog {

    /**  TODO */
    private Image      image;
    
    /**  TODO */
    private Controller controller;
    
    /**  TODO */
    private Text       data;

    /**
     * Constructor.
     *
     * @param parentShell
     * @param controller
     */
    public DialogDebug(final Shell parentShell, final Controller controller) {
        super(parentShell);
        this.controller = controller;
        this.image = controller.getResources().getManagedImage("logo_small.png"); //$NON-NLS-1$
    }

    @Override
    public boolean close() {
        return super.close();
    }

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setImages(Resources.getIconSet(newShell.getDisplay()));
    }

    @Override
    protected void createButtonsForButtonBar(final Composite parent) {

        // Create OK Button
        parent.setLayoutData(SWTUtil.createFillGridData());
        
        final Button ceButton = createButton(parent,
                                             Integer.MAX_VALUE,
                                             "Clear events", false); //$NON-NLS-1$
        ceButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                controller.actionClearEventLog();
                data.setText(controller.getDebugData());
            }
        });
        
        final Button okButton = createButton(parent,
                                             Window.OK,
                                             "OK", true); //$NON-NLS-1$
        okButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                setReturnCode(Window.OK);
                close();
            }
        });
    }
    
    @Override
    protected Control createContents(Composite parent) {
    	Control contents = super.createContents(parent);
        setTitle("Debugging Console"); //$NON-NLS-1$
        setMessage("Displays internal data structures"); //$NON-NLS-1$
        if (image!=null) setTitleImage(image); //$NON-NLS-1$
        return contents;
    }
    
    @Override
    protected Control createDialogArea(final Composite parent) {
        parent.setLayout(new GridLayout());

        // License
        data = new Text(parent, SWT.NONE | SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
        data.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));
        data.setText(controller.getDebugData());
        final GridData d = SWTUtil.createFillGridData();
        d.heightHint = 200;
        d.grabExcessVerticalSpace = true;
        data.setLayoutData(d);

        return parent;
    }
    
    @Override
    protected boolean isResizable() {
        return false;
    }
}
