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.Calendar;
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 javax.faces.model.SelectItem;

import main_package.DataBaseConnection;
import main_package.UserInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import report_package.DataBaseReportMethods;

@ManagedBean(name="userNotificationJson")
@ViewScoped
public class UserNotificationJsonBean implements Serializable
{
	String json;
	Date startDate=new Date();
	Date endDate=new Date();
	String loanMsg="", depositMsg="";
	
	public UserNotificationJsonBean() throws ParseException 
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
	
		String shareNo=params.get("userid");
		String branchStr = params.get("branchId");
		int branchId = Integer.valueOf(branchStr);
		
		if(shareNo.contains("'"))
		{
			shareNo="";
		}
		
		
		ArrayList<UserInfo> emiList=new ArrayList<>();
		String[] selectedCenter = null;
		ArrayList<SelectItem> centerList=new ArrayList<>();
		
		emiList=new DataBaseReportMethods().allUpcomingEMI(branchId,startDate,endDate,"loan", conn, selectedCenter, centerList, false, false);
      // System.out.println(emiList.size()+"========"+tempdt);
        for(UserInfo ll: emiList)
        {
        	if(ll.getShareHolderNo().equalsIgnoreCase(shareNo))
        	{
             	loanMsg="Dear "+ll.getLoanNo()+" - "+ll.getFirst_applicantName()+", Your Loan EMI Rs. "+ll.getAmount() +" is Upcoming On "+ll.getEntryDateStr();
        	}
        }
        
        emiList=new ArrayList<>();
        emiList=new DataBaseReportMethods().allUpcomingEMI(branchId,startDate,endDate,"rd", conn, selectedCenter, centerList, false, false);
        // System.out.println(emiList.size()+"========"+tempdt);
          for(UserInfo ll: emiList)
          {
          	if(ll.getShareHolderNo().equalsIgnoreCase(shareNo))
          	{
               	depositMsg="Dear "+ll.getLoanNo()+" - "+ll.getFirst_applicantName()+", Your RD EMI Rs. "+ll.getAmount() +" is Upcoming On "+ll.getEntryDateStr();
          	}
          }
		
          System.out.println(loanMsg);
          System.out.println(depositMsg);
          	JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
				
			 JSONObject obj = new JSONObject();
			   
			  
			   obj.put("loanMsg",loanMsg);
			   obj.put("depositMsg",depositMsg);
			   
			   arr.add(obj);
		     
			      

		    mainobj.put("track", arr);

		json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
		  
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	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;
	}
}
