package com.nisum.mytime.controller;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Account;
import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.model.Domain;
import com.nisum.mytime.model.Employee;
import com.nisum.mytime.service.IEmployeeService;
import com.nisum.mytime.service.IRoleMappingService;

import lombok.extern.slf4j.Slf4j;

@RestController

@Slf4j
public class EmployeeController {

	@Autowired
	private IEmployeeService empService;

	@Autowired
	private IRoleMappingService roleMappingService;

	private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);
	

	@RequestMapping(value = "/employees", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> getEmployee(@RequestParam("emailId") String emailId) throws MyTimeException {
		Employee employee = empService.getEmployeeByEmaillId(emailId);
		if (employee != null) {
			if (employee.getRole() != null && employee.getRole().equalsIgnoreCase("Admin")) {
				employee.setRole("Admin");
			} else {
				String roleName = roleMappingService.getEmployeeRole(employee.getEmployeeId());
				if (roleName != null) {
					employee.setRole(roleName);
				}
			}
		}
		System.out.println("emailId" + emailId + "result" + employee);
		return new ResponseEntity<>(employee, HttpStatus.OK);
	}

	@RequestMapping(value = "/employees/{empId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> createEmployee(@RequestBody Employee employeeReq,
			@PathVariable(value = "empId") String loginEmpId) throws MyTimeException {
		Employee employeePersisted = empService.createEmployee(employeeReq, loginEmpId);
		return new ResponseEntity<>(employeePersisted, HttpStatus.OK);
	}

	@RequestMapping(value = "/employees/{empId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> updateEmployee(@RequestBody Employee employeeRoles,
			@PathVariable(value = "empId") String loginEmpId) throws MyTimeException {
		Employee employeeRole = empService.updateEmployeeRole(employeeRoles, loginEmpId);
		return new ResponseEntity<>(employeeRole, HttpStatus.OK);
	}

	@RequestMapping(value = "/employees/{empId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> deleteEmployee(@PathVariable("empId") String empId) throws MyTimeException {
		empService.deleteEmployee(empId);
		return new ResponseEntity<>("Success", HttpStatus.OK);
	}

	//@RequestMapping(value = "/getUserRoles", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/active", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Employee>> getUserRoles() throws MyTimeException {
		List<Employee> employeesRoles = empService.getActiveEmployees();
		return new ResponseEntity<>(employeesRoles, HttpStatus.OK);
	}

	//@RequestMapping(value = "/getEmployeeRoleData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/{empId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> getEmployeeRoleData(@PathVariable("empId") String empId) throws MyTimeException {
		Employee employeesRole = empService.getEmployeesRoleData(empId);
		return new ResponseEntity<>(employeesRole, HttpStatus.OK);
	}

	/*
	@RequestMapping(value = "/getEmployeeLocations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<EmployeeLocationDetails>> getEmployeeLocations(@RequestParam("employeeId") String empId)
			throws MyTimeException {
		List<EmployeeLocationDetails> employeeLocationDetails = empService.getEmployeeLocationDetails(empId);
		return new ResponseEntity<>(employeeLocationDetails, HttpStatus.OK);
	}
	*/

	// @RequestMapping(value = "/getManagers"
	@RequestMapping(value = "/employees/managers/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Employee>> getManagers() throws MyTimeException {
		List<Employee> managersList = empService.getManagers();
		return new ResponseEntity<>(managersList, HttpStatus.OK);
	}

	/*
//	@RequestMapping(value = "/getAllShifts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/shifts/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getAllShifts() throws MyTimeException {
		
		List<String> shifts = empService.getAllShifts().stream().filter(e -> "Y".equalsIgnoreCase(e.getActiveStatus()))
				.map(Shift::getShiftName).sorted().collect(Collectors.toList());
		return new ResponseEntity<>(shifts, HttpStatus.OK);
	}
	*/

	/*
//	@RequestMapping(value = "/getAllDesignations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/designations/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getAllDesignations() throws MyTimeException {
		List<String> designations = empService.getAllDesignations().stream()
				.filter(e -> "Y".equalsIgnoreCase(e.getActiveStatus())).map(Designation::getDesignationName).sorted()
				.collect(Collectors.toList());
		return new ResponseEntity<>(designations, HttpStatus.OK);
	}

*/
	
	/*
//	@RequestMapping(value = "/getSkills", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/skills/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getTechnologies() throws MyTimeException {
		List<String> technologies = empService.getTechnologies().stream()
				.filter(e -> "Y".equalsIgnoreCase(e.getActiveStatus())).map(Skill::getSkillName).sorted()
				.collect(Collectors.toList());
		return new ResponseEntity<>(technologies, HttpStatus.OK);

	}
	*/

	/*
	//@RequestMapping(value = "/getLocations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/locations/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getLocations() throws MyTimeException {
		System.out.println(" userService.getLocations()" + empService.getLocations());
		List<String> locations = empService.getLocations().stream().filter(e -> (e.isActiveStatus() == true))
				.map(Location::getLocation).sorted().collect(Collectors.toList());
		logger.info("getLocations " + locations);
		return new ResponseEntity<>(locations, HttpStatus.OK);

	}
	*/

	@RequestMapping(value = "/updateProfile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> updateProfile(@RequestBody Employee employeeRoles) throws MyTimeException {
		Employee employeeRole = empService.updateProfile(employeeRoles);
		return new ResponseEntity<>(employeeRole, HttpStatus.OK);
	}

	//@RequestMapping(value = "/getAccounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employee/accounts/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Account>> getAccounts() throws MyTimeException {
		List<Account> technologies = empService.getAccounts().stream().filter(e -> "Y".equalsIgnoreCase(e.getStatus()))
				// .filter(a -> !("Nisum
				// India".equalsIgnoreCase(a.getAccountName())))
				// .map(Account::getAccountName).sorted()
				.collect(Collectors.toList());
		return new ResponseEntity<>(technologies, HttpStatus.OK);
	}

	@RequestMapping(value = "/getEmployeeRoleDataForSearchCriteria", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Employee> getEmployeeRoleDataForSearchCriteria(@RequestParam("searchId") String searchId,
			@RequestParam("searchAttribute") String searchAttribute) throws MyTimeException {
		Employee employeesRole = empService.getEmployeeRoleDataForSearchCriteria(searchId, searchAttribute);
		return new ResponseEntity<>(employeesRole, HttpStatus.OK);
	}

	@RequestMapping(value = "/getEmployeeDetailsForAutocomplete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getEmployeeDetailsForAutocomplete() throws MyTimeException {
		List<String> details = empService.getEmployeeDetailsForAutocomplete();
		return new ResponseEntity<>(details, HttpStatus.OK);
	}

	/*@RequestMapping(value = "/getMasterData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map<String, List<String>>> getMasterData() throws MyTimeException {
		Map<String, List<String>> masterDataMap = new HashMap<>();
		Map<String, List<MasterData>> result = empService.getMasterData().stream()
				.filter(e -> (e.isActiveStatus() == true))
				.collect(Collectors.groupingBy(MasterData::getMasterDataType));
		for (String key : result.keySet()) {
			List<MasterData> valueList = result.get(key);
			List<String> technologies = valueList.stream().map(MasterData::getMasterDataName).sorted()
					.collect(Collectors.toList());
			masterDataMap.put(key, technologies);
		}
		return new ResponseEntity<>(masterDataMap, HttpStatus.OK);

	}
	*/

	//@RequestMapping(value = "/getEmployeeByStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/status/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Employee>> getEmployeeByStatus(@RequestParam("status") String status) {

		List<Employee> employeesRoles = empService.getEmployeesByStatus(status);
		return new ResponseEntity<>(employeesRoles, HttpStatus.OK);
	}

	//@RequestMapping(value = "/getDeliveryLeads", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/employees/deliveryLeads/{domainId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<HashMap<String, String>>> getDeliveryLeads(@PathVariable("domainId") String domainId)
			throws MyTimeException {
		List<HashMap<String, String>> managers = empService.getDeliveryLeads(domainId);
		return new ResponseEntity<>(managers, HttpStatus.OK);
	}

	@RequestMapping(value = "/getAccountsInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<AccountInfo>> getAccountsInfo() throws MyTimeException {
		List<AccountInfo> technologies = empService.getAccountsInfo().stream()
				.filter(e -> "Active".equalsIgnoreCase(e.getStatus())).collect(Collectors.toList());
		return new ResponseEntity<>(technologies, HttpStatus.OK);
	}

	@RequestMapping(value = "/getDomains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Domain>> getDomains(@RequestParam("accountId") String accountId) throws MyTimeException {
		List<Domain> domains = empService.getDomains(accountId).stream()
				.filter(e -> "Active".equalsIgnoreCase(e.getStatus())).collect(Collectors.toList());
		return new ResponseEntity<>(domains, HttpStatus.OK);
	}

	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ResponseEntity<String> exportDataFromFile(@RequestParam(value = "file") MultipartFile file,
			@RequestParam(value = "empId") String loginEmpId) throws MyTimeException {
		log.info("Uploaded file: {} with size: {}", file.getOriginalFilename(), file.getSize());
		String result = empService.importDataFromExcelFile(file, loginEmpId);
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

}