package ca.ubc.cs.commandrecommender.usagedata.gathering.screenshot;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Timer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import ca.ubc.cs.commandrecommender.usagedata.gathering.UsageDataCaptureActivator;

public class TimedScreenCapture implements ActionListener{
	
	private static final int DELAY = 10000; // ten seconds in milliseconds
	private static final String IMG_NAME = "screenshot";
	private static final String IMG_FORMAT = ".png";
	
	private Timer timer;
	private int imgCounter;
	private UsageDataCaptureActivator udca;
	
	public TimedScreenCapture(UsageDataCaptureActivator udca) throws AWTException {
		//this.robot = new Robot();
		this.timer = new Timer(DELAY, this);
		timer.start();
		imgCounter = 0;
		this.udca = udca;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				final Display display = PlatformUI.getWorkbench().getDisplay();
				Composite shell = display.getActiveShell();
				if (shell == null) {
					return;
				} else {
					takeScreenshot(display, shell);
				}
			}

			public void takeScreenshot(Display display, Composite shell) {
				if (shell.getParent() == null) {
					GC gc = new GC(display);
					final Image screenshot = new Image(display, shell.getBounds());
					gc.copyArea(screenshot, shell.getBounds().x, shell.getBounds().y);
					gc.dispose();
					
					String fileName = IMG_NAME + imgCounter + IMG_FORMAT;
					imgCounter++;
					String imgFilePath = udca.getSettings().getScreenCapFilePath(fileName);
					// TODO printing file path for testing only
					System.out.println(imgFilePath);
					ImageLoader imgLoader = new ImageLoader();
					imgLoader.data = new ImageData[] {screenshot.getImageData()};
					imgLoader.save(imgFilePath, SWT.IMAGE_PNG);

					screenshot.dispose();
				} else {
					takeScreenshot(display, shell.getParent());
				}
			}
		});	
	}
}

@@ -97,272 +97,82 @@
 on;%0A
-import java.awt.Rectangle;%0Aimport java.awt.Robot;%0Aimport java.awt.Toolkit;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0Aimport java.awt.image.BufferedImage;%0Aimport java.io.File;%0Aimport java.io.IOException;%0A%0Aimport javax.imageio.ImageIO;
+%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0A
 %0Aimp
@@ -472,46 +472,8 @@
 ay;%0A
-import org.eclipse.swt.widgets.Shell;%0A
 impo
@@ -1001,38 +1001,8 @@
 n %7B%0A
-%09%09//this.robot = new Robot();%0A
 %09%09th
@@ -1230,22 +1230,16 @@
 ) %7B%0A%09%09%09%09
-final 
 Display 

Remove imports from previous implementation using Robot