<!DOCTYPE html>

<%@ include file = "ErTracker.jsp"%>
<%@ page import="com.egurkha.ini.*" %>
<%@ page import="com.eg.*" %>
<%@ page import="com.eg.EgInstallInfo" %>
<%@ page import="com.eg.EgMappingInfo" %>	
<%@ page import="com.eg.EgReportInfo" %>	
<%@ page import = "com.egurkha.util.DateTimeTokenizer" %>
<%@ page import = "com.egurkha.util.DateUtil" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.egurkha.util.StringUtils"%>
<%@ page import="net.sf.json.*"%>

<%
	ErUtils utils = new ErUtils();
	EgTablesInfo tablesInfo = new EgTablesInfo("manager");
	EgDbInfo dbInfo=new EgDbInfo("manager");
	IniUI iniui = IniUI.getInstance();
	UserRegistry mregistry = UserRegistry.getInstance();
	User muser = mregistry.getUser(session.getId());
	String thisLanguage = iniui.getDefaultLanguage();
	List lst = new LinkedList();
	if (muser!=null)
	{
		String userLang = muser.getLanguage();
		if (userLang!=null && !userLang.equalsIgnoreCase("NULL"))
		{
			thisLanguage=userLang;
		}
	}	
	EgMappingInfo mappingInfo=new EgMappingInfo(thisLanguage);
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	String uiFile = "eg_ui.ini";
	com.eg.IniFile egUI = new com.eg.IniFile(s_iniFile_dir + uiFile);
	String currentDateFormat = ErReportUtil.getCurrentDateFormat();
	String titleToDisplay = request.getParameter("er_RmDispName");
	String starthr = request.getParameter("starthr");
	String endhr = request.getParameter("endhr");
	String stmin = request.getParameter("startmin");
	String enmin = request.getParameter("endmin");
	String st_date = request.getParameter("startdate");
	String end_date = request.getParameter("enddate");
	String er_desktop = request.getParameter("er_desktop");
	String er_compType = request.getParameter("er_compType");
	String st_component = request.getParameter("er_component");
	String er_user = request.getParameter("er_user");
	String er_info = request.getParameter("er_info");
	String printOption = request.getParameter("printOption");
	String str_pdf = request.getParameter("is_pdf");

	EgTimeZoneConverter converterStart=null;
	EgTimeZoneConverter converterEnd = null;

	converterStart = new EgTimeZoneConverter(st_date+"  "+starthr+":"+stmin,muser);
	converterEnd = new EgTimeZoneConverter(end_date+"  "+endhr+":"+enmin,muser);
	String st_Time = converterStart.getDateTime();
	String end_Time = converterEnd.getDateTime();
	lst.add(mappingInfo.getTextDescription("reporter.common.Application Billing")+" - "+st_component);
	lst.add(mappingInfo.getTextDescription("reporter.thin.Component Type")+" : "+mappingInfo.getComponentTypeDescription(er_compType));
	
	String ssd="Start Date : "+st_Time+"  "+"End Date : "+end_Time;
	lst.add('"'+ssd+'"');
%>
<!Doctype html>
<head>
<title>&nbsp;<egui:intnl text="reporter.executive.REPORTER"/>&nbsp;DATA</title>
<meta name="Author" content="">
<meta  charset="UTF-8">

<!--<link rel="stylesheet" type="text/css" href="/final/styles/eGLightTheme.css" />-->
<link rel="stylesheet" type="text/css" href="/final/styles/eGLightThemeIcons.css" />
<link rel="stylesheet" type="text/css" href="../../final/reporter/styles/printpagestyle.css" />
<!--<link rel="stylesheet" type="text/css" href="/final/resources/css/ext-eg.css" />-->


<style type="text/css" media="print">
#printbt{ display:none}
</style>

<!--<script language="JavaScript1.2" src="../reporter/scripts/er_reporter.js"></script>
<script language="JavaScript1.2" src="../reporter/scripts/er_images.js"></script>
<script language="JavaScript1.2" src="../scripts/<%=script_src%>"></script>-->
<!--<script language="JavaScript1.2">
DD_roundies.addRule('.box', 5, true);
</script>-->
<script language="JavaScript1.2">

function saveInCSVFormat()
{
	window.location.href="../servlet/egurkha.reporter.SaveInCSVFormat","CSVFormat","width=1012,height=603,left=0,top=105,scrollbars=1,scrollbars=yes,resizable=yes";
}
function closeParendWidow()
{
	if(window.parent.Ext.getCmp('maximizeDataWindowLightBox')!=undefined)
		window.parent.Ext.getCmp('maximizeDataWindowLightBox').doClose();
	else if(window.parent.Ext.getCmp('popupprint')!=undefined)
		window.parent.Ext.getCmp('popupprint').doClose(); 
	else if(window.parent.Ext.getCmp('dataWin')!=undefined)
		window.parent.Ext.getCmp('dataWin').doClose(); 
}
</script>
</head>

