package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.text.DecimalFormat;
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 org.json.simple.JSONArray;
import org.json.simple.JSONObject;


@ManagedBean(name="MyNidhiLoanCalculatorJson")
@ViewScoped

public class LoanCalculatorJsonBean implements Serializable
{
	String json;
	double totalIntrest,discount,emiPayment;
	String loanType,calStatus;
	DataBaseMethods obj=new DataBaseMethods();
	
	public LoanCalculatorJsonBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		Double loanAmount =Double.valueOf(params.get("loanAmount"));
		Double interstRate= Double.valueOf(params.get("interstRate"));
		Double terms = Double.valueOf(params.get("duration"));
		Integer frequency = Integer.valueOf(params.get("frequency"));
		String status= params.get("status");
		if(status.contains("'"))
		{
			status="";
		}
		
		if((loanAmount==null || loanAmount.equals(""))&&(interstRate==null || interstRate.equals(""))&&(terms==null || terms.equals("")) &&(frequency==null || frequency.equals("")))
       {
    	   
       }
       else
	     {
	       if(status.equalsIgnoreCase("flat"))
		     {
		   		totalIntrest=discount=0;
		   		emiPayment=obj.calculateLoanEmiForCalculator(loanAmount,terms,interstRate,frequency,"both");  	
		   		if(Double.valueOf(loanAmount)==0.0)
		   		{
		   			emiPayment=0;
		   		}
		   		
		   		if(emiPayment>0)
		   		{
		   			double totalPayment=emiPayment*terms;
		   			double falseInterest=totalPayment-loanAmount;
		   			double trueInterest=(loanAmount*interstRate*terms)/(100*frequency);
		   			trueInterest=Math.round(trueInterest);
		   			discount=(int) (falseInterest-trueInterest);
		   			discount=Math.round(discount);	
		   			
		   			
		   		}
		   		
		   		totalIntrest=Double.parseDouble(new DecimalFormat("##.##").format((emiPayment*terms)-loanAmount));
		   	
		    }
			
	       
	       else
	       {
		   		totalIntrest=0;
		   		loanType="reducing";
		   		emiPayment=obj.calculateLoanEmi(loanAmount,terms, interstRate, frequency);
		   		if(loanAmount==0)
		   		{
		   			emiPayment=0;
		   		}
	   		
		   		if(emiPayment>0)
		   		{
		   				double totalPayment=emiPayment*terms;
		   				double falseInterest=totalPayment-loanAmount;
		   				double trueInterest=new DataBaseMethods().totalInterestForDiscount(loanAmount,terms,interstRate,frequency,emiPayment);
		   				trueInterest=Math.round(trueInterest);
		   				discount=(int) (falseInterest-trueInterest);
		   				discount=Math.round(discount);
		   		}
	   		
	   		totalIntrest=new DataBaseMethods().totalInterestForDiscount(loanAmount,terms,interstRate,frequency,emiPayment);
	   		totalIntrest=Math.round(totalIntrest);
	       }
	       
	     } 
       
     
     
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
			 JSONObject obj = new JSONObject();
			 
			 
			 obj.put("emiPayment",emiPayment);
			 obj.put("interest",totalIntrest);
			 obj.put("discount",discount);
	    	  
		     arr.add(obj); 
		      
			      
try {
	conn.close();
} catch (Exception e) {
	// TODO: handle exception
}
		    mainobj.put("track", arr);

		json=mainobj.toJSONString();
	
	}
	
	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;
	}

}