package com.nisum.myteam.controller;

import java.util.HashMap;
import java.util.List;

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.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.Account;
import com.nisum.myteam.model.Project;
import com.nisum.myteam.repository.AccountRepo;
import com.nisum.myteam.repository.ProjectRepo;
import com.nisum.myteam.service.IEmployeeService;
import com.nisum.myteam.service.IProjectService;
import com.nisum.myteam.utils.MyTeamUtils;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class ProjectController {

	@Autowired
	private IEmployeeService employeeService;

	@Autowired
	private IProjectService projectService;

	@Autowired
	private AccountRepo accountRepo;

	@Autowired
	private ProjectRepo projectRepo;

	@RequestMapping(value = "/projects", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> createProject(@RequestBody Project projectReq,
			@RequestParam(value = "loginEmpId") String loginEmpId) throws MyTeamException {

		// checking project duplicateName
		int projectNameCount = 0;
		if (projectReq.getAccountId() != null) {
			
			List<Project> projects=projectService.getProjectsUnderDomain(projectReq.getDomainId());
			
			for (Project existproject : projects) {
				if (projectReq.getProjectName().equalsIgnoreCase(existproject.getProjectName()))
					projectNameCount++;
			}
		}
		if (projectNameCount > MyTeamUtils.INT_ZERO) {
			MyTeamException myTimeException = new MyTeamException("Project name already exist !!! try with new");
			return new ResponseEntity<>(myTimeException, HttpStatus.OK);
		} else {
			String accountName = "";
			String accountId = projectReq.getAccountId();
			// String accountName=projectAdded.getAccount();
			Account account = accountRepo.findByAccountId(accountId);
			if (account != null)
				accountName = account.getAccountName();
			int sequenceNumber = account.getAccountProjectSequence();
			account.setAccountProjectSequence(sequenceNumber + 1);
			// account.setAuditFields(loginEmpId, MyTimeUtils.UPDATE);
			accountRepo.save(account);
			String projectId = accountName + String.format("%04d", sequenceNumber + 1);
			projectReq.setProjectId(projectId);
			Project project = projectService.createProject(projectReq, loginEmpId);
			return new ResponseEntity<>(project, HttpStatus.OK);
		}
	}

	// @RequestMapping(value = "/updateProject"
	@RequestMapping(value = "/projects", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> updateProject(@RequestBody Project project,
			@RequestParam(value = "loginEmpId") String loginEmpId) throws MyTeamException {
		// checking project duplicateName
		int projectNameCount = 0;
		if (project.getAccountId() != null) {
			List<Project> projects = projectRepo.findByDomainId(project.getDomainId());
			for (Project existproject : projects) {
				if (project.getProjectName().equalsIgnoreCase(existproject.getProjectName())
						&& !project.getProjectId().equals(existproject.getProjectId())) {
					projectNameCount++;
				}
			}
		}
		if (projectNameCount > 0) {
			MyTeamException myTimeException = new MyTeamException("Project name already exist !!! try with new");
			return new ResponseEntity<>(myTimeException, HttpStatus.OK);
		}
		Project updatedProject = projectService.updateProject(project, loginEmpId);
		return new ResponseEntity<>(updatedProject, HttpStatus.OK);
	}

	// @RequestMapping(value = "/deleteProject"
	@RequestMapping(value = "/deleteProject", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> deleteProject(@RequestParam("projectId") String projectId) throws MyTeamException {
		projectService.deleteProject(projectId);
		return new ResponseEntity<>("Success", HttpStatus.OK);
	}

	// @RequestMapping(value = "/getProjects"
	@RequestMapping(value = "/projects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<HashMap<Object, Object>>> getProjects(
			@RequestParam(value = "empId", required = false, defaultValue = MyTeamUtils.ZERO) String empId)
			throws MyTeamException {
		List<HashMap<Object, Object>> projects = null;

		if (!"undefined".equalsIgnoreCase(empId)) {
			boolean isDl = employeeService.verifyEmployeeRole(empId, MyTeamUtils.DL);
			if (isDl) {
				projects = projectService.deliveryLeadProjects(empId);
			}
		} else {
			projects = projectService.getProjects();
		}
		return new ResponseEntity<>(projects, HttpStatus.OK);
	}
	
	
    @RequestMapping(value = "/projects/{deliveryLeadId}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Project>> getProjectsUnderDeliveryLead(
            @RequestParam("deliveryLeadId") String deliveryLeadId)
            throws MyTeamException {
        List<Project> projects = projectService.getProjectsUnderDeliveryLead(deliveryLeadId);
        return new ResponseEntity<>(projects, HttpStatus.OK);
    }
    
    
	@RequestMapping(value = "/getMyProjectAllocations", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<HashMap<Object, Object>>> getMyProjectAllocations(
			@RequestParam("employeeId") String employeeId) throws MyTeamException {
		List<HashMap<Object, Object>> empPrjtsInfo = projectService.getProjectsForEmployee(employeeId);
		return new ResponseEntity<>(empPrjtsInfo, HttpStatus.OK);
	}
	
    

}