package com.nisum.myteam.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
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 com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.model.Employee;
import com.nisum.myteam.model.EmployeeDashboardVO;
import com.nisum.myteam.model.EmployeeVisa;
import com.nisum.myteam.model.Resource;
import com.nisum.myteam.repository.EmployeeVisaRepo;
import com.nisum.myteam.service.IEmployeeService;
import com.nisum.myteam.service.IProjectService;
import com.nisum.myteam.service.IResourceService;
import com.nisum.myteam.utils.MyTeamUtils;

@RestController
@RequestMapping("/projectTeam")
public class ResourceController {

	@Autowired
	private IEmployeeService employeeService;

	@Autowired
	private IProjectService projectService;

	@Autowired
	private EmployeeVisaRepo employeeVisaRepo;

	@Autowired
	private IResourceService resourceService;

	

	// @RequestMapping(value = "/addEmployeeToTeam"
	@RequestMapping(value = "/resources", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Resource> addResourceToTeam(@RequestBody Resource resource,
			@RequestParam(value = "loginEmpId", required = false) String loginEmpId) throws MyTeamException {
		resource.setActive(true);
		resource.setAuditFields(loginEmpId, MyTeamUtils.CREATE);
		Resource resourcePersisted = resourceService.addResource(resource, loginEmpId);
		return new ResponseEntity<>(resourcePersisted, HttpStatus.OK);
	}

	// @RequestMapping(value = "/updateTeammate"
	@RequestMapping(value = "/resources", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> updateResource(@RequestBody Resource resource,
			@RequestParam(value = "loginEmpId") String loginEmpId) throws MyTeamException {
		resource.setAuditFields(loginEmpId, MyTeamUtils.UPDATE);
		String response = resourceService.updateResource(resource, loginEmpId);
		return new ResponseEntity<>(response, HttpStatus.OK);
	}

	// @RequestMapping(value = "/deleteTeammate"
	@RequestMapping(value = "/resources", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> deleteResource(@RequestBody Resource resource,
			@RequestParam(value = "loginEmpId") String loginEmpId) throws MyTeamException {
		resourceService.deleteResource(resource.getEmployeeId(), resource.getProjectId(), resource.getId(), loginEmpId);
		return new ResponseEntity<>("Success", HttpStatus.OK);
	}

	
	
	
	// @RequestMapping(value = "/getEmployeeProjectInfo"
	@RequestMapping(value = "/resources", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getResourcesSortByStartDate(@RequestParam("empId") String empId)
			throws MyTeamException {
		List<Resource> projectInfo = resourceService.getResourcesSortByStartDate(empId);
		return new ResponseEntity<>(projectInfo, HttpStatus.OK);
	}

	//@RequestMapping(value = "/getMyTeamDetails"
	@RequestMapping(value = "/resources/active", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getActiveResources(@RequestParam("employeeId") String employeeId)
			throws MyTeamException {
		List<Resource> employeesRoles = resourceService.getActiveResources(employeeId);
		return new ResponseEntity<>(employeesRoles, HttpStatus.OK);
	}

	

	// @RequestMapping(value = "/getShiftDetails"
	@RequestMapping(value = "/resources/{shift}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getResourcesForShift(@PathVariable("shift") String shift)
			throws MyTeamException {
		List<Resource> resourcesList = resourceService.getResourcesForShift(shift);
		return new ResponseEntity<>(resourcesList, HttpStatus.OK);
	}

	// @RequestMapping(value = "/getProjectAllocations"
	@RequestMapping(value = "/resources/projects/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getResourcesAllocatedForAllProjects() throws MyTeamException {
		List<Resource> resourcesList = resourceService.getResourcesForActiveProjects();
		return new ResponseEntity<>(resourcesList, HttpStatus.OK);
	}

	// @RequestMapping(value = "/getProjectDetails"
	@RequestMapping(value = "/resources/project/{projectId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getResourcesForProject(@PathVariable("projectId") String projectId,
			@RequestParam(value = "status", required = false, defaultValue = MyTeamUtils.ACTIVE) String status)
			throws MyTeamException {
		List<Resource> resourcesList = resourceService.getResourcesForProject(projectId, status);
		return new ResponseEntity<>(resourcesList, HttpStatus.OK);
	}

	
	//@RequestMapping(value = "/getTeamDetails"
	@RequestMapping(value = "/resources/deliverylead/{deliveryLeadId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Resource>> getTeamDetails(@PathVariable("deliveryLeadId") String deliveryLeadId)
			throws MyTeamException {
		List<Resource> resourcesList = resourceService.getResourcesUnderDeliveryLead(deliveryLeadId);
		return new ResponseEntity<>(resourcesList, HttpStatus.OK);
	}
	
	
	
	@RequestMapping(value = "/getUnAssignedEmployees", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Employee>> getUnAssignedEmployees() throws MyTeamException {
		List<Employee> employeesList = projectService.getUnAssignedEmployees();
		return new ResponseEntity<>(employeesList, HttpStatus.OK);
	}
	
	
	
	
	

	@RequestMapping(value = "/getEmployeesDashBoard", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<EmployeeDashboardVO>> getEmployeesDashBoard() throws MyTeamException {
		List<EmployeeDashboardVO> employeeDashBoardList = projectService.getEmployeesDashBoard();
		return new ResponseEntity<>(employeeDashBoardList, HttpStatus.OK);
	}

	@RequestMapping(value = "/getEmployeesHavingVisa", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Employee>> getEmployeesHavingVisa(@RequestParam("visa") String passport)
			throws MyTeamException {
		List<Employee> employees = new ArrayList<>();
		if (passport != null && !"passport".equalsIgnoreCase(passport)) {
			List<EmployeeVisa> employeeVisas = employeeVisaRepo.findByVisaName(passport);
			List<String> employeeIds = null;
			if (employeeVisas != null) {
				employeeIds = employeeVisas.stream().map(EmployeeVisa::getEmployeeId).collect(Collectors.toList());
			}
			if (employeeIds != null && !employeeIds.isEmpty()) {
				List<Employee> emps = employeeService.getActiveEmployees();
				for (Employee emp : emps) {
					if (employeeIds.contains(emp.getEmployeeId())) {
						employees.add(emp);
					}
				}
			}
			return new ResponseEntity<>(employees, HttpStatus.OK);
		} else {
			// List<EmployeeRoles> employees = new ArrayList<>();
			if (employeeService.getActiveEmployees() != null) {
				employees = employeeService.getActiveEmployees().stream()
						.sorted((o1, o2) -> o1.getEmployeeName().compareTo(o2.getEmployeeName()))
						.collect(Collectors.toList());
			}
			return new ResponseEntity<>(employees, HttpStatus.OK);
		}

	}

	
	
	
	@RequestMapping(value = "/addEmployeeToTeamWithCheck", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> addEmployeeToTeamWithCheck(@RequestBody Resource resource,
			@RequestParam(value = "loginEmpId") String loginEmpId) throws MyTeamException {
		String response = projectService.addProjectTeamMateWithCheck(resource, loginEmpId);
		return new ResponseEntity<>(response, HttpStatus.OK);
	}

}

// @RequestMapping(value = "/employee", method = RequestMethod.GET,
// public ResponseEntity<Employee> getEmployee(

// @RequestMapping(value = "/updateEmployeeRole", method =
// public ResponseEntity<Employee> updateEmployeeRole(@RequestBody Employee

// @RequestMapping(value = "/deleteEmployee"
// public ResponseEntity<String> deleteEmployee

// @RequestMapping(value = "/getEmployeeRoleData",
// public ResponseEntity<Employee> getEmployeeRoleData(

// @RequestMapping(value = "/addProject"
// public ResponseEntity<Project> addProject(

// @RequestMapping(value = "/getEmployeesToTeam"
// public ResponseEntity<List<Employee>> getActiveEmployeesSortByName() throws
// MyTimeException {
