<!-- Author : Jagbir Singh -->
<!-- Debug : Jagbir Singh -->
<!-- Tested by : Ajinkya bilolikar, Siddhesh Surve -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">




<%@page import="java.util.ArrayList"%>

<%@ page language="java" import="java.sql.*, javax.sql.DataSource, javax.naming.*" errorPage="" %>

<%

String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String userId = request.getParameter("userId");



						//same as in calculate.jsp
DataSource pool;
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
String spclQuery = null;
ArrayList<String> totalEnergy  = new ArrayList<String>();	
ArrayList<String> actvEnergy  = new ArrayList<String>();
ArrayList<String> numsteps = new ArrayList<String>();
ArrayList<String> dataDate = new ArrayList<String>();
ArrayList<String> dateLabel = new ArrayList<String>();
ArrayList<String> numLabel = new ArrayList< String>();
ArrayList<String> sleepStart = new ArrayList< String>();
ArrayList<String> sleepEnd = new ArrayList< String>();
ArrayList<Integer> numSleepHrs = new ArrayList<Integer>();
ArrayList<String> lyingStart = new ArrayList< String>();
ArrayList<String> lyingEnd = new ArrayList< String>();
ArrayList<Integer> numLyingHrs = new ArrayList<Integer>();
ArrayList<Integer> varEnergy = new ArrayList<Integer>();
double finEnergy = 0;
double actvEner =0;
double sedEnergy =0;
double modEnergy =0;
double vigEnergy =0;
double veryVigEnergy =0;
int sedPerEnergy = 0;
int modPerEnergy = 0;
int vigPerEnergy = 0;
int veryVigPerEnergy = 0;

int phyHrs =0, sedHrs=0, modHrs =0, vigHrs = 0, veryVigHrs =0, totalDur = 0;
int elements = 0;

