package com.nisum.service;

import com.nisum.dao.Dao;
import com.nisum.dto.EmployeeDto;
import com.nisum.model.Employee;
import com.nisum.model.impl.EmployeeImp;
import com.nisum.model.impl.JobRoleImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Optional;

@Service
public class EmployeeService {

  @Autowired
  @Qualifier(value = "EmployeeDao")
  private Dao employeeDao;

  public Collection<EmployeeImp> getAllEmployees() {
    return employeeDao.getAll();
  }


  public void createEmployee(EmployeeDto employeeDto) {
    EmployeeImp employeeImp = new EmployeeImp();
    JobRoleImp jobRoleImp = new JobRoleImp(employeeDto.getJobTitle());
    employeeImp.setFirstName(employeeDto.getFirstName()); //Make sure to set it to the employeeDto not employeeImp
    employeeImp.setLastName(employeeDto.getLastName());
    employeeImp.setJobRole(jobRoleImp);
    employeeDao.save(employeeImp);
  }

  public void delete(int userId){
    Optional user = employeeDao.getById(userId);
    if(user.isPresent()){
      employeeDao.delete(user.get());
    }
  }


  public EmployeeImp getEmployeeById(int id) {
    Optional user = employeeDao.getById(id);
    return (EmployeeImp) user.get();
  }


  public void update(int id, EmployeeDto employeeDto) {
    Optional user = employeeDao.getById(id);
    EmployeeImp employeeImp = (EmployeeImp) user.get();
    // employeeImp.getJobRole().setTitle(employeeDto.getJobTitle());
    employeeImp.setFirstName(employeeDto.getFirstName());
    employeeImp.setLastName(employeeDto.getLastName());
    employeeDao.update(employeeImp);
  }

}
