package com.Dao;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.Model.Customer;

public class CustomerOperations {
	public int checkLogin(String cId, String cPwd){
		int check = 0;
		java.sql.Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		System.out.println("Inside DAo");
		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			
			con = DriverManager.getConnection("jdbc:derby:D:\\Users\\2736227\\MyDB;create=true");
			
			String sql = "SELECT * FROM customer WHERE custId = ? AND cPwd = ?";
			pst = con.prepareStatement(sql);
			pst.setString(1, cId);
			pst.setString(2, cPwd);
			
			System.out.println("**********Inside the DAO************");
			//rs contains all the rows received after the execution of the SQL query
			rs = pst.executeQuery();
			if(rs.next()){
				check = 1;
			}
//			while(rs.next()){
//				Admin ad = new Admin();
//				System.out.println(rs.getInt(1)+" "+rs.getString(2));
//				if(rs.getInt(1)==ad.getEmpId() && rs.getString(2).equals(ad.getPwd())){
//					check = 1;
//				}
//			}
			
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return check;
	}
	
	public ArrayList<Customer> findAllCustomers(){
		ArrayList<Customer> cust_list = new ArrayList<>();
		
		ResultSet rs = null;
		java.sql.Connection con = null;
		PreparedStatement pst = null;
		
		
		try {
			
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			
			con= DriverManager.getConnection("jdbc:derby:D:\\Users\\2736227\\MyDB;create=true");
			
			String sql="select * from customer";
			
			pst= con.prepareStatement(sql);
			
			rs=pst.executeQuery();
			
			while(rs.next())
			{
				Customer c = new Customer();
				
				c.setcId(rs.getString(1));
				c.setcPwd(rs.getString(2));
				c.setcSsnId(rs.getString(3));
				c.setfName(rs.getString(4));
				c.setlName(rs.getString(5));
				c.setEmail(rs.getString(6));
				c.setAddress(rs.getString(7));
				c.setContactNo(rs.getString(8));
				c.setAadharNo(rs.getString(9));
				c.setPanNo(rs.getString(10));
				c.setAcNo(rs.getInt(11));
				c.setInitialAmt(rs.getInt(12));
				c.setAcStatus(rs.getBoolean(13));
				
				cust_list.add(c);
			}
						
			
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("\n\n\n*******INSERT block ::::: NO VALUES*******\n\n\n\n");
		}
		
		return cust_list;
	} 
}
