package com.nisum.mytime.controller;

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

import javax.servlet.http.HttpServletRequest;

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.Domains;
import com.nisum.mytime.service.DomainService;

import com.nisum.mytime.utils.MyTimeUtils;

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

	@Autowired
	private DomainService domainService;

	@RequestMapping(value = "/domains", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> addDomain(@RequestBody Domains domain, HttpServletRequest request) throws MyTimeException {
		String response = null;
		response = domainService.addDomains(domain);

		ResponseDetails createRespDetails = new ResponseDetails(new Date(), 801, "Domain has been created",
				"Domain Creation", null, request.getContextPath(), "details", domain);
		return new ResponseEntity<ResponseDetails>(createRespDetails, HttpStatus.OK);

	}

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

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

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

	@RequestMapping(value = "/domains", method = RequestMethod.PUT, produces = MediaType.TEXT_PLAIN_VALUE)
	public ResponseEntity<?> updateDomain(@RequestBody Domains domains, HttpServletRequest request)
			throws MyTimeException {
		String response = null;
		response = domainService.updateDomain(domains);

		ResponseDetails updateRespDetails = new ResponseDetails(new Date(), 802, "Domain has been updated",
				"Domain Updation", null, request.getContextPath(), "Updation Domain details", domains);

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

	}

	@RequestMapping(value = "/domains", method = RequestMethod.DELETE, produces = MediaType.TEXT_PLAIN_VALUE)
	public ResponseEntity<?> deleteDomain(@RequestParam String domainId, HttpServletRequest request)
			throws MyTimeException {
		domainService.deleteDomain(domainId);

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

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

	}

}