package json_package;



import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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 javax.servlet.http.HttpSession;



import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@ManagedBean(name="cibilApiHitJson")
@ViewScoped

public class CibilApiHitJson implements Serializable
{
	String json;
	
	public CibilApiHitJson() 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		String userId = params.get("userId");
		String branchId = params.get("branchId");
		String panNo = params.get("panNo");
		String name= params.get("name");
		String mobileNo= params.get("mobileNo");
		String gender= params.get("gender");
		String cibilType=params.get("cibilType");
		
		//System.out.println(cibilType);
		
		
		if(userId.contains("'"))
		{
			userId="";
		}
		
		
		  JSONObject mainobj = new JSONObject();
				JSONArray arr=new JSONArray();
			   
 			    	 JSONObject obj = new JSONObject();
 			    	
 			    	//int apiBal=dbm.checkApiBalance(cibilType);
 			   	
					/*
					 * String status="true"; String score = "", link="";
					 * if(cibilType.equalsIgnoreCase("EXPERIAN")) { CibilApiMethod db = new
					 * CibilApiMethod(); CibilApiMethod.CibilResponse response =
					 * db.hitExperian(name, panNo, mobileNo, userId); //System.out.println("Score: "
					 * + response);
					 * 
					 * // extract the score and link score = response.getScore(); link =
					 * response.getLink();
					 * 
					 * if(link.trim().equals("")) {
					 * status="THERE IS TECHNICAL ISSUE !!! PLEASE TRY AGAIN LATER"; }
					 * //System.out.println("Score: " + score); //System.out.println("Link: " +
					 * link);
					 * 
					 * } else if(cibilType.equalsIgnoreCase("CIBIL TRANSUNION")) {
					 * System.out.println("Score: "); CibilApiMethod db = new CibilApiMethod();
					 * CibilApiMethod.CibilResponse response =db.hitCibil(name, panNo, mobileNo,
					 * userId, gender);
					 * 
					 * // extract the score and link score = response.getScore(); link =
					 * response.getLink();
					 * 
					 * //System.out.println("Score: " + score); //System.out.println("Link: " +
					 * link);
					 * 
					 * if(link.trim().equals("")) {
					 * status="THERE IS TECHNICAL ISSUE !!! PLEASE TRY AGAIN LATER"; }
					 * 
					 * } else { status="THIS OPTION IS UNDER DEVELOPMENT."; }
					 * 
					 * 
					 * 
					 * 
					 * 
					 * if(status.equalsIgnoreCase("true")) { obj.put("status", status);
					 * obj.put("apiBal", 0); obj.put("url", link); obj.put("score", score); } else {
					 * obj.put("status", status); obj.put("apiBal", 0); obj.put("url", "");
					 * obj.put("score", 0); }
					 */
 				    	
 			    	
			    	 
				      arr.add(obj);
				    	
	
			    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;
	}
	
}