<body  style="padding:30px;background-color:#ffffff" style="padding:0px 0px 10px 10px;margin-top:40px">
<div class="printHeaderBox" style="padding-right:40px;position:fixed;top:0px;">
<%
	if (!StringUtils.isNull(titleToDisplay))
	{
%>
			<div class="printHeaderBoxTitle"><%=titleToDisplay%></div>
<%
	}
	else
	{
%>
			<div class="printHeaderBoxTitle"><egui:intnl text="reporter.executive.Application Billing"/></div>
<%
	}
	if(printOption!=null && printOption.equalsIgnoreCase("data"))
	{
%>
			<div style="float:right;margin:0px 0px 0px 5px"><img class="mouse" src="/final/images/Light/newPopupWindowClose.png" border="0" onClick="javascript:closeParendWidow();" /></div>
<%
	}
			if(str_pdf==null || !str_pdf.equalsIgnoreCase("yes"))
			{
%>
			<div style="float:right;margin:0px 5px 0px 5px"><img class="iconPrinter mouse" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" border="0" onClick="javascript:print();" alt='<egui:intnl text="reporter.common.PRINT"/>' title='<egui:intnl text="reporter.common.PRINT"/>'/></div>
			
<%			}

	if(printOption!=null && printOption.equalsIgnoreCase("data"))
	{
%>
			<div style="float:right;margin:0px 5px 0px 5px"><img class="iconCsv mouse" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" border="0" onClick="javascript:saveInCSVFormat();" alt='<egui:intnl text="reporter.common.CSV"/>' title='<egui:intnl text="reporter.common.CSV"/>'/></div>
<%
	}
%>
</div>
<div class="printOuterBoxNoBorder">
<div class="printSelectionFields" ><egui:intnl text='<%="reporter.common.Component"%>'/>- <span class="bold"><%=st_component%></span></div>
<div class="printSelectionFields">Timeline - <span class="bold"><%=st_Time%> to <%=end_Time%></span></div>
</div>
 <div class="clearDiv" style="border-top:1px solid #b0b1b2;">&nbsp;</div>

