package ontop.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;


public class OnTopDlg extends Dialog {

    private boolean mouseDown = false;

    private Point p;

    private Point origin;

    private Composite mainComp;

    private String title = "";

    private Shell shell = null;

    private Display display;

    public OnTopDlg(Shell parent, String title) {
        super(parent, SWT.TOOL | SWT.ON_TOP);
        this.title = title;

        initializeComponents();
    }

    public void run() {
        p = shell.getLocation();
        origin = p;

        final Label topLbl = new Label(shell, SWT.BORDER | SWT.CENTER);
        topLbl.setText("Drag Bar - " + title);
        topLbl.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));
        topLbl.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));

        mainComp = new Composite(shell, SWT.NONE);
        GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.heightHint = 300;
        gd.widthHint = 400;
        mainComp.setLayout(new GridLayout(1, false));
        mainComp.setLayoutData(gd);

        topLbl.addMouseMoveListener(new MouseMoveListener() {

            @Override
            public void mouseMove(MouseEvent e) {
                if (mouseDown == true) {
                    // System.out.println(e.x);
                    // System.out.println(e.y);
                    // shell.setLocation(e.x, e.y);

                    if (origin != null) {
                        p = display.map(shell, null, e.x, e.y);
                        shell.setLocation(p.x - origin.x, p.y - origin.y);
                    }
                }
            }
        });

        topLbl.addMouseListener(new MouseListener() {

            @Override
            public void mouseUp(MouseEvent e) {
                mouseDown = false;

                Point location = shell.getLocation();

                System.out.println("**************************");
                System.out.println(Display.getCurrent().getClientArea());
                System.out.println(shell.getMonitor().getClientArea());

                if (location.y < shell.getMonitor().getClientArea().y) {
                    shell.setLocation(location.x, shell.getMonitor().getClientArea().y);
                }
                else if (location.y > shell.getMonitor().getClientArea().height
                        + shell.getMonitor().getClientArea().y - 25) {
                    shell.setLocation(location.x, shell.getMonitor().getClientArea().height
                            + shell.getMonitor().getClientArea().y - 25);
                }

            }

            @Override
            public void mouseDown(MouseEvent e) {
                mouseDown = true;

                // Get the mouse's click position.
                origin = new Point(e.x, e.y);
            }

            @Override
            public void mouseDoubleClick(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });
    }

    public boolean isDisposed() {
        if (shell == null || shell.isDisposed()) {
            return true;
        }

        return false;
    }

    public void open() {
        initializeComponents();
        shell.setBounds(new Rectangle(100, 100, 300, 300));
        // shell.setSize(300, 300);
        shell.open();
    }

    private void initializeComponents() {
        display = getParent().getDisplay();
        shell = new Shell(display, SWT.TOOL | SWT.ON_TOP);
        shell.setLayout(new GridLayout(1, false));
        shell.setLayoutData(new GridData(SWT.FILL, SWT.DEFAULT, true, false));

        run();

        Button exitBtn = new Button(shell, SWT.PUSH);
        exitBtn.setText("Exit");
        GridData gd = new GridData(SWT.CENTER, SWT.DEFAULT, true, false);
        exitBtn.setLayoutData(gd);
        exitBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });

    }
}
