<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../../EgTrackerAJSON.jsp"%>
<%@ page import = "java.util.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.io.*" %>

<%@ page import = "com.eg.EgUsersInfo" %>
<%@ page import = "com.eg.EgInstallInfo" %>
<%@ page import = "com.eg.IniFile" %>
<%@ page import = "com.egurkha.util.DateUtil" %>
<%@ page import = "egurkha.reporter.*" %>
<%@ page import = "com.egurkha.ini.*" %>
<%@ page import = "java.net.URLEncoder" %>
<%@ page import = "com.eg.EgConfigInfo"%>
<%@ page import = "com.objectplanet.chart.*"%>

<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="net.sf.json.JSONArray"%>
	

 <%
	String drilldownenabled = request.getParameter("drilldownenabled");
	String er_timeline = request.getParameter("er_timeline");
	String datePickFrom = request.getParameter("datePickFrom");
	String datePickTo = request.getParameter("datePickTo");
	String er_mode = request.getParameter("er_mode");
	String fwdPage = request.getParameter("fwdPage");
	boolean isNamedUsers = EgInstallInfo.isnamedUserLicense;

	if(drilldownenabled == null){
		drilldownenabled="yes";
	}
		if(er_mode == null){
		er_mode="3D";
	}
	String fontStyleDbl = "";
	String fontStyleDblSingl = "";
	if(drilldownenabled.equalsIgnoreCase("yes"))
	{
		fontStyleDbl = " multi_font";
		fontStyleDblSingl = "multi_font";
	}
	else
	{
		fontStyleDbl = "";
		fontStyleDblSingl = "";
	}
	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	com.eg.IniFile egUI = new com.eg.IniFile(s_iniFile_dir + uiFile);
	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	ErEsxVMutils erEsxUtils = new ErEsxVMutils();
	com.egurkha.ini.IniUI ui = (com.egurkha.ini.IniUI)com.egurkha.ini.IniUI.getInstance();
	EgUsersInfo usersInfo = new EgUsersInfo();

	String colors = egUI.getValue("REPORTER_GRAPH_PARAMS","SAMPLECOLORS");

	StringTokenizer stColor =  new StringTokenizer(colors,",");
	String col = "";
	Vector colrlist = new Vector();
	int colcount =0;

	while(stColor.hasMoreTokens())
	{
		col = stColor.nextToken();
		colrlist.add(col.substring(col.indexOf("#")+1,col.length()));
	}
	if(er_timeline == null  || er_timeline.equals(""))
	{
		int windowCount = usersInfo.getWindowValue();
		if(windowCount !=0 && windowCount<7)
		{
			er_timeline=Integer.toString(windowCount);
			er_timeline=er_timeline+" "+"days";
		}else if(windowCount==7){
			er_timeline="1 week";
		}
		else if(windowCount !=0 && windowCount>=8){
		  er_timeline="2 weeks";
		}
	}
	String currentDateFormat = egUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	HashMap mappedtimeline = erEsxUtils.getTimeline();

	Vector v_time = (Vector)mappedtimeline.get("timehour");
	Vector v_min = (Vector)mappedtimeline.get("timemin");
	String dispDate = null;
	String comFormat  = null;
	String startDate="";
	String endDate="";
	String disable="";
	String enmin = "";
	String strmin = "";
	String s_en_hr = "";
	String s_st_hr = "";
	String StrDate = "";
	String s_st_time = "";
	String s_en_time = "";
	String stsec = "";
	String ensec = "";
	String format1="";
	ArrayList datesLabel=new ArrayList();
	JSONObject barChartDataObj = new JSONObject();
	if(er_timeline != null && !er_timeline.equals("Any"))
	{
		disable ="disabled";//for disable timeline calender pop up
		s_st_time = "";
		s_en_time = "";
		ErEsxVMutils ervm = new ErEsxVMutils();
		HashMap mapTimeLine = ervm.getDateTime(er_timeline,"none");
		startDate = (String)mapTimeLine.get("dbstartdate");
		endDate = (String)mapTimeLine.get("dbenddate");
		s_st_time = (String)mapTimeLine.get("calStartDate");
		s_en_time = (String)mapTimeLine.get("calEndDate");
		s_st_hr = (String)mapTimeLine.get("startHour");
		s_en_hr = (String)mapTimeLine.get("endHour");
		strmin = (String)mapTimeLine.get("startMin");
		enmin = (String)mapTimeLine.get("endMin");
		stsec = (String)mapTimeLine.get("startSec");
		ensec = (String)mapTimeLine.get("endSec");	 
		datePickFrom = startDate;
		String endDateformat = DateUtil.changeDateFormat(endDate,"dd-MM-yyyy",currentDateFormat);
		String startDateformat = DateUtil.changeDateFormat(datePickFrom,"dd-MM-yyyy",currentDateFormat);
		barChartDataObj.put("startDate",startDateformat);
		barChartDataObj.put("endDate",endDateformat);

	}
	else
	{
		startDate = DateUtil.changeDateFormat(datePickFrom,"MMM dd,yyyy","dd-MM-yyyy");
		DateFormat df = new SimpleDateFormat("MMM dd,yyyy");
        	Date date_1 = df.parse(datePickTo);
        	Calendar cal_temp_date = new GregorianCalendar();
        	cal_temp_date.setTime(date_1);
        	cal_temp_date.add(Calendar.DATE,1);
        	java.util.Date dt_temp = cal_temp_date.getTime();
        	datePickTo = df.format(dt_temp);
 
		endDate = DateUtil.changeDateFormat(datePickTo,"MMM dd,yyyy","dd-MM-yyyy");
	}


	Map aListData3 = usersInfo.getTotalUser("Total",startDate,endDate);
	String errormsg="No Data found";
	boolean showNamedUsers = true;
	
	if(aListData3!=null && aListData3.size()>0)
	{
		Iterator itr = aListData3.keySet().iterator();
		format1 =DateUtil.getDateFormatForTimeLinechart(currentDateFormat);
		int k=0;
		JSONArray dataSetArr=new JSONArray();
		JSONArray xLabel=new JSONArray();
		JSONArray xSubLabel=new JSONArray();
		JSONArray dataSet=new JSONArray();
		JSONObject data=new JSONObject();
		while(itr.hasNext())
		{
			String timeformat="";
			String st_key = (String)itr.next();
			String value = "";
			if(st_key!=null) 
				value = (String)aListData3.get(st_key);

			timeformat = DateUtil.changeDateFormat(st_key.trim(),"yyyy-MM-dd HH:mm:ss",currentDateFormat);
			String selDate = st_key+"^"+timeformat;
			timeformat=DateUtil.changeDateFormat(timeformat,currentDateFormat,format1);
			xLabel.add(timeformat);
			JSONObject dataValObj=new JSONObject();
			if(!isNamedUsers)
			{
				dataValObj.put("y",Double.valueOf(value));	
				dataValObj.put("name",timeformat);	
				dataValObj.put("id",selDate);
				dataSet.add(dataValObj);
				showNamedUsers = true;
			}
			else
			{	
				showNamedUsers = false;
				dataSet.add(Double.valueOf(value));
			}
		}
		data.put("data",dataSet);
		data.put("pointWidth",new Integer(25));
		dataSetArr.add(data);

		int chartHeight=360;
		int chartWidth=860; //660;
		
		barChartDataObj.put("xAxisLabel",xLabel);
		barChartDataObj.put("xAxisSubLabel",xSubLabel);
		barChartDataObj.put("yAxisLabel","Total Count");
		barChartDataObj.put("xAxisLabelName","Days ("+format1+")");
		barChartDataObj.put("DataSet",dataSetArr);
		barChartDataObj.put("enableClick",new Boolean(showNamedUsers));
		barChartDataObj.put("chartType","column");
		barChartDataObj.put("title","");
		barChartDataObj.put("chartHeight",new Integer(chartHeight));
		barChartDataObj.put("chartWidth",new Integer(chartWidth));
		barChartDataObj.put("LegendPosition","top");
		barChartDataObj.put("legend", "false");
		barChartDataObj.put("barLabelAngle", "90");
		barChartDataObj.put("sampleLabelAngle", "90");
		barChartDataObj.put("rangeAxisLabelAngle", "90");
		barChartDataObj.put("mytooltip", new Boolean(true));
	}
	else
	{
		barChartDataObj.put("errormsg",errormsg);
	}

	if(fwdPage != null && fwdPage.equalsIgnoreCase("licensePdf"))
	{
		Chart chart=null;
		egurkha.reporter.builder.builderErChart  builderChart = new egurkha.reporter.builder.builderErChart(barChartDataObj,"bar");
		chart=builderChart.getFinalChart();
		chart.setLabelAngle("barLabelAngle", 270);
		chart.setLabelAngle("rangeAxisLabelAngle", 270);
		chart.setLabel("rangeAxisLabel", "Total Count");
		chart.setLabel("sampleAxisLabel", "Days ("+format1+")");
		chart.setValueLabelsOn(true);;
		String width="1050";
		String height="280";
		String thisLanguage="US";

		OutputStream outStream = null;
		response.setContentType("image/gif;charset=UTF-8");
		outStream = response.getOutputStream();
		String className="com.objectplanet.chart.ChartApplet";
		ErChart erChart = new ErChart(className,chart,true,thisLanguage,"bar");
		erChart.writeImageToStream(Integer.parseInt(width),Integer.parseInt(height),outStream);
		outStream.flush();
		outStream.close();
	}
	
	out.println(barChartDataObj);

  %>
