package com.nisum.mytime.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

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.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.RestController;

import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.exception.handler.ResponseDetails;
import com.nisum.mytime.model.Account;
import com.nisum.mytime.service.IAccountService;

@RestController
public class AccountController {

	@Autowired
	private IAccountService accountService;

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

	@RequestMapping(value = "/accounts", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> createAccount(@Valid @RequestBody Account account, HttpServletRequest request)
			throws MyTimeException {

		logger.info("Serving the Account Creation action");
		boolean isAccountExists = accountService.isAccountExists(account);
		logger.info("is Account exists with the name " + account.getAccountName() + ":::" + isAccountExists);
		if (!isAccountExists) {
			Account accountPersisted = accountService.createAccount(account);

			ResponseDetails createResponseDetails = new ResponseDetails(new Date(), 601, "Account has been created",
					"status description",null, request.getContextPath(), "details", accountPersisted);
			return new ResponseEntity<ResponseDetails>(createResponseDetails, HttpStatus.OK);

		}
		ResponseDetails responseDetails = new ResponseDetails(new Date(), 602, "An Account is already existed",
				"Choose the different account name",null, request.getContextPath(), "details", account);

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

	@RequestMapping(value = "/accounts/{accountId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> updateAccount(@RequestBody Account account, @PathVariable String accountId,
			HttpServletRequest request) throws MyTimeException {
		logger.info("Updating the account with details::"+account);
		boolean isAccountExists = accountService.isAccountExists(account);
		if (isAccountExists == true) {
			Account accountPersisted = accountService.updateAccount(account);

			ResponseDetails updateRespDetails = new ResponseDetails(new Date(), 604, "Account has been updated",
					"status description",null, request.getContextPath(), "details", accountPersisted);

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

		}
		ResponseDetails responseDetails = new ResponseDetails(new Date(), 605, "Account is Not found",
				"Choose the correct updating account name",null, request.getContextPath(), "details", account);

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

	}

	@RequestMapping(value = "/accounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> getAccounts(HttpServletRequest request) throws MyTimeException {
		List<Map<Object, Object>> accountsList = accountService.getAccountsList();
		logger.info("The accounts list::" + accountsList);

		ResponseDetails getRespDetails = new ResponseDetails(new Date(), 604, "Retrieved the accounts successfully",
				"Accounts list", accountsList, request.getContextPath(), "details", null);

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

	}

	@RequestMapping(value = "/accounts/names", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> getAccountNames(HttpServletRequest request) throws MyTimeException {
		List<Account> acountsList = accountService.getAccounts();
		List<String> accountNamesList = new ArrayList<>();

		for (Account account : acountsList) {
			accountNamesList.add(account.getAccountName());
		}
		logger.info("The account names list::" + accountNamesList);

		ResponseDetails getRespDetails = new ResponseDetails(new Date(), 604,
				"Retrieved the account names successfully", "Account names list", accountNamesList,
				request.getContextPath(), "details", null);

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

	}

	@RequestMapping(value = "/accounts/{accountId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> deleteAccount(@PathVariable String accountId, HttpServletRequest request)
			throws MyTimeException {
		logger.info("Deleting account with accountId:" + accountId);
		Account accountDeleted = accountService.deleteAccount(accountId);
		ResponseDetails deleteRespDetails = new ResponseDetails(new Date(), 604,
				"Account has been deleted successfully", "status description", null,request.getContextPath(), "details",
				accountDeleted);

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

	}
}