<%@ page session="true" contentType="text/html; charset=UTF-8" %>
<%@ page import="com.edeal.frontline.*" %> 
<%@ page import="java.util.*" %> 
<%@ page import="com.edeal.frontline.controller.ControllerServlet" %> 
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="com.edeal.frontline.navigation.*" %>
<%@ page import="java.util.Map.Entry" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.NumberFormat" %>
<%@ page import="com.edeal.frontline.controller.actions.PopulationMassEmailingAction" %>
<%@ page import="com.edeal.frontline.controller.actions.BackgroundAjaxAction" %>
<%@ page import="com.edeal.frontline.controller.actions.GetMassEmailingStatsAction" %>

<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>

<script type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/edeal/libraries/json.org/json2/json2.js"></script>

<link rel="stylesheet" type="text/css" href="<fl:webapp/>/css/common_css.jsp"/>	
<style type="text/css">
	.tabFdBlanc td, .tabFdBleu td {
		border-right: 1px solid #ccc;
		padding-top: 3px;
		padding-right: 5px;
		padding-bottom: 3px;
		padding-left: 5px;
		vertical-align:	middle;
	}
	.tabFdBlanc {
		font-size: 12px;
		font-family: Arial,Verdana,sans-serif;
		color: rgb(0, 0, 0);
		background-color: rgb(255, 255, 255);
	}
	.tabFdBleu {
		font-size: 12px;
		font-family: Arial,Verdana,sans-serif;
		color: rgb(0, 0, 0);
		background-color: rgb(235, 244, 246);
	}
	.tabFdBlanc, .tabFdBleu {
		border: 0px;
		border-bottom: 1px solid #ccc;
		border-left: 1px solid #ccc;
	}
	.titreTable {
		font-family: Arial, Verdana, sans-serif;
		font-weight: bold;
		font-size: 12px;
	}
</style>

<%
final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
ArrayList<Integer> arrEmailingStats = (ArrayList<Integer>)request.getAttribute(GetMassEmailingStatsAction.ARR_EMAILING_STATS); 
ArrayList<Integer> arrEmailingStatsHavingSameRegrouping = (ArrayList<Integer>)request.getAttribute(GetMassEmailingStatsAction.ARR_EMAILING_STATS_HAVING_SAME_REGROUPING); 
HashMap<String, Integer> hmapLinkStatsRegrouped = (HashMap<String, Integer>)request.getAttribute(GetMassEmailingStatsAction.HMAP_LINK_STATS_REGROUPED); 
HashMap<String, Integer> hmapLinkStats = (HashMap<String, Integer>)request.getAttribute(GetMassEmailingStatsAction.HMAP_LINK_STATS); 
HashMap<String, Integer> hmapLinkStatsHavingSameRegrouping = (HashMap<String, Integer>)request.getAttribute(GetMassEmailingStatsAction.HMAP_LINK_STATS_HAVING_SAME_REGROUPING);

Date emlSentDate = (Date) request.getAttribute(GetMassEmailingStatsAction.EML_SENT_DATE);

String period = request.getParameter(GetMassEmailingStatsAction.PERIOD_ANALYSE);

NumberFormat floatFormat = FlLocale.getInstance(session).getDecimalFormat("#,##0");
String strDate = "";
if (emlSentDate != null) {
	strDate = Utils.formatToWeb(session, emlSentDate, false, "datetime" );
}		

FlRequest flRequest = new FlRequest(request);
FlContext context = flRequest.getContext();

String id = (String)request.getParameter("id");

Date lastStatsDate = (Date)flRequest.getAttribute(GetMassEmailingStatsAction.LAST_STATS_DATE);

String strLastStatsDate = "N/A";
if (lastStatsDate != null) {
	strLastStatsDate = Utils.formatToWeb(session, lastStatsDate, false, "datetime" );
}
		
//Synthèse de l'e-mailing
float volumeCible = 0, hardBounces = 0, softBounces = 0, presumesArrives = 0, ouvert = 0, sentError = 0, desabonne = 0, spam = 0;

