package net.java.dev.moskito.webui.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import net.anotheria.maf.action.ActionCommand;
import net.anotheria.maf.action.ActionMapping;
import net.anotheria.maf.bean.FormBean;
import net.java.dev.moskito.core.calltrace.CurrentlyTracedCall;
import net.java.dev.moskito.core.calltrace.TraceStep;
import net.java.dev.moskito.core.journey.Journey;
import net.java.dev.moskito.core.journey.NoSuchJourneyException;
import net.java.dev.moskito.webui.bean.AnalyzeProducerCallsMapBean;

public class AnalyzeJourneyAction extends BaseJourneyAction{
	
	@Override
	protected String getLinkToCurrentPage(HttpServletRequest req) {
		return "";
	}
	
	private static Logger log = Logger.getLogger(AnalyzeJourneyAction.class);

	@Override
	public ActionCommand execute(ActionMapping mapping, FormBean formBean, HttpServletRequest req, HttpServletResponse res){

		List<AnalyzeProducerCallsMapBean> callsList = new ArrayList<AnalyzeProducerCallsMapBean>();
		
		String journeyName = req.getParameter("pJourneyName");
		Journey journey = null;
		try{
			journey = getJourneyManager().getJourney(journeyName);
		}catch(NoSuchJourneyException e){
			throw new IllegalArgumentException("Journey with name "+journeyName+" not found.");
		}
		AnalyzeProducerCallsMapBean overallCallsMap = new AnalyzeProducerCallsMapBean(journey.getName()+" - TOTAL");
		callsList.add(overallCallsMap);
		req.setAttribute("journeyName", journeyName);
		
		List<CurrentlyTracedCall> tracedCalls = journey.getTracedCalls();
		for (CurrentlyTracedCall tc : tracedCalls){
			if (tc==null){
				log.warn("TracedCall is null!");
				continue;
			}
			AnalyzeProducerCallsMapBean singleCallMap = new AnalyzeProducerCallsMapBean(tc.getName());
			for (TraceStep step : tc.getRootStep().getChildren()){
				addStep(step, singleCallMap, overallCallsMap);
			}
			callsList.add(singleCallMap);
			//System.out.println("\t"+tc.get)
		}
		
		
		//req.setAttribute("overallCallsMap", overallCallsMap);
		req.setAttribute("callsList", callsList);
		return mapping.success();
	}
	
	private void addStep(TraceStep step, AnalyzeProducerCallsMapBean... maps){
		String producerName = step.getProducer() == null ? 
				"UNKNOWN" : step.getProducer().getProducerId();
		for (AnalyzeProducerCallsMapBean map : maps){
			map.addProducerCall(producerName,  step.getDuration());
		}
		for (TraceStep childStep : step.getChildren()){
			addStep(childStep, maps);
		}
		
	}
}
@@ -615,42 +615,140 @@
 ito.
-webui.bean.AnalyzeProducerCallsMap
+core.stats.TimeUnit;%0Aimport net.java.dev.moskito.webui.bean.AnalyzeProducerCallsMapBean;%0Aimport net.java.dev.moskito.webui.bean.Unit
 Bean
@@ -1213,16 +1213,70 @@
 n%3E();%0A%09%09
+TimeUnit currentUnit = getCurrentUnit(req).getUnit();%0A
 %0A%09%09Strin
@@ -2096,16 +2096,29 @@
 ep(step,
+ currentUnit,
  singleC
@@ -2400,16 +2400,35 @@
 ep step,
+ TimeUnit timeUnit,
  Analyze
@@ -2650,16 +2650,40 @@
 rName,  
+timeUnit.transformNanos(
 step.get
@@ -2693,16 +2693,17 @@
 ation())
+)
 ;%0A%09%09%7D%0A%09%09
@@ -2750,16 +2750,16 @@
 ren())%7B%0A
-
 %09%09%09addSt
@@ -2771,16 +2771,26 @@
 ildStep,
+ timeUnit,
  maps);%0A

fix duration calculation in currently selected unit