package com.nisum.myteam.controller;

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

import javax.servlet.http.HttpServletRequest;

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.util.StringUtils;
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.exception.handler.ResponseDetails;
import com.nisum.myteam.model.Domain;
import com.nisum.myteam.service.IDomainService;
import com.nisum.myteam.utils.MyTeamUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Vijay
 *
 */
@RestController
@Slf4j
public class DomainController {

	@Autowired
	private IDomainService domainService;

	@RequestMapping(value = "/domains", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> createDomain(@RequestBody Domain domain, HttpServletRequest request)
			throws MyTeamException {

		log.info("Domain Creation");

		if (!domainService.isDomainExists(domain)) {
			Domain domainPeristed = domainService.create(domain);

			ResponseDetails createRespDetails = new ResponseDetails(new Date(), 801, "Domain has been created",
					"Domain Creation", null, "", "details", domainPeristed);
			return new ResponseEntity<ResponseDetails>(createRespDetails, HttpStatus.OK);
		}
		log.info("A domain is already existed with the requested name" + domain.getDomainName());

		ResponseDetails responseDetails = new ResponseDetails(new Date(), 802, "Domain is already existed",
				"Choose the different domain name", null, request.getRequestURI(), "Domain details", domain);

		return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

	}

	@RequestMapping(value = "/domains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> getDomains(HttpServletRequest request) throws MyTeamException {

		ResponseDetails getRespDetails = new ResponseDetails(new Date(), 804, "Retrieved the domains successfully",
				"Domains list", domainService.getDomainsList(), request.getRequestURI(), "details", null);

		return new ResponseEntity<ResponseDetails>(getRespDetails, HttpStatus.OK);
	}

	@RequestMapping(value = "/domains", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> updateDomain(@RequestBody Domain domain, HttpServletRequest request)
			throws MyTeamException {

		boolean isDomainExists = domainService.isDomainExists(domain);
		if (isDomainExists == true) {
			Domain domainPersisted = domainService.update(domain);
			ResponseDetails updateRespDetails = new ResponseDetails(new Date(), 802, "Domain has been updated",
					"Domain Updation", null, request.getRequestURI(), "Updation Domain details", domainPersisted);

			return new ResponseEntity<ResponseDetails>(updateRespDetails, HttpStatus.OK);
		}
		ResponseDetails responseDetails = new ResponseDetails(new Date(), 803, "Domain is Not found",
				"Choose the correct updating domain name", null, request.getRequestURI(), "details", domain);
		return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.OK);

	}

	@RequestMapping(value = "/domains/{domainId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> deleteDomain(@PathVariable String domainId, HttpServletRequest request)
			throws MyTeamException {
		domainService.delete(domainId);

		ResponseDetails deleteRespDetails = new ResponseDetails(new Date(), 804, "Domain has been deleted",
				"Domain Deletion", null, request.getRequestURI(), "Deletion Domain details", domainId);

		return new ResponseEntity<ResponseDetails>(deleteRespDetails, HttpStatus.OK);

	}


   //getting domains list under accountId which is an active. 
	@RequestMapping(value = "/domains/{accountId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Domain>> getDomains(@PathVariable("accountId") String accountId) throws MyTeamException {
		List<Domain> domains = domainService.getDomainsUnderAccount(accountId).stream()
				.filter(e -> "Active".equalsIgnoreCase(e.getStatus())).collect(Collectors.toList());
		return new ResponseEntity<>(domains, HttpStatus.OK);
	}

}