package artistmetrics.actions;


import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointListener;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.IDebugEventSetListener;
import org.eclipse.debug.core.IExpressionListener;
import org.eclipse.debug.core.IExpressionManager;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IExpression;
import org.eclipse.debug.core.model.IProcess;


/*
 * This class will be return all the break point add by user
 * also the when the debuger start and finished.
 * 
 * @author Jen-Hung Hsueh(Lewis)
 */


public class bPlug 
{
	public static IBreakpointManager IBM;
	public static ILaunchManager ILM;
	public static IExpressionManager IEM;
	
	public User user;
/*Constructor*/
	/*
	 * Create a new log to store the debug detail, such as add/remove breakpoint
	 * and start and finish debug event.
	 * Create the IBreakpointManager to get all breakpoint events.
	 * Create ILaunchManager to get all debug launch events. 
	 */
	public bPlug(User user)
	{	
		this.user = user;
		IBM = DebugPlugin.getDefault().getBreakpointManager(); 
		IBM.addBreakpointListener(new aBreakListener(this,IBM));
		ILM = DebugPlugin.getDefault().getLaunchManager();
		ILM.addLaunchListener(new aLaunchListener(this,ILM));
		IEM = DebugPlugin.getDefault().getExpressionManager();
		IEM.addExpressionListener(new aExpressionListener(this,IEM));
	}
	/*
	 * Write the event messages to the debugger event log.
	 * Currently it's writing to the log when some states changes. 
	 *
	 *	Maybe it's a good idea to store all the information and write to the log all at once
	 * when user are logging off.... 
	 */
	/*
	 * This is the old method that will get all the proccess for each launch
	 * This method is no longer be call, I just put here just incase need in the
	 * future
	 * 
	 */
	public void printAllLaunch()
	{
		try
		{
			ILaunch[] IL = ILM.getLaunches();
			for(int index = 0; index < IL.length; index++)
			{
				ILaunch ILL = IL[index];
				IProcess[] ip = ILL.getProcesses();
				for(int index1= 0;index1< ip.length; index1++)
				{
					/* got the time when this process end!!!!*/
					System.out.println("process: " + ip[index1].getLabel() );
				}
			}
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In printAllLaunch: " + e.getMessage());
		}
	}

}



//Overwrite IBreakpointListener 
class aBreakListener implements IBreakpointListener 
{
	public IBreakpointManager IBM;
	private bPlug bp;
	public aBreakListener(bPlug 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());
		}
		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());
		}
	}
}
//Overwrite ILaunchListener
class aLaunchListener implements ILaunchListener
{
	public ILaunchManager ILM;
	private bPlug bp;
	public aLaunchListener(bPlug 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:");
		}
		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);
		}
		catch(Exception e)
		{
			System.out.println("Error!!!!");
			System.out.println("In removeLaunch: " + e.getMessage());
		}
		
	}
	
}
/*
 * Unsure what the expression are for.....
 * 
 * 
 */
class aExpressionListener implements IExpressionListener
{

	public IExpressionManager IEM;
	private bPlug bp;
	public aExpressionListener(bPlug bp,IExpressionManager IEM)
	{
		this.bp = bp;
		this.IEM = IEM;
	}
	public void expressionAdded(IExpression expression) 
	{
		System.out.println("testing");
		System.out.println(expression.toString());
		IEM.addExpression(expression);
		
	}

	public void expressionChanged(IExpression expression) 
	{
		// TODO Auto-generated method stub
		
	}

	public void expressionRemoved(IExpression expression) 
	{
		// TODO Auto-generated method stub
		
	}
}


