//   Copyright 2012 Georg-August-Universität Göttingen, Germany
//
//   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 de.ugoe.cs.autoquest.ui.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;

import de.ugoe.cs.util.StringTools;
import de.ugoe.cs.util.console.CommandExecuter;
import de.ugoe.cs.util.console.GlobalDataContainer;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class DataTabComposite extends Composite {

    List dataList;

    /**
     * Create the composite.
     * 
     * @param parent
     * @param style
     */
    public DataTabComposite(Composite parent, int style) {
        super(parent, style);
        createContent();
    }

    private void createContent() {
        setLayout(new GridLayout(3, false));

        dataList = new List(this, SWT.BORDER | SWT.V_SCROLL);
        dataList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

        Button btnLoad = new Button(this, SWT.NONE);
        btnLoad.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                GetObjectNameDialog getObjectNameDialog =
                    new GetObjectNameDialog(getShell(), SWT.NONE);
                getObjectNameDialog.open();
                String objectName = getObjectNameDialog.getObjectName();
                if ("".equals(objectName)) {
                    return;
                }
                FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
                String filename = fileDialog.open();
                if (filename == null) {
                    return;
                }
                String command = "loadObject " + filename + " " + objectName;
                CommandExecuter.getInstance().exec(command);
                updateDataList();
            }
        });
        btnLoad.setText("Load");

        Button btnSave = new Button(this, SWT.NONE);
        btnSave.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String[] selectedStrings = dataList.getSelection();
                if (selectedStrings.length == 0) {
                    SWTHelpers.noSelectionError(getShell());
                    return;
                }
                if (selectedStrings.length > 1) {
                    MessageBox messageBox = new MessageBox(getShell(), SWT.ERROR);
                    messageBox.setText("Error");
                    messageBox.setMessage("Only one object storable at a time." +
                        StringTools.ENDLINE + "Please select only one object.");
                    return;
                }
                FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
                String filename = fileDialog.open();
                if (filename == null) {
                    return;
                }
                String command = "saveObject " + filename + " " + selectedStrings[0];
                CommandExecuter.getInstance().exec(command);
            }
        });
        btnSave.setText("Save");

        Button btnDelete_2 = new Button(this, SWT.NONE);
        btnDelete_2.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (SWTHelpers.deleteSelectedFromStorage(dataList)) {
                    updateDataList();
                }
                else {
                    SWTHelpers.noSelectionError(getShell());
                }
            }
        });
        btnDelete_2.setText("Delete");
    }

    @Override
    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    public void updateDataList() {
        dataList.removeAll();
        for (String key : GlobalDataContainer.getInstance().getAllKeys()) {
            dataList.add(key + " (" + GlobalDataContainer.getInstance().getData(key).getClass().toString() + ")");
        }
    }

}