ResultSet rs = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
int j=0, k=0, y= 0;;
try{
	InitialContext ctx = new InitialContext();
	pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
	if (pool == null)
        throw new SQLException("Unknown DataSource 'jdbc/TestDB'");
	conn = pool.getConnection();
	
	
		
		spclQuery = "SELECT totEnerExp, numSteps, dataOfData, physActivityDur, sedenTary, modActivity, vigrousActivity, veryVigActivity  from dataInfo where dataOfData > ? and dataOfData < ? and userId = ?";
	       pstmt = conn.prepareStatement(spclQuery);   
	       	pstmt.setString(1, startDate);
	       	pstmt.setString(2, endDate);
	       	pstmt.setString(3, userId);
	        rs = pstmt.executeQuery();
	       
	        String query2 = "SELECT totEnerExp from dataInfo where dataOfData > ? and dataOfData < ? and physActivityDur = 1 and userId = ?";
	        pstmt2 = conn.prepareStatement(query2);   
	       	pstmt2.setString(1, startDate);
	       	pstmt2.setString(2, endDate);
	       	pstmt2.setString(3, userId);
	      
	        rs2 = pstmt2.executeQuery();
	        
	        String query3 = "SELECT sleepTime, lyingDownTime, dataOfData from dataInfo where userId = ?";
	        pstmt3 = conn.prepareStatement(query3);   
	       	pstmt3.setString(1, userId);
	     
	        rs3 = pstmt3.executeQuery();
	        
	       while(rs.next())
	       	{
	 
	    		totalEnergy.add(rs.getString("totEnerExp"));
	    	
	    		numsteps.add(rs.getString("numSteps"));
	    		
    		
    		String data = rs.getString("dataOfData");
    		data.replace(' ', '_');
  
    		dataDate.add(data);
    		if(rs.getInt("physActivityDur")==1){
    			
    			phyHrs++;
    			
    		}
			if(rs.getInt("sedenTary")==1){
    			
    			sedHrs++;
    			sedEnergy = sedEnergy + Double.parseDouble(totalEnergy.get(elements));
    		}
			if(rs.getInt("modActivity")==1){
		
			modHrs++;
			modEnergy = modEnergy + Double.parseDouble(totalEnergy.get(elements));
			}
			
			if(rs.getInt("vigrousActivity")==1){
			vigHrs++;
			vigEnergy = vigEnergy + Double.parseDouble(totalEnergy.get(elements));
			}
			
			if(rs.getInt("veryVigActivity")==1){
			veryVigHrs++;
			veryVigEnergy = veryVigEnergy + Double.parseDouble(totalEnergy.get(elements));
			}
    		
			totalDur = phyHrs + sedHrs;
			elements++;
    	
	    	 }
	        k = 0;
	       while(rs2.next()){
	    	   
	    	   actvEnergy.add(rs2.getString("totEnerExp"));
	    	    actvEner = actvEner + Double.parseDouble(actvEnergy.get(k));
	    	   k++;
	       }

			elements = 0;
	       elements = totalEnergy.size();

	        k = 0;
	       while(elements != 1){
	    	   finEnergy = finEnergy + Double.parseDouble(totalEnergy.get(k));
	    	   	elements--;
	    	   	k++;
	       }
	       
	   	k =1;
		int flag = 0, flag2=0;
		y = 1;
		while(rs3.next()){
			
			if(rs3.getInt("sleepTime")==1 && flag==0)
			{
				sleepStart.add(rs3.getString("dataOfData"));
				flag = 1;
			}
			if(flag == 1){
				k++;
			}
			if(rs3.getInt("sleepTime")==0 && flag ==1 && k >= 20){
				sleepEnd.add(rs3.getString("dataOfData"));
				flag = 0;
				numSleepHrs.add(k);
			
			}
			
			if(rs3.getInt("lyingDownTime")==1 && flag2==0)
			{
				lyingStart.add(rs3.getString("dataOfData"));
				flag2 = 1;
			}
			if(flag2 == 1){
				y++;
			}
			if(rs3.getInt("sleepTime")==0 && flag2 ==1 && y>=20){
				lyingEnd.add(rs3.getString("dataOfData"));
				flag2 = 0;
				numLyingHrs.add(y);
				//y =1;
			}
			
		}
	
	       sedPerEnergy = ((int)((sedEnergy/finEnergy)*100));
	       
	       modPerEnergy = ((int)((modEnergy/finEnergy)*100));
	       vigPerEnergy = ((int)((vigEnergy/finEnergy)*100));
	       veryVigPerEnergy = ((int)((veryVigEnergy/finEnergy)*100));
	       
	       varEnergy.add(0, veryVigPerEnergy);
	       varEnergy.add(1, sedPerEnergy);
	       varEnergy.add(2, modPerEnergy);
	       varEnergy.add(3, vigPerEnergy);
	      
	       
int size = dataDate.size();
int start = size/6;
	
	String index;
	  index = dataDate.get(0);
	  index = index.toString();
      		

	dateLabel.add(index);
	
	index = dataDate.get(start);
	
	dateLabel.add(index);
	
	index = dataDate.get(2*start);
	
	dateLabel.add(index);

	index = dataDate.get(3*start);
		dateLabel.add(index);

	index = dataDate.get(4*start);
	
	dateLabel.add(index);

	index = dataDate.get(size -1);
	
	dateLabel.add(index);

	float num =0;
	String steps;
	
	for(int i =0; i<6; i++){
		
		num =0;
		for( j=0 + (i*start) ; j<start + (i*start); j++){
			 steps = numsteps.get(j);
		
			 num = num + Float.parseFloat(steps);
			
		}
	steps = Float.toString(num);
		numLabel.add(steps);
		
	}
	

	rs.close();
    pstmt.close();
    rs2.close();
    pstmt2.close();
    rs3.close();
    pstmt3.close();
    conn.close();  
    
    
	
}catch(SQLException se){
	se.printStackTrace();
}catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
 }finally{
    //finally block used to close resources
    try{
       if(pstmt!=null)
          pstmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
       if(conn!=null)
       conn.close();
    }catch(SQLException se){
       se.printStackTrace();
    }//end finally try
 } //end try


%>




