package com.nisum.mytime.controller;

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

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.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.exception.handler.ResponseDetails;
import com.nisum.mytime.model.Domain;
import com.nisum.mytime.service.IDomainService;

import com.nisum.mytime.utils.MyTimeUtils;

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

	@Autowired
	private IDomainService domainService;

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

	@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 MyTimeException {

		logger.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);
		}
		logger.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 MyTimeException {

		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 MyTimeException {

		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 MyTimeException {
		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);

	}

}