package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
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.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@ViewScoped
@ManagedBean(name="savingToSavingTransferJson")
public class SavingToSavingTransferJsonBean implements Serializable
{
	String json;
	Date startDate;
	
	public SavingToSavingTransferJsonBean() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
		String userid = params.get("userid");
		String amount=params.get("amount");
		String searchType = params.get("searchType");
		String searchNo=params.get("searchNo");
		String branchStr = params.get("branchId");
		int branchId = Integer.valueOf(branchStr);
		
		HttpSession ss1=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		ss1.setAttribute("username", userid);
		
		System.out.println("user id: "+userid+" Amount"+amount+" Search No."+searchNo+" Search Type "+searchType);
		JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
			
			 JSONObject obj = new JSONObject();
					
			 DataBaseMethods dbm=new DataBaseMethods();
			 Connection conn=DataBaseConnection.JAVAConnection();
			 
				String shno="";
			 	if(searchType.equalsIgnoreCase("Saving Account No"))
			 	{
			 		shno=dbm.shareHolderNoBySavingAccountNO(searchNo);
			 	}
			 	else
			 	{
			 		shno=dbm.shareHolderDetailByAadharCardNo(branchId,"shareholderno" ,searchNo, conn);
			 	}
			 
			 	String acc1=dbm.savingAccountByShareHolderNo(userid);
			 	String acc2=dbm.savingAccountByShareHolderNo(shno);
			 	
			 int i=dbm.savingAppUserTransferEntry(acc1, searchNo, amount, shno,branchId,conn) ;
			 Date currentDate=new Date();
			 
			 // Amount Deposit in Account Two
			 String remarkDeposit="Transfer From "+acc1;
			 int	m = dbm.userDeposit(branchId,acc2, amount, currentDate,"Transfer", "", null, "","approved","N/A",
					 remarkDeposit, "");
			 if (m >= 1) 
			 {
				 double preAmount=dbm.acconuntHolderAmountByAccountNo(acc2);
					Double newAmount=preAmount+Double.parseDouble(amount);
					dbm.updateSavingAmountByAccountNo(acc2,newAmount);
			 }
			 
			 
			 // Amount Withdraw from Account One
			 String remarkWithdraw="Transfer to "+acc2;
		 	int	n = dbm.userWithdrwal(branchId,acc1, amount, currentDate,"Transfer", "", null, "", "","approved",
		 			remarkWithdraw, "");
			if (n >= 1) 
			{
				double preAmount=dbm.acconuntHolderAmountByAccountNo(acc1);
				Double newAmount=preAmount-Double.parseDouble(amount);
				dbm.updateSavingAmountByAccountNo(acc1,newAmount);
			}
			 
			 double savingBalance=dbm.acconuntHolderAmountByAccountNo(acc1);
			 
		    	 obj.put("st",i);
		    	 obj.put("savingBalance",savingBalance);
		    	  
			     arr.add(obj); 
			      
			     try {
						conn.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		    mainobj.put("track", arr);

		json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
		  
	}
	
	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;
	}
}
