package com.nisum.mytime.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import com.nisum.mytime.configuration.DbConnection;
import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AttendenceData; 
import com.nisum.mytime.utils.MyTimeLogger;
import com.nisum.mytime.utils.MyTimeUtils;

@Service
public class AttendanceServiceImpl implements AttendanceService {

	@Autowired
	DbConnection dbConnection;

	@Override
	public List getAttendanciesReport(String reportDate) throws MyTimeException, SQLException {
		long start_ms = System.currentTimeMillis();
		List listOfEmployees = new ArrayList<>();    
		 String querys=MyTimeUtils.ATTENDANCE_PRESENT_REPORT_QUERY +"'"+reportDate+"'"+ MyTimeUtils.UNION +MyTimeUtils.ATTENDANCE_ABSENT_REPORT_QUERY+"'"+reportDate+"')";
		 try(Connection connection = dbConnection.getDBConnection(); 
				Statement statement = connection.createStatement();      
				ResultSet resultSet = statement.executeQuery(querys.toString()))
				{   
					while (resultSet.next()) {  
						AttendenceData attendData=new AttendenceData();      
						attendData.setEmployeeId(resultSet.getString("EmployeeCode"));    
						attendData.setEmployeeName(resultSet.getString("FirstName"));
						attendData.setIfPresent(resultSet.getString("AttStatus"));     
						listOfEmployees.add(attendData);          
					}        
				MyTimeLogger.getInstance().info("Time Taken for " + (System.currentTimeMillis() - start_ms));
			} catch (Exception e) {
				MyTimeLogger.getInstance().error("Exception occured due to : ", e);
				throw new MyTimeException(e.getMessage());
			}
		return listOfEmployees;
		}
	}


