package com.nisum.myteam.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

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

import com.nisum.myteam.configuration.DbConnection;
import com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.model.EmployeeEfforts;
import com.nisum.myteam.service.IEmployeeEffortsService;
import com.nisum.myteam.utils.MyTeamLogger;
import com.nisum.myteam.utils.MyTeamUtils;

@Component
@Transactional
public class EmployeeEffortsService implements IEmployeeEffortsService {
	
	@Autowired
	DbConnection dbConnection;

	@Override
	public List<EmployeeEfforts> getEmployeeEffortsReport(String fromDate, String toDate)
			throws MyTeamException, SQLException {
		String query = null;
		query = MyTeamUtils.EMPLOYEE_EFFORTS_QUERY + "'" + fromDate.replace("-", "/") + "'"
				+ MyTeamUtils.EMPLOYEE_EFFORTS_QUERY1 + "'" + toDate.replace("-", "/") + "'"
				+ MyTeamUtils.EMPLOYEE_EFFORTS_QUERY2;		
		List<EmployeeEfforts> listOfEmpEffortsData = getEmployeeEfforts(query);
		
		return listOfEmpEffortsData;
	}
	
	private List<EmployeeEfforts> getEmployeeEfforts(String query) {
		
		List<EmployeeEfforts> listOfEmpEffortsData = new ArrayList<>();
		try (Connection connection = dbConnection.getDBConnection();
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery(query.toString())) {
			while (resultSet.next()) {
				EmployeeEfforts employeeEfforts = new EmployeeEfforts();
				employeeEfforts.setEmployeeId(resultSet.getString("EmployeeCode"));
				employeeEfforts.setEmployeeName(resultSet.getString("FirstName"));
				employeeEfforts.setTotalHoursSpentInWeek(resultSet.getString("TotalHoursInWeek") + ':' + resultSet.getString("TotalMinutesInWeek"));
				listOfEmpEffortsData.add(employeeEfforts);
			}
		} catch (Exception e) {
			e.printStackTrace();
			MyTeamLogger.getInstance().error(e.getMessage());
		}
		
		return listOfEmpEffortsData;
	}

}


