package com.nisum.mytime.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.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.Account;
import com.nisum.mytime.model.EmployeeRoles;
import com.nisum.mytime.model.Project;
import com.nisum.mytime.repository.AccountRepo;
import com.nisum.mytime.repository.ProjectRepo;
import com.nisum.mytime.service.ProjectService;
import com.nisum.mytime.service.UserService;
import com.nisum.mytime.utils.MyTimeUtils;

@RestController
@RequestMapping("/project")
public class ProjectController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private AccountRepo accountRepo;
	@Autowired
	private ProjectRepo projectRepo;
	@RequestMapping(value = "/employee", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<EmployeeRoles> getEmployeeRole(@RequestParam("emailId") String emailId)
			throws MyTimeException {
		EmployeeRoles employeesRole = userService.getEmployeesRole(emailId);
		return new ResponseEntity<>(employeesRole, HttpStatus.OK);
	}

	@RequestMapping(value = "/addProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> addProject(@RequestBody Project projectAdded) throws MyTimeException {
		// checking project duplicateName
    	int projectNameCount=0;
		if (projectAdded.getAccountId() != null) {
			List<Project> projects = projectRepo.findByDomainId(projectAdded.getDomainId());
			for (Project existproject : projects) {
				if (projectAdded.getProjectName().equalsIgnoreCase(existproject.getProjectName()))
					projectNameCount++;		}
		}
		if (projectNameCount>0)
		{
			MyTimeException myTimeException= new MyTimeException("Project name already exist !!! try with new");
			return new ResponseEntity<>(myTimeException, HttpStatus.OK);
		}
		else
		{
		    String accountName="";
		    String accountId=projectAdded.getAccountId();
		   // String accountName=projectAdded.getAccount();
		   Account account= accountRepo.findByAccountId(accountId);
		   if(account!=null)
		      accountName=account.getAccountName();
		  int sequenceNumber= account.getAccountProjectSequence();
		       account.setAccountProjectSequence(sequenceNumber+1);
		       accountRepo.save(account);
		     String projectId=  accountName+String.format("%04d", sequenceNumber+1);
		     projectAdded.setProjectId(projectId);
		Project project = projectService.addProject(projectAdded);
		return new ResponseEntity<>(project, HttpStatus.OK);
		}
	}

	@RequestMapping(value = "/updateProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> updateEmployeeRole(@RequestBody Project project) throws MyTimeException {
		// 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)
		{
			MyTimeException myTimeException= new MyTimeException("Project name already exist !!! try with new");
			return new ResponseEntity<>(myTimeException, HttpStatus.OK);
		}
		Project updatedProject = projectService.updateProject(project);
		return new ResponseEntity<>(updatedProject, HttpStatus.OK);
	}

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

	@RequestMapping(value = "/getProjects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<HashMap<Object, Object>>> getProjects(@RequestParam(value="empId", required = false, defaultValue = MyTimeUtils.ZERO) String  empId) throws MyTimeException {
		List<HashMap<Object, Object>> projects = null;
		
		if(!"undefined".equalsIgnoreCase(empId) ) {
			boolean isDl = userService.verifyRole(empId,MyTimeUtils.DL) ;
			if( isDl ){
				projects = projectService.deliveryLeadProjects(empId);
			}
		}else {
			projects = projectService.getProjects();
		}
		return new ResponseEntity<>(projects, HttpStatus.OK);
	}

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