/**
 * 
 */
package com.nisum.mytime.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.model.EmployeeVisa;
import com.nisum.mytime.model.TravelRequest;
import com.nisum.mytime.model.Visa;
import com.nisum.mytime.repository.EmployeeVisaRepo;
import com.nisum.mytime.repository.TravelRepo;
import com.nisum.mytime.repository.VisaRepo;
import com.poiji.bind.Poiji;
import com.poiji.exception.PoijiExcelType;
import com.poiji.option.PoijiOptions;

import lombok.extern.slf4j.Slf4j;

/**
 * @author nisum
 *
 */
@Service
@Slf4j
public class VisaServiceImpl implements VisaService {

    @Autowired
    private VisaRepo visaRepo;
    @Autowired
    private TravelRepo travelRepo;

    @Autowired
    private EmployeeVisaRepo employeeVisaRepo;

    @Autowired
    private UserService userService;

    @Override
    public List<Visa> getAllVisas() {
        return visaRepo.findAll();
    }

    @Override
    public List<EmployeeVisa> getAllEmployeeVisas() {
        return employeeVisaRepo.findAll();
    }

    @Override
    public EmployeeVisa addEmployeeVisas(EmployeeVisa e) {
        return employeeVisaRepo.save(e);
    }

    @Override
    public EmployeeVisa updateEmployeeVisas(EmployeeVisa e) {
        return employeeVisaRepo.save(e);
    }

    @Override
    public void deleteEmployeeVisas(EmployeeVisa e) {
        employeeVisaRepo.delete(e);
    }

    @Override
    public List<TravelRequest> getAllTravels() {
        return travelRepo.findAll();
    }

    @Override
    public TravelRequest addTravelRequest(TravelRequest e) {
        return travelRepo.save(e);
    }

    @Override
    public TravelRequest updateTravelRequest(TravelRequest e) {
        return travelRepo.save(e);
    }

    @Override
    public void deleteEmployeeVisas(TravelRequest e) {
        travelRepo.delete(e);
    }

    @Override
    public String exportDataFromExcelFile(MultipartFile file)
            throws MyTimeException {
        String result = "Failure";
        int counter = 0;
        try {
            PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings()
                    .preferNullOverDefault(true).datePattern("dd-MMM-yyyy")
                    .build();

            List<EmployeeRoles> employees = Poiji.fromExcel(
                    file.getInputStream(), PoijiExcelType.XLS,
                    EmployeeRoles.class, options);
            if (!employees.isEmpty()) {
                for (EmployeeRoles employee : employees) {
                    System.out.println("test employee" + employee);
                    if (null != employee.getEmployeeId())
                        findAndModifyEmployeeRole(employee);
                    else
                        counter++;
                }
                if (counter == 0) {
                    result = "Success";
                    log.info("Exported {} employee records from file: {}",
                            employees.size(), file.getOriginalFilename());
                } else {
                    log.info(
                            "Uploaded file: {}, does not contain valid employee records",
                            file.getOriginalFilename());
                }
            }
        } catch (Exception e) {
            log.error(
                    "Exception occured while exporting the data from excel file due to: {}",
                    e);
            throw new MyTimeException("");
        }
        return result;
    }

    private void findAndModifyEmployeeRole(EmployeeRoles employee) {
        /*
         * Query query = new
         * Query(Criteria.where("employeeId").is(employee.getEmployeeId()))
         * .addCriteria(Criteria.where("emailId").is(employee.getEmailId()));
         * Update update = new Update(); update.set("employeeId",
         * employee.getEmployeeId()); update.set("employeeName",
         * employee.getEmployeeName()); update.set("emailId",
         * employee.getEmailId()); update.set("role", employee.getRole());
         * update.set("functionalGroup", employee.getFunctionalGroup());
         * update.set("empStatus", employee.getEmpStatus());
         * update.set("employmentType", employee.getEmploymentType());
         * update.set("empLocation", employee.getEmpLocation());
         * update.set("domain", employee.getDomain()); update.set("designation",
         * employee.getDesignation()); update.set("dateOfBirth",
         * employee.getDateOfBirth()); update.set("dateOfJoining",
         * employee.getDateOfJoining()); update.set("shift",
         * employee.getShift()); update.set("baseTechnology",
         * employee.getBaseTechnology()); update.set("technologyKnown",
         * employee.getTechnologyKnown()); update.set("mobileNumber",
         * employee.getMobileNumber()); update.set("alternateMobileNumber",
         * employee.getAlternateMobileNumber()); update.set("personalEmailId",
         * employee.getPersonalEmailId()); update.set("createdOn", new Date());
         * update.set("lastModifiedOn", new Date()); FindAndModifyOptions
         * options = new FindAndModifyOptions(); options.returnNew(true);
         * options.upsert(true); mongoTemplate.findAndModify(query, update,
         * options, EmployeeRoles.class);
         * log.info("Inserted Employee record with Id: {}",
         * employee.getEmployeeId());
         */
        employee.setRole("Employee");
        EmployeeRoles emp = userService
                .getEmployeesRoleData(employee.getEmployeeId());
        if (emp == null) {
            try {
                userService.assigingEmployeeRole(employee);
            } catch (MyTimeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}