<html>
<head>
<style>
table.table3{
    font-family:Arial;
    font-size: 18px;
    font-style: normal;
    font-weight: normal;
    text-transform: uppercase;
    letter-spacing: -1px;
    line-height: 1.7em;
    text-align:center;
    
    margin-left:auto;
    margin-right:auto;
    width: 80%;
}
.table3 thead th{
    padding:6px 10px;
    text-transform:uppercase;
    color:#444;
    font-weight:bold;
    text-shadow:1px 1px 1px #fff;
    border-bottom:5px solid #444;
}
.table3 thead th:empty{
    background:transparent;
    border:none;
}
.table3 thead :nth-child(2),
.table3 tfoot :nth-child(2){
    background-color: #1589FF;
}
.table3 tfoot :nth-child(2){
    -moz-border-radius:0px 0px 0px 5px;
    -webkit-border-bottom-left-radius:5px;
    border-bottom-left-radius:5px;
}
.table3 thead :nth-child(2){
    -moz-border-radius:5px 0px 0px 0px;
    -webkit-border-top-left-radius:5px;
    border-top-left-radius:5px;
}
.table3 thead :nth-child(3),
.table3 tfoot :nth-child(3){
    background-color: #1589FF;
}
.table3 thead :nth-child(4),
.table3 tfoot :nth-child(4){
    background-color: #1589FF;
}
.table3 thead :nth-child(5),
.table3 tfoot :nth-child(5){
    background-color: #1589FF;
}
.table3 thead :nth-child(5){
    -moz-border-radius:0px 5px 0px 0px;
    -webkit-border-top-right-radius:5px;
    border-top-right-radius:5px;
}
.table3 tfoot :nth-child(5){
    -moz-border-radius:0px 0px 5px 0px;
    -webkit-border-bottom-right-radius:5px;
    border-bottom-right-radius:5px;
}
.table3 tfoot td{
    font-size:38px;
    font-weight:bold;
    padding:15px 0px;
    text-shadow:1px 1px 1px #fff;
}
.table3 tbody td{
    padding:10px;
}
.table3 tbody tr:nth-child(4) td{
    font-size:18px;
    font-weight:normal;
}
.table3 tbody td:nth-child(even){
    background-color:#444;
    color:#444;
    border-bottom:1px solid #444;
    background:-webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.39, rgb(189,189,189)),
        color-stop(0.7, rgb(224,224,224))
        );
    background:-moz-linear-gradient(
        center bottom,
        rgb(189,189,189) 39%,
        rgb(224,224,224) 70%
        );
    text-shadow:1px 1px 1px #fff;
}
.table3 tbody td:nth-child(odd){
    background-color:#555;
    color:#f0f0f0;
    border-bottom:1px solid #444;
    background:-webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.39, rgb(85,85,85)),
        color-stop(0.7, rgb(105,105,105))
        );
    background:-moz-linear-gradient(
        center bottom,
        rgb(85,85,85) 39%,
        rgb(105,105,105) 70%
        );
    text-shadow:1px 1px 1px #000;
}
.table3 tbody td:nth-last-child(1){
    border-right:1px solid #222;
}
.table3 tbody th{
    color:#1589FF;
    text-align:right;
    padding:0px 10px;
    border-right:1px solid #aaa;
}
.table3 tbody span.check::before{
    content : url(../images/check2.png)
}
</style>
</head>
<body >
<br><br>
<form name="home" " action="welcome.jsp">
<input type="image" src="home.jpg" alt="Submit" align="left" width="70" height="60">
</form>
<form name="graph" " action="logout.jsp">
<input type="image" src="logout.jpg" alt="Submit" align="right" width="60" height="60">
</form>
<form name="statistics" " action="calculate.jsp">
<input type = "hidden" value="<%=userId%>" name="userId">
<input type = "hidden" value="<%=startDate%>" name="StartDate">
<input type = "hidden" value="<%=endDate%>" name="EndDate">
<input type="image" src="calculate.jpg" alt="Submit" align="left" width="80" height="80">
</form>
<br><br><br><br>

<form method="post" action="update.jsp">

<table class="table3">


 <tr>
    <td>Total Energy (Calories)</td>
    <td>
    <%=finEnergy%>
    </td>
  </tr>
  
  
  <tr>
    <td>Active Energy (Calories)</td>
    <td> 
    <%=actvEner%>
    </td>
  </tr>
 
  <tr>
    <td>Total Sleep TIme (min)</td>
    <td><%=k%></td>
  </tr>
 
  <tr>
    <td>Total Lying Down Time (min)</td>
    <td><%=y%></td>
  </tr>
  
 
  
  <tr>
    <td>Physical Activity Duration (min)</td>
    <td>
    <%=phyHrs%></td>
  </tr>
  
  <tr>
    <td>Sedentary Activity Duration (min)</td>
    <td>
    <%=sedHrs%>
    </td>
  </tr>
  
  <tr>
    <td>Sedentary Energy Expenditure (Calories)</td>
    <td>
    <%=sedEnergy%>
    </td>
  </tr>

  <tr>
    <td>Moderate  Activity Duration (min)</td>
    <td>
    <%=modHrs%>
    </td>
  </tr>
  
  <tr>
    <td>Moderate  Energy Expenditure (Calories)</td>
    <td>
    <%=modEnergy%>
    </td>
  </tr>
  
  <tr>
    <td>Vigrous  Activity Duration (min)</td>
    <td>
    <%=vigHrs%>
    </td>
  </tr>
 
 <tr>
    <td>Vigrous  Energy Expenditure (Calories)</td>
    <td>
    <%=vigEnergy%>
    </td>
  </tr>
 
  
  <tr>
  <td>Very Vigrous Activity Duration (min)</td>
    <td>
    <%=veryVigHrs%>
    </td>
  </tr>
  
  <tr>
  <td>Very Vigrous Energy Expenditure (Calories)</td>
    <td>
    <%=veryVigEnergy%>
    </td>
  </tr>


</table>
</form>
</body>
</html>