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

import java.awt.AWTException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
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.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() {
				Display display = PlatformUI.getWorkbench().getDisplay();
				Composite shell = display.getActiveShell();
				// if the Eclipse window is not active, skip the screenshot
				if (shell == null) {
					return;
				} else {
					takeScreenshot(display, shell);
				}
			}

			public void takeScreenshot(Display display, Composite shell) {
				// if this is the main Eclipse workbench (i.e., not a child of the Eclipse window,
				// so parent == the display which is null), then take the screenshot using the shell coordinates
				if (shell.getParent() == null) {
					// take the screenshot
					GC gc = new GC(display);
					Rectangle winRect = shell.getBounds();
					Image screenshot = new Image(display, winRect);
					gc.copyArea(screenshot, winRect.x, winRect.y);
					gc.dispose();
					
					// save the screenshot
					String fileName = IMG_NAME + imgCounter + IMG_FORMAT;
					System.out.println(fileName);
					imgCounter++;
					String imgFilePath = udca.getSettings().getScreenCapFilePath(fileName);
					ImageLoader imgLoader = new ImageLoader();
					imgLoader.data = new ImageData[] {screenshot.getImageData()};
					imgLoader.save(imgFilePath, SWT.IMAGE_PNG);
					System.out.println(imgFilePath);

					screenshot.dispose();
				} else {
					// if this is a child of the main Eclipse shell, pass its parent shell back to takeScreenshot
					// recursion should be minimal as there will only ever be a small number of Eclipse windows open
					takeScreenshot(display, shell.getParent());
				}
			}
		});	
	}
}

@@ -686,19 +686,19 @@
 Listener
+ 
 %7B%0A
-%09
 %0A%09privat
@@ -866,17 +866,16 @@
 %22.png%22;%0A
-%09
 %0A%09privat
@@ -955,17 +955,16 @@
 r udca;%0A
-%09
 %0A%09public
@@ -1014,17 +1014,20 @@
 or udca)
- 
+%0A%09%09%09
 throws A
@@ -1137,17 +1137,16 @@
 dca;%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -1450,25 +1450,150 @@
 %09%09%09%09
-if (shell == null
+// TODO this is not working correctly, not taking screenshot if%0A%09%09%09%09// multiple child windows open%0A%09%09%09%09if (shell == null %7C%7C shell.isDisposed()
 ) %7B%0A
@@ -1795,24 +1795,31 @@
 t a child of
+%0A%09%09%09%09//
  the Eclipse
@@ -1880,32 +1880,39 @@
 ), then take the
+%0A%09%09%09%09//
  screenshot usin
@@ -2198,21 +2198,16 @@
 pose();%0A
-%09%09%09%09%09
 %0A%09%09%09%09%09//
@@ -2286,16 +2286,62 @@
 FORMAT;%0A
+%09%09%09%09%09// Print to console for testing purposes%0A
 %09%09%09%09%09Sys
@@ -2550,16 +2550,17 @@
 Data%5B%5D %7B
+ 
 screensh
@@ -2576,16 +2576,17 @@
 geData()
+ 
 %7D;%0A%09%09%09%09%09
@@ -2629,16 +2629,62 @@
 E_PNG);%0A
+%09%09%09%09%09// Print to console for testing purposes%0A
 %09%09%09%09%09Sys
@@ -2816,16 +2816,24 @@
 pass its
+%0A%09%09%09%09%09//
  parent 
@@ -2925,16 +2925,24 @@
 ver be a
+%0A%09%09%09%09%09//
  small n
@@ -3036,15 +3036,14 @@
 %09%7D%0A%09%09%7D);
-%09
 %0A%09%7D%0A%7D%0A

Add comments to TimedScreenCapture for clarity