if (arrEmailingStats != null && !arrEmailingStats.isEmpty()) {
	try {
		hardBounces = arrEmailingStats.get(0);//EstNbFatalError
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'hardBounces'", ex);
	}
	try {
		softBounces = arrEmailingStats.get(1);//EstNbTempError
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'softBounces'", ex);
	}
	try {
		presumesArrives = arrEmailingStats.get(2);//EstNbSent
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'presumesArrives'", ex);
	}
	try {
		ouvert = arrEmailingStats.get(3);//EstNbOpened
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'ouvert'", ex);
	}
	//inclure dans le comptage des présumés arrivés les ouverts
	presumesArrives = presumesArrives + ouvert;
	
	try {
		sentError = arrEmailingStats.get(4);//EstNbSentError
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'erreur envoi'", ex);
	}
	try {
		volumeCible = arrEmailingStats.get(5);//EstNbTotal
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'total'", ex);
	}
	try {
		desabonne = arrEmailingStats.get(6);//EstNbUnsubscribed
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'nombre de désabonné'", ex);
	}
	try {
		spam = arrEmailingStats.get(7);//EstNbSpam
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'nombre de spam'", ex);
	}
	logger.debug("volumeCible vaut" + volumeCible);
}	

//Synthèse de l'e-mailing en %
float perHardBounces = 0, perSoftBounces = 0, perPresumesArrives = 0, perOuvert = 0, perSentError = 0, perDesabonne = 0, perSpam = 0;

if (volumeCible > 0) {
	try {
		perHardBounces = hardBounces / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perHardBounces'", ex);
	}
	try {
		perSoftBounces = softBounces / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSoftBounces'", ex);
	}
	try {
		perPresumesArrives = presumesArrives / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perPresumesArrives'", ex);
	}
	try {
		perOuvert = ouvert / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perOuvert'", ex);
	}
	try {
		perSentError = sentError / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSentError'", ex);
	}
	try {
		perDesabonne = desabonne / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perDesabonne'", ex);
	}
	try {
		perSpam = spam / volumeCible * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSpam'", ex);
	}
}

//Moyenne des e-mailings de même regroupement 
float volumeCibleRg = 0, hardBouncesRg = 0, softBouncesRg = 0, presumesArrivesRg = 0, ouvertRg = 0, sentErrorRg = 0, desabonneRg = 0, spamRg = 0;

if (arrEmailingStatsHavingSameRegrouping != null && !arrEmailingStatsHavingSameRegrouping.isEmpty()) {
	try {
		hardBouncesRg = arrEmailingStatsHavingSameRegrouping.get(0);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de récupérer 'hardBouncesRg'", ex);
	}
	try {
		softBouncesRg = arrEmailingStatsHavingSameRegrouping.get(1);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de récupérer 'softBouncesRg'", ex);
	}
	try {
		presumesArrivesRg = arrEmailingStatsHavingSameRegrouping.get(2);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de récupérer 'presumesArrivesRg'", ex);
	}
	try {
		ouvertRg = arrEmailingStatsHavingSameRegrouping.get(3);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de récupérer 'ouvertRg'", ex);
	}
	try {
		sentErrorRg = arrEmailingStatsHavingSameRegrouping.get(4);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'erreur envoi'", ex);
	}
	try {
		volumeCibleRg = arrEmailingStatsHavingSameRegrouping.get(5);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'total'", ex);
	}
	try {
		desabonneRg = arrEmailingStatsHavingSameRegrouping.get(6);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'desabonneRg'", ex);
	}
	try {
		spamRg = arrEmailingStatsHavingSameRegrouping.get(7);
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'spamRg'", ex);
	}
	//volumeCibleRg = hardBouncesRg + softBouncesRg + presumesArrivesRg ;//+ ouvertRg;
	logger.debug("VolumeCibleRg vaut" + volumeCibleRg);
}	

//Moyenne des e-mailings de même regroupement en %	
float perHardBouncesRg = 0, perSoftBouncesRg = 0, perPresumesArrivesRg = 0, perOuvertRg = 0, perSentErrorRg = 0, perDesabonneRg = 0, perSpamRg = 0;
float evolHardBounces = 0, evolSoftBounces = 0, evolPresumesArrives = 0, evolOuvert = 0,evolCible = 0, evolSentError = 0, evolDesabonne = 0, evolSpam = 0;

if (volumeCibleRg > 0) {
	try {
		perHardBouncesRg = hardBouncesRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perHardBouncesRg'", ex);
	}
	try {
		perSoftBouncesRg = softBouncesRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSoftBouncesRg'", ex);
	}
	try {
		perPresumesArrivesRg = presumesArrivesRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perPresumesArrivesRg'", ex);
	}
	try {
		perOuvertRg = ouvertRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perOuvertRg'", ex);
	}
	try {
		perSentErrorRg = sentErrorRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSentErrorRg'", ex);
	}
	try {
		perDesabonneRg = desabonneRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perDesabonneRg'", ex);
	}
	try {
		perSpamRg = spamRg / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perSpamRg'", ex);
	}
	try {
		evolCible = (volumeCible - volumeCibleRg) / volumeCibleRg * 100;
	} catch (Exception ex) {
		logger.error("Une Exception s'est produite quand on tente de calculer 'perOuvertRg'", ex);
	}
	
	if (hardBouncesRg > 0) {
		try {
			evolHardBounces = (hardBounces - hardBouncesRg) / hardBouncesRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolHardBounces'", ex);
		}
	}
	
	if (softBouncesRg > 0) {
		try {
			evolSoftBounces = (softBounces - softBouncesRg) / softBouncesRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolSoftBounces'", ex);
		}
	}
	
	if (presumesArrivesRg > 0) {
		try {
			evolPresumesArrives = (presumesArrives - presumesArrivesRg) / presumesArrivesRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolPresumesArrives'", ex);
		}
	}
	
	if (ouvertRg > 0) {
		try {
			evolOuvert = (ouvert - ouvertRg) / ouvertRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolOuvert'", ex);
		}
	}
	
	if (sentErrorRg > 0) {
		try {
			evolSentError = (sentError - sentErrorRg) / sentErrorRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolOuvert'", ex);
		}
	}
	
	if (desabonneRg > 0) {
		try {
			evolDesabonne = (desabonne - desabonneRg) / desabonneRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolDesabonne'", ex);
		}
	}
	
	if (spamRg > 0) {
		try {
			evolSpam = (spam - spamRg) / spamRg * 100;
		} catch (Exception ex) {
			logger.error("Une Exception s'est produite quand on tente de calculer 'evolSpam'", ex);
		}
	}
}
%>

<table class="container">
	<tr>
		<td class="dataLabel">
			<fl:getMessage code="L707B" alt="Envoyé le"/> : <%= strDate %>, <fl:getMessage code="F60C9" alt="Calculé le"/> : <%= strLastStatsDate %>
		</td>
	</tr>
</table>

