package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import main_package.DataBaseConnection;
import main_package.UserInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import report_package.DataBaseReportMethods;

@ManagedBean(name="dailyCollectionJson")
@ViewScoped

public class DailyCollectionJsonBean implements Serializable
{
	String json,loanAmount,rdAmount,fdAmount,savingAmount,totalAmount, ddAmount, gdAmount,misAmount;
	Date startDate, endDate;
	double total, penaltyAmount, fileChargeAmount, insFeeAmount;
	DataBaseReportMethods dbm=new DataBaseReportMethods();
	DatabaseMethods1 dbm1= new DatabaseMethods1();
	
	public DailyCollectionJsonBean() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
		Connection conn = DataBaseConnection.JAVAConnection();
		String sDate=params.get("startDate");
		String eDate=params.get("endDate");
		String branchStr = params.get("branchId");
		int branchId = Integer.valueOf(branchStr);
		/*String mobile=params.get("mobile");
		String pwd=params.get("pwd");*/
		
		 if(sDate.contains("'") || eDate.contains("'"))
			{
				sDate="";
				eDate="";
			}
		 
		ArrayList<UserInfo> list=new ArrayList<>();
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		if(sDate!=null && eDate!=null && !sDate.equals("") && !eDate.equals(""))
		{
			startDate = (Date)formatter.parse(sDate);
			endDate = (Date)formatter.parse(eDate);
		}
		
		loanAmount=String.valueOf(dbm.dailyEmiCollectionBetweenDate(branchId,startDate, endDate, "both", "both",conn, ""));
		if(loanAmount==null)
		{
			loanAmount="0";
		}
		
		rdAmount=String.valueOf(dbm.dailyRdFdCollectionBetweenDate(branchId,startDate, endDate, "both", "RD",conn, ""));
		if(rdAmount==null)
		{
			rdAmount="0";
		}
		
		fdAmount=String.valueOf(dbm.dailyRdFdCollectionBetweenDate(branchId,startDate, endDate, "both", "FD",conn, ""));
		if(fdAmount==null)
		{
			fdAmount="0";
		}
		
		ddAmount=String.valueOf(dbm.dailyRdFdCollectionBetweenDate(branchId,startDate, endDate, "both", "DD",conn, ""));
		if(ddAmount==null)
		{
			ddAmount="0";
		}
		
		misAmount=String.valueOf(dbm.dailyRdFdCollectionBetweenDate(branchId,startDate, endDate, "both", "MIS",conn, ""));
		if(misAmount==null)
		{
			misAmount="0";
		}
		savingAmount=dbm1.savingCollection(branchId, startDate,endDate);
		if(savingAmount==null)
		{
			savingAmount="0";
		}
		
		penaltyAmount=dbm1.dailyIncomeCollection(branchId,startDate,endDate, "Receive Penalty");
		fileChargeAmount=dbm1.dailyIncomeCollection(branchId,startDate,endDate, "File Charge");
		insFeeAmount=dbm1.dailyIncomeCollection(branchId,startDate,endDate, "Insurance Fee");

try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}


		total = Double.valueOf(loanAmount)+Double.valueOf(rdAmount)+Double.valueOf(fdAmount)+Double.valueOf(ddAmount)+Double.valueOf(misAmount)+Double.valueOf(savingAmount)
				+penaltyAmount+fileChargeAmount+insFeeAmount;
		totalAmount = String.valueOf(total);
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
				
			
					   JSONObject obj = new JSONObject();
					   
					   
					   obj.put("Loan Amount",loanAmount);
					   obj.put("DD Amount",ddAmount);
					   obj.put("RD Amount",rdAmount);
					   obj.put("FD Amount",fdAmount);
					   obj.put("MIS Amount",misAmount);
					   obj.put("Saving Amount",savingAmount);
					   obj.put("Penaly Amount",penaltyAmount);
					   obj.put("File Charge Amount",fileChargeAmount);
					   obj.put("Insurance Fees Amount",insFeeAmount);
					   obj.put("Total Amount",totalAmount);
					   
					   
					   arr.add(obj);
			
				/*String status="";
		    	 if(i==1)
		    	 {
		    		 status="Registered Successfully";
		    	 }
		    	 else if(i==-1)
		    	 {
		    		 status="Please enter a unique email id.";
		    	 }
		    	 else
		    	 {
		    		status="Something went wrong.";
		    	 }*/
		    	 
		    	  
			      

		    mainobj.put("track", arr);

		json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
		  
	}
	
	public void renderJson() throws IOException {
 	    FacesContext facesContext = FacesContext.getCurrentInstance();
 	    ExternalContext externalContext = facesContext.getExternalContext();
 	    externalContext.setResponseContentType("application/json");
 	    externalContext.setResponseCharacterEncoding("UTF-8");
 	    externalContext.getResponseOutputWriter().write(json);
 	    facesContext.responseComplete();
 	    
 	    
 	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
	
}
