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="MyNidhiRdFdCalculatorJson")
@ViewScoped

public class RdFdCalculatorJsonBean implements Serializable
{
	String json;
	double totalIntrest,discount,maturityValue;
	String loanType,calStatus;
	DataBaseMethods obj=new DataBaseMethods();
	
	public RdFdCalculatorJsonBean() 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		Double amount =Double.valueOf(params.get("amount"));
		Double interstRate= Double.valueOf(params.get("interstRate"));
		Integer terms = Integer.valueOf(params.get("duration"));
		Integer frequency = Integer.valueOf(params.get("frequency"));
		String status= params.get("status");
		if(status.contains("'"))
		{
			status="";
		}

		if((amount==null ||amount.equals(""))&&(interstRate==null || interstRate.equals(""))&&(terms==null || terms.equals("")) &&(frequency==null || frequency.equals("")))
       {
    	   
       }
       else
		 {
	    	maturityValue=obj.findInterest(terms,amount,interstRate,status,frequency);
	   		if(status.equals("RD"))
	   		{
	   			totalIntrest=maturityValue-(terms*amount);
	   		}
	   		else
	   		{
	   			totalIntrest=maturityValue-amount;
	   		}
	   		maturityValue=Double.parseDouble(new DecimalFormat("##.##").format(maturityValue));
	   		totalIntrest=Double.parseDouble(new DecimalFormat("##.##").format(totalIntrest));
      } 
       
     
     
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
			 JSONObject obj = new JSONObject();
			 
			 
			 obj.put("maturityValue",maturityValue);
			 obj.put("interest",totalIntrest);
	    	  
		     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;
	}

}
