package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
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.DataBaseAutoCompAndDuplicate;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import other_package.DataBaseMethodsOther;
import other_package.ReportInfo;

@ViewScoped
@ManagedBean(name="savingStatementJson")
public class SavingStatementJson implements Serializable
{
	String json;
	double totalDebit,totalCredit,closingBalance,opBalance;
 	ArrayList<ReportInfo> list;
 	Date startDate=new Date(),endDate=new Date();
 	DecimalFormat df=new DecimalFormat("##.##");
 	DataBaseMethodsOther dbmp=new DataBaseMethodsOther();
	
	public SavingStatementJson() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		String accNo = params.get("accNo");
		String sDate = params.get("sDate");
		String eDate = params.get("eDate");
		
		 if(accNo.contains("'") || sDate.contains("'") || eDate.contains("'"))
			{
		 accNo="";
				sDate="";
				eDate="";
			}
	 	
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		startDate=formatter.parse(sDate);
		endDate=formatter.parse(eDate);
		
		list=dbmp.savingAccountReport(accNo,startDate,endDate);
		opBalance=dbmp.openingBalance(accNo,startDate);
		Collections.sort(list);
		calculate();
     
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();

		     if(list.size()>0)
		     {
		    	 for(ReportInfo uu : list)
				   {
					   JSONObject obj = new JSONObject();
					   
					   obj.put("sno",uu.getsNo());
					   obj.put("dateStr",uu.getDateStr());
					   obj.put("particular",uu.getStatus());
					   obj.put("debit",uu.getCreditAmount());
					   obj.put("credit",uu.getDebitAmount());
					   obj.put("balance",uu.getBalance());
					   obj.put("openingBalance",opBalance);
					   obj.put("totalDebit",totalCredit);
					   obj.put("totalCredit",totalDebit);
					   obj.put("closingBalance",closingBalance);
					   
					   
					   
					   arr.add(obj);
				   }
					
		     }
		     
		    mainobj.put("track", arr);

		json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
		  
	}
	
	public void calculate()
	{
		totalCredit=totalDebit=closingBalance=0;
		double balance=0;
		if(list.size()>0)
		{
			balance=opBalance;
			for(int i=0;i<list.size();i++)
			{
				list.get(i).setsNo(i+1);
				totalCredit+=list.get(i).getCreditAmount();
				totalDebit+=list.get(i).getDebitAmount();
				balance=(balance+list.get(i).getDebitAmount())-list.get(i).getCreditAmount();
				list.get(i).setBalance(balance);
			}
		}
		
		totalDebit+=opBalance;
		closingBalance=totalDebit-totalCredit;
		totalCredit=Double.parseDouble(df.format(totalCredit));
		totalDebit=Double.parseDouble(df.format(totalDebit));
		closingBalance=Double.parseDouble(df.format(closingBalance));
	}
	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;
	}
}
