package artistmetrics.actions;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.IDebugTarget;

public class DebugLaunchListener implements ILaunchListener {

	public ILaunchManager ILM;
	private DebugTracker bp;
	public DebugLaunchListener(DebugTracker bp,ILaunchManager ILM)
	{
		this.bp = bp;
		this.ILM = ILM;
	}
	/*
	 * This method basically do nothing it just add the debug launch event to the manager 
	 * you can't get any information out, because the launch event haven't excute yet.
	 * you have to wait till the whole debug event finish to get the detail.
	 * 
	 */
	public void launchAdded(ILaunch launch) 
	{
		try
		{
			ILM.addLaunch(launch);
			bp.user.writeToLog("Debug Start:");
			
			User user = User.getSystemUser();
			Map<String, String> data = new HashMap<String, String>();
			user.sendMetric("debugger-start", data);	
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In addLaunch: " + e.getMessage());
		}
	}
	/*
	 *	This method will be enter when debugger run into error message
	 *   At the moment I am not sure what should we do about it.
	 */
	public void launchChanged(ILaunch launch) 
	{
		try
		{
			
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("random: " + e.getMessage());
		}
	}
	/*
	 *  If want to get more debug even such as which class have the debug go into etc 
	 *  have to get it inside this method.
	 * 
	 */
	public void launchRemoved(ILaunch launch) 
	{
		try
		{
			IDebugTarget[] ibt = launch.getDebugTargets();
			// return all the class involve with this debug events
			for(int index = 0; index < ibt.length; index++)
			{
				System.out.println(ibt[index].getName());
			}
			//write to the log the debug have finished.
			bp.user.writeToLog("Debug Finished:");
			ILM.removeLaunch(launch);
			
			
			User user = User.getSystemUser();
			Map<String, String> data = new HashMap<String, String>();
			user.sendMetric("debugger-end", data);
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In removeLaunch: " + e.getMessage());
		}
		
	}
	
}