<div class="edealGrid">
<div id="grid-data">
<table>
	<caption class="titreTable"><fl:getMessage code="L70B9" alt="Statistiques d\'envois"/></caption>
	<thead>
		<tr>
			<th width="25%"></th>
			<th width="30%" colspan="2"><fl:getMessage code="L817A" alt="Synthèse du faxing"/></th>
			<th width="30%" colspan="2"><fl:getMessage code="L817B" alt="Moyenne des faxings de même regroupement"/></th>
			<th width="15%"><fl:getMessage code="L7072" alt="Evolution"/></th>
		</tr>
	</thead>
	<tbody>
		<tr class="tabFdBleu">
			<td><fl:getMessage code="L706D" alt="Volume de la cible"/></td>	
			<td align="right"><%= (int)volumeCible %></td>	
			<td></td>	
			<td align="right"><%= (int)volumeCibleRg %></td>	
			<td></td>	
			<td align="right"><%= floatFormat.format(evolCible) %>%</td>	
		</tr>
		<%-- Numéros fax erronés --%>
		<tr class="tabFdBlanc">
			<td><div style="display:hidden" id="bgstatusMessageSentError"></div><a href="javascript:doBackgroundAjax('<fl:webapp/>/create_population_faxing_ajax.fl?id=<%= id %>&poptype=<%= PopulationMassEmailingAction.PopulationType.SENTERROR %>',null,'bgstatusMessageSentError')"><fl:getMessage code="L816F" alt="Numéro fax erroné"/></a></td>	
			<td align="right"><%= (int)sentError %></td>	
			<td align="right"><%= floatFormat.format(perSentError) %>%</td>	
			<td align="right"><%= (int)sentErrorRg %></td>	
			<td align="right"><%= floatFormat.format(perSentErrorRg) %>%</td>	
			<td align="right"><%= floatFormat.format(evolSentError) %>%</td>	
		</tr>
		<%-- Erreur --%>
		<tr class="tabFdBleu">
			<td><div style="display:hidden" id="bgstatusMessageHardBounces"></div><a href="javascript:doBackgroundAjax('<fl:webapp/>/create_population_faxing_ajax.fl?id=<%= id %>&poptype=<%= PopulationMassEmailingAction.PopulationType.HARDBOUNCES %>',null,'bgstatusMessageHardBounces')"><fl:getMessage code="L8171" alt="Non réponse"/></a></td>	
			<td align="right"><%= (int)hardBounces %></td>	
			<td align="right"><%= floatFormat.format(perHardBounces) %>%</td>	
			<td align="right"><%= (int)hardBouncesRg %></td>	
			<td align="right"><%= floatFormat.format(perHardBouncesRg) %>%</td>	
			<td align="right"><%= floatFormat.format(evolHardBounces) %>%</td>	
		</tr>
		<%-- Non réponse --%>
		<tr class="tabFdBlanc">
			<td><div style="display:hidden" id="bgstatusMessageSoftBounces"></div><a href="javascript:doBackgroundAjax('<fl:webapp/>/create_population_faxing_ajax.fl?id=<%= id %>&poptype=<%= PopulationMassEmailingAction.PopulationType.SOFTBOUNCES %>',null,'bgstatusMessageSoftBounces')"><fl:getMessage code="L8170" alt="Transmission impossible"/></a></td>	
			<td align="right"><%= (int)softBounces %></td>	
			<td align="right"><%= floatFormat.format(perSoftBounces) %>%</td>	
			<td align="right"><%= (int)softBouncesRg %></td>	
			<td align="right"><%= floatFormat.format(perSoftBouncesRg) %>%</td>	
			<td align="right"><%= floatFormat.format(evolSoftBounces) %>%</td>		
		</tr>
		<%-- Présumés arrivés --%>
		<tr class="tabFdBleu">
			<td><div style="display:hidden" id="bgstatusMessagePresumesArrives"></div><a href="javascript:doBackgroundAjax('<fl:webapp/>/create_population_faxing_ajax.fl?id=<%= id %>&poptype=<%= PopulationMassEmailingAction.PopulationType.OPENED %>',null,'bgstatusMessagePresumesArrives')"><fl:getMessage code="L8172" alt="Présumé arrivé"/></a></td>	
			<td align="right"><%= (int)ouvert %></td>	
			<td align="right"><%= floatFormat.format(perOuvert) %>%</td>	
			<td align="right"><%= (int)ouvertRg %></td>	
			<td align="right"><%= floatFormat.format(perOuvertRg) %>%</td>	
			<td align="right"><%= floatFormat.format(evolOuvert) %>%</td>		
		</tr>
	</tbody>	
</table>
<br>
</div>
</div>
<br/>

<%-- Graphe --%>
<table class="container" cellpadding="1" align="center">
	<tr>
		<td>
			<center>
				<fl:sdi code="STDNBFAXSENT"/>
			</center>
		</td>
	</tr>	
	<tr class="ligne_separation">
		<td colspan="4"></td>
	</tr>
</table>