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

@RestController
public class DomainController {

	@Autowired
	private DomainService domainService;

	@RequestMapping(value = "/domains", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Domains> addDomain(@RequestBody Domains domains)
			throws MyTimeException {
		domains.setDomainId(domainService.getAllDomains().size()+1+"");
		return new ResponseEntity<>(domainService.addDomain(domains), HttpStatus.OK);
	}
	
	@RequestMapping(value = "/domains", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<HashMap<Object, Object>>> getAccounts() throws MyTimeException {
		return new ResponseEntity<>(domainService.getAllDomains(), HttpStatus.OK);
	}

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