package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
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.DataBaseMethods;
import main_package.LoanInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import other_package.DataBaseMethodsOther;


@ManagedBean(name="emiDetailJson")
@ViewScoped

public class EmiDetailJsonBean implements Serializable
{
	String json;
	Connection conn=DataBaseConnection.JAVAConnection();
	
	public EmiDetailJsonBean() 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		String shareId = params.get("userid");
		if(shareId.contains("'"))
		{
			shareId="";
		}
		
		DataBaseMethods dbm= new DataBaseMethods();
     ArrayList<LoanInfo> list=new ArrayList<>();
     list =  new DataBaseMethodsOther().allLoanList(shareId);
     
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();

		     if(list.size()>0)
		     {
		    	 for(LoanInfo uu : list)
				   {
					   JSONObject obj = new JSONObject();
					   
					   double  penaltyAmount=0,penaltyPaid=0; 
					   ArrayList<LoanInfo> loanEMIList;
					   
					   if(uu.getLoanType().equalsIgnoreCase("Group Loan") || uu.getLoanType().equalsIgnoreCase("grouploan"))
					   {
						   obj.put("loanNo",uu.getSubLoanNo());
						     penaltyAmount=dbm.calculatePenaltyAndChequeBounceCharges(uu.getSubLoanNo(), "Penalty", "grouploan", conn);
							 penaltyPaid=dbm.totalPaidPenaltyAndChequeBounceCharges(uu.getSubLoanNo(), "Penalty", conn);
							   loanEMIList=dbm.loanInstallment("grouploan",uu.getSubLoanNo(), conn);
					   }
					   else
					   {
						   obj.put("loanNo",uu.getLoanNo());
						     penaltyAmount=dbm.calculatePenaltyAndChequeBounceCharges(uu.getLoanNo(), "Penalty", "personalloan", conn);
							 penaltyPaid=dbm.totalPaidPenaltyAndChequeBounceCharges(uu.getLoanNo(), "Penalty", conn);
							   loanEMIList=dbm.loanInstallment("personalloan",uu.getLoanNo(), conn);
					   }
					   
					
						double penaltyDue=penaltyAmount-penaltyPaid;
						penaltyDue=Double.parseDouble(new DecimalFormat("##.##").format(penaltyDue));
						
						 int   currentDueEmi=0;double currentDueAmount=0;
							
				    	
				    	  
					   		for(int l=0;l<loanEMIList.size();l++)
					   		{
					   			Date dt=new Date();
					   			if(loanEMIList.get(l).getStatus().equals("Paid"))
					   			{
					   				loanEMIList.get(l).setColorStatus("paid");
					   			}
					   			else
					   			{
					   				if(loanEMIList.get(l).getInstallDate().before(dt))
					   				{
					   					loanEMIList.get(l).setColorStatus("due");
					   					if(loanEMIList.get(l).getColorStatus().equalsIgnoreCase("due"))
					   					{
					   						currentDueEmi+=1;
					   						currentDueAmount+=loanEMIList.get(l).getDueAmount();
					   					}
					   				}
					   				else
					   				{
					   					loanEMIList.get(l).setColorStatus("upcoming");
					   				}
					   			}
					   		}
				    	   
					   		
					   
					   obj.put("sno",uu.getSno());
					   obj.put("requestDate",uu.getStrRequestDate());
					   obj.put("loanType",dbm.loanValueByLoanLabel(uu.getLoanType()));
					   obj.put("loanAmount",uu.getLoanAmount());
					   obj.put("loanInterest",uu.getLoanInterest());
					   obj.put("loanTerms",uu.getLoanTerm());
					   obj.put("loanEmi",uu.getInstallAmount());
					   obj.put("tableName",uu.getTableName());
					   
					  /* double	penaltyPaid=dbm.totalPaidPenaltyAndChequeBounceCharges(uu.getLoanNo(), "Penalty");
						System.out.println(penaltyPaid);
					  obj.put("paidPenalty",penaltyPaid);*/
					  
					  obj.put("duePenalty",penaltyDue);
					  obj.put("currentDueEmi",currentDueEmi);
				    	 obj.put("currentDueAmount", currentDueAmount);
				    	 
					   arr.add(obj);
				   }
					
		     }

		     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;
	}
	
}
