package com.modofo.molv;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

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.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class Molv implements AlertListener {
	static {
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			is = ClassLoader.getSystemResourceAsStream("swt-win32-3347.dll");

			File dllFile = new File("swt-win32-3347.dll");
			dllFile.deleteOnExit();

			fos = new FileOutputStream(dllFile);

			byte[] buf = new byte[1024];
			int n;
			while ((n = is.read(buf)) != -1) {
				fos.write(buf, 0, n);
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	Scheduler scheduler;
	Display display;
	Shell shell;
	Tray tray;
	ToolTip tip;
	
	public static int ONE_MIN=60,ONE_HOUR = ONE_MIN*60; 
	public static void main(String[] args) {
		Molv molv = new Molv();
		Alert alert = new Alert(ONE_HOUR);
		molv.start(alert);
	}

	public Molv() {
		scheduler = new Scheduler();
		scheduler.setListener(this);
	}

	void start(Alert alert) {
		display = new Display();
		shell = new Shell(display);
		Image image = new Image(display, Molv.class
				.getResourceAsStream("icon3.png"));
		tray = display.getSystemTray();

		tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
		tip.setMessage(" ");
		
		if (tray != null) {
			TrayItem item = new TrayItem(tray, SWT.NONE);
			item.setImage(image);

			tip.setAutoHide(true);
			
			item.setToolTip(tip);

			item.addListener(SWT.Show, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("show");
				}
			});
			item.addListener(SWT.Hide, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("hide");
				}
			});
			item.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("selection");
				}
			});
			item.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("default selection");
				}
			});

			final Menu menu = new Menu(shell, SWT.POP_UP);
			MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
			
//			menuItem.setText("Start");
//			menuItem = new MenuItem(menu, SWT.PUSH);
//			menuItem.addListener(SWT.Selection, new Listener() {
//				public void handleEvent(Event e) {
//					//restart 
//				}
//			});
			
			menuItem.setText("Exit");
			menuItem.addSelectionListener(new SelectionAdapter() {      
		         public void widgetSelected(SelectionEvent arg0) {
		        	scheduler.stop();
		            display.dispose();      
		         }
		      });
			
			item.addListener(SWT.MenuDetect, new Listener() {
				public void handleEvent(Event event) {
					menu.setVisible(true);
				}
			});
		}

		scheduler.schedule(alert);

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		image.dispose();
		display.dispose();
		shell.dispose();
	}

	public void onAlert(final Alert alert) {
		display.syncExec(new Runnable() {
			public void run() {
				tip.setText(alert.message);
				tip.setVisible(true);
			}
		});
	}

	static {
		InputStream is = null;
		FileOutputStream fos = null;
		try {
			is = ClassLoader.getSystemResourceAsStream("swt-win32-3347.dll");

			File dllFile = new File("swt-win32-3347.dll");
			dllFile.deleteOnExit();

			fos = new FileOutputStream(dllFile);

			byte[] buf = new byte[1024];
			int n;
			while ((n = is.read(buf)) != -1) {
				fos.write(buf, 0, n);
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

class Alert {
	String message = "Have a break!";
	int interval; // hour

	public Alert(int interval) {
		this.interval = interval;
	}

	public Alert(String message, int interval) {
		this.interval = interval;
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public int getInterval() {
		return interval;
	}
}

class Scheduler extends TimerTask {
	boolean hasJob;
	Alert alert;
	AlertListener listener;
	Timer timer;

	public void schedule(Alert alert) {
		this.alert = alert;
		timer = new Timer();
		timer.schedule(this, new Date(), alert.getInterval() * 1000);
	}

	public void stop() {
		timer.cancel();
	}

	public void setListener(AlertListener listener) {
		this.listener = listener;
	}

	@Override
	public void run() {
		System.out.println("alert"+System.currentTimeMillis());
		listener.onAlert(alert);
	}
}

interface AlertListener {
	void onAlert(Alert alert);
}