<%
	List resultList = (ArrayList)session.getAttribute("MULTIMEASURE_RESULT_MAP");
	String maxValue = (String)session.getAttribute("MULTIMEASURE_MAX");
	String Qstring ="";
	if(resultList!=null && resultList.size()>0)
	{
		String showLegend = request.getParameter("showLegend");
		long lowerLimit = 0;
		long higherLimit = 0;
		JSONArray dataAry = new JSONArray();
		JSONObject timelineObj = new JSONObject();
		EgReportInfo reportInfo = new EgReportInfo("manager");	
		String ChartColor = reportInfo.getValue("APPLICATION_REPORT", "ChartColor");
		StringTokenizer colorToken = new StringTokenizer(ChartColor,",");
		ArrayList colorLst = new ArrayList();
		while(colorToken.hasMoreTokens())
		{
			colorLst.add(colorToken.nextToken());
		}
		int colorCnt=0;
		ArrayList totalDate = new ArrayList();
		SimpleDateFormat format=null;
		String pattern = "MM/dd/yyyy HH:mm:ss";	
		if(currentDateFormat != null){
			currentDateFormat=currentDateFormat+" HH:mm:ss";
			format = new SimpleDateFormat(currentDateFormat);
		}
		else
			format = new SimpleDateFormat(pattern);

		HashMap multiMesUnitMp = (HashMap)session.getAttribute("multiMesUnit");
		ArrayList multiMesUnitLst = new ArrayList();
		for(int i=0;i<resultList.size();i++)
		{
			HashMap resultMap = (HashMap)resultList.get(i);
			Set sets = resultMap.entrySet();
			Iterator iterator = sets.iterator();
			while (iterator.hasNext())
			{
				Map.Entry entry = (Map.Entry) iterator.next();
				String test_measure = (String) entry.getKey();
				ArrayList dataList = (ArrayList) entry.getValue();
				JSONObject dataObj = new JSONObject();
				
				ArrayList lowerValueList = (ArrayList)dataList.get(0); // lowerLimit
				long lowerLimit_temp = Long.parseLong(lowerValueList.get(0)+"");// lowerLimit value
				if(lowerLimit==0 || lowerLimit_temp<lowerLimit)
					lowerLimit = lowerLimit_temp;
				ArrayList higherValueList = (ArrayList)dataList.get(dataList.size()-1); // higherLimit
				long higherLimit_temp = Long.parseLong(higherValueList.get(0)+"");// higherLimit value
				if(higherLimit==0 || higherLimit_temp>higherLimit)
					higherLimit = higherLimit_temp;
				for(int j=0;j<dataList.size();j++)
				{
					ArrayList datLst = (ArrayList)dataList.get(j);
					String dateVal = (String)datLst.get(0);
					if(!totalDate.contains(dateVal))
						totalDate.add(dateVal);
				}

				String testS = test_measure.substring(0,test_measure.indexOf(":"));
				String measureS = test_measure.substring(test_measure.indexOf(":")+1,test_measure.length());
				
				String unitmeasure=tablesInfo.getUnitForMeasure(testS.trim(),measureS.trim());
				if(unitmeasure==null || unitmeasure.equals(""))
				{
					String str_measureName = dbInfo.getMeasureForColumn(testS,measureS);
					unitmeasure = tablesInfo.getUnitForMeasure(testS,str_measureName);

				}
				if((unitmeasure==null || unitmeasure.length()==0) && multiMesUnitMp!=null)
				{
					unitmeasure = (String)multiMesUnitMp.get(measureS.toUpperCase());
				}
				if(unitmeasure!=null && unitmeasure.length()>0)
				{
					unitmeasure = " ("+unitmeasure+")";
				}
				else
					unitmeasure = "";

				if(!multiMesUnitLst.contains(unitmeasure))
					multiMesUnitLst.add(unitmeasure);
				if(testS.indexOf("_DDD")>-1)
					testS = testS.substring(0,testS.lastIndexOf("_"));
				measureS = mappingInfo.getMeasureDescription(testS,measureS);
				testS = mappingInfo.getTestDescription(testS);
				JSONArray dataListAry = new JSONArray();
				for(int v=0;v<dataList.size();v++)
				{
					JSONArray valueAry = new JSONArray();
					ArrayList valueList = (ArrayList)dataList.get(v);
					/*for(int j=0;j<valueList.size();j++)
					{
						String data = (String)valueList.get(j);
						double dataVal = Double.parseDouble(data);
						if(!data.equals("-"))
							valueAry.add(dataVal);
					}*/
					String dateVal = (String)valueList.get(0);
					valueAry.add(dateVal);
					String data = (String)valueList.get(1);
					double dataVal = Double.parseDouble(data);
					if(!data.equals("-"))
						valueAry.add(dataVal);
					if(valueAry.size()==valueList.size())
						dataListAry.add(valueAry);
				}
				//dataObj.put("name",testS+"/"+measureS+unitmeasure);
				dataObj.put("name",measureS+unitmeasure);
				dataObj.put("color",(String)colorLst.get(colorCnt));
				dataObj.put("yAxis",multiMesUnitLst.indexOf(unitmeasure));
				dataObj.put("type","line");
				dataObj.put("data",dataListAry);
				dataAry.add(dataObj);
				colorCnt++;
			}
		}
		JSONArray columnList = new JSONArray();
		JSONArray gridDataArry = new JSONArray();
		JSONArray gridDataValArray = null;
		columnList.add("Date");
		Collections.sort(totalDate);
		for(int i=0;i<dataAry.size();i++)
		{
			JSONObject dataObj = (JSONObject)dataAry.get(i);
			columnList.add((String)dataObj.get("name"));
			JSONArray dataList = (JSONArray)dataObj.get("data");
			for(int j=0;j<totalDate.size();j++)
			{
				String tableDate = (String)totalDate.get(j);
				if(i==0)
				{
					gridDataValArray = new JSONArray();
					java.util.Date dateVal=new java.util.Date(Long.parseLong(tableDate));
					//time zone convertion
					String s_check = format.format(dateVal).toString();
					String formatdisp = DateUtil.changeDateFormat(s_check,"MMM dd, yyyy HH:mm:ss",muser.getUserDateFormat()+" HH:mm:ss");
					converterStart.convertProcess(formatdisp,muser,false);
					s_check = converterStart.getDateTime();
					
					gridDataValArray.add(s_check);
				}
				else
					gridDataValArray = (JSONArray)gridDataArry.get(j);

				boolean isNotPresent=true;
				for(int k = 0; k<dataList.size();k++)
				{
					JSONArray dataValAry=(JSONArray)dataList.get(k);
					String cmpredate=dataValAry.get(0)+"";
					if(tableDate.equalsIgnoreCase(cmpredate))
					{
						gridDataValArray.add(utils.round(Double.parseDouble(dataValAry.get(1)+""),4));
						isNotPresent=false;
						break;
					}
				}
				if(isNotPresent)
					gridDataValArray.add("-");
				
				if(i==0)
				{
					gridDataArry.add(gridDataValArray); 
				}
			}
		}
		
		// 5 mins by adding and subtracting 300000
		// fix of problem in easy chart
		if(lowerLimit==higherLimit)
		{
			lowerLimit = lowerLimit-300000;
			higherLimit = higherLimit+300000;
		}
		timelineObj.put("userDateFormat",currentDateFormat);
		timelineObj.put("TimeFormatOut","HH:mm");
		timelineObj.put("Chart","line");
		timelineObj.put("chartType","timeline");
		timelineObj.put("lowerLimit",lowerLimit+"");
		timelineObj.put("timezoneOffsetValue","-330");
		timelineObj.put("higherLimit",higherLimit+"");
		//timelineObj.put("yLabel",Ylabel);
		for(int i=0;i<multiMesUnitLst.size();i++)
		{
			timelineObj.put("yAxisLabel"+(i+1),(String)multiMesUnitLst.get(i));
		}
		timelineObj.put("timeScale","30");
		timelineObj.put("fontName","Roboto");
		timelineObj.put("legendFontSize","11");
		timelineObj.put("labelFontSize","10");
		timelineObj.put("xLabel","Time(HH:mm)");
		timelineObj.put("LineWidth","2");
		timelineObj.put("LegendPosition","right");
		timelineObj.put("minRange","0.0");
		timelineObj.put("maxRange",maxValue);
		timelineObj.put("showdisconnectedline","No");
		timelineObj.put("DataSet",dataAry);
		timelineObj.put("showLegend",showLegend);
		timelineObj.put("spacingLeft",10);
		timelineObj.put("spacingTop",40);
		timelineObj.put("marginTop",50);
		timelineObj.put("fromPopup","yes");
		timelineObj.put("combineChartType","combinedChart");
	
		session.setAttribute("MULTIMEASURE_GRAPH",timelineObj);
		String mngrIP = new IniFile(new EgConfigInfo("manager").getDbFile()).getValue("DB_PROPERTIES","ManagerUrl");
		String servlet = "../servlet/com.eg.ErMultiMeasureReportC;jsessionid="+session.getId()+"?er_height=300&er_width=850&er_view=IMAGE";

		if(printOption!=null && printOption.equalsIgnoreCase("data"))
		{
			lst.add(" ");
			Vector v_measure_csv = new Vector();
			List list_data = new LinkedList();
%>
	<div class="newTable">
	<table align="center" valign="middle" width="500px" cellspacing="0" cellpadding="0" border="0" class="printTable">
		<thead>
		<tr>
<%
			for(int i=0;i<columnList.size();i++)
			{
				v_measure_csv.add(columnList.get(i));
%>
			<th  class="printTableHeader2" style="text-align:center;"><%=columnList.get(i)%></th>
<%
			}
%>
		</tr>
		</thead>
		
		<tbody>
<%
		for(int i=0;i<gridDataArry.size();i++)
		{	
			JSONArray gridDataArray = (JSONArray)gridDataArry.get(i);
%>
			<tr>
<%
			for(int j=0;j<gridDataArray.size();j++)
			{
				list_data.add(gridDataArray.get(j));
%>
				<td style="text-align:center;"><%=gridDataArray.get(j)%></td>
<%
			}
%>
			</tr>
<%
		}
		Hashtable h_tabrc = new Hashtable();
		Hashtable h_tab = new Hashtable();
		h_tabrc.put(v_measure_csv,list_data);
		String s_title = "Application Billing - "+st_component;
		h_tab.put(s_title,h_tabrc);
		lst.add(h_tab);
%>
		</tbody>
	</table>
	</div>
<%
		
		}
		else
		{


%>	<table class="newimg" align="center" width="100%" cellspacing="0" cellpadding="0" border="0" > 
		<tr>
			<td align="center" width="100%"><img src="<%=servlet%> "scrolling="no" frameborder="0" align="center" frameborder="0"/></td>
		</tr>
	</table>		
		
<%
		}
	}
	else
	{
%>
<table width='100%' height='100%'>
	<tr>
		<td align='center' valign='middle'><img src='/images/Light/nomeasures_custom.png' /></td>
	</tr>
</table>
<%
		lst.add(mappingInfo.getTextDescription("reporter.common.No measures available"));
	}
	if(lst!= null && !lst.isEmpty())
	{
		session.setAttribute("list",lst);
	}
	session.setAttribute("ScheduleCSVData",lst);

%>

</body>
</html>