package artistmetrics.actions;

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

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.debug.core.IBreakpointListener;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;

public class DebugBreakListener implements IBreakpointListener {
	public IBreakpointManager IBM;
	private DebugTracker bp;
	
	public DebugBreakListener(DebugTracker bp,IBreakpointManager IBM)
	{
		this.bp = bp;
		this.IBM = IBM;
	}
	/*
	 * A breakpoint has added, and it will inform bPlug class about it, and write to the log.
	 * Also the breakpoint added has add to the breakpoint manager.
	 */
	public void breakpointAdded(IBreakpoint breakpoint)
	{
		try
		{
			IBM.addBreakpoint(breakpoint);
			IMarker IM = breakpoint.getMarker();
			bp.user.writeToLog("Added breakPoint on line number " + IM.getAttribute(IM.LINE_NUMBER).toString() + " of file " + IM.getResource().getFullPath().toString());
			
			
			User user = User.getSystemUser();
			
			Map<String, String> data = new HashMap<String, String>();
			data.put("file", IM.getResource().getFullPath().toString());
			data.put("linenumber", IM.getAttribute(IM.LINE_NUMBER).toString());
			
			user.sendMetric("breakpoint", data);		
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In addBreakPoint: " + e.getMessage());
		}
	}
	/*
	 * This method are need inorder to do the overwrite.
	 * at the moment I don't think this method can do 
	 */
	public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) 
	{
		
	}
	/*
	 * A breakpoint has been removed, and it will inform bPlug class about it, and write to the log.
	 * Also the breakpoint added has removed to the breakpoint manager.
	 */
	public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) 
	{
		try
		{
			IMarker IM = breakpoint.getMarker();
			bp.user.writeToLog("Removed breakPoint on line number " + IM.getAttribute(IM.LINE_NUMBER).toString() + " of file " + IM.getResource().getFullPath().toString());
			IBM.removeBreakpoint(breakpoint, true);
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In removeBreakPoint: " + e.getMessage());
		}
	}
}
