package com.nisum.mytime.controller;

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

import org.apache.commons.lang.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nisum.mytime.exception.handler.MyTimeException;
import com.nisum.mytime.model.AccountInfo;
import com.nisum.mytime.service.AccountServiceImpl;
import com.nisum.mytime.utils.MyTimeUtils;

@RestController
@RequestMapping("/account")
public class AccountController {

	@Autowired
	private AccountServiceImpl accountServiceImpl;


	@RequestMapping(value = "/accounts", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> addAccount(@RequestBody AccountInfo accountInfo,@RequestParam(value = "action") String action) throws MyTimeException {

		String response="";

		AccountInfo account= null;
		
		if( action != null && action.equals("N")) {
			List<AccountInfo> acounts = accountServiceImpl.validateAccounts(accountInfo.getAccountName());
			
			if( !acounts.isEmpty() &&  acounts.size() > 0){
				response ="Account already exist";
			}else {
				accountInfo.setAccountId(generateAccountId());
				accountInfo.setStatus(MyTimeUtils.ACTIVE);
				account = accountServiceImpl.addAccount(accountInfo);
				
				if(account.getId() != null) {
					response="saved Succesfully";
				}else {
					response = "Error occured while Account creating";
				}
			}
		}else if(action != null && action.equals("U")){
			accountInfo.setStatus(MyTimeUtils.ACTIVE);
			account = accountServiceImpl.addAccount(accountInfo);
			response="updated Succesfully";
		}
		return new ResponseEntity<>(response, HttpStatus.OK);
	}
	
	
	// generating the account id.
	// accountId format is "Acc001"
	private String generateAccountId() throws MyTimeException {
		return (MyTimeUtils.ACC + MyTimeUtils.ZERO_) + (accountServiceImpl.getAccounts().size() + MyTimeUtils.ONE);
	}

	@RequestMapping(value = "/accounts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<Object, Object>>> getAccounts() throws MyTimeException {
		List<Map<Object, Object>> acounts = accountServiceImpl.getAccountsList();
		return new ResponseEntity<>(acounts, HttpStatus.OK);
	}

	@RequestMapping(value = "/accountNames", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<String>> getAccountNames() throws MyTimeException {
		List<AccountInfo> acounts = accountServiceImpl.getAccounts();
		List<String> accountNames = new ArrayList<>();
		for (AccountInfo account : acounts) {
			accountNames.add(account.getAccountName());
		}
		return new ResponseEntity<>(accountNames, HttpStatus.OK);
	}

	@RequestMapping(value = "/accounts", method = RequestMethod.DELETE, produces = MediaType.TEXT_PLAIN_VALUE)
	public ResponseEntity<String> deleteAccount(@RequestParam(value = "accountId") String accountId)
			throws MyTimeException {
		accountServiceImpl.deleteAccount(accountId);
		return new ResponseEntity<>("Deleted Successfully", HttpStatus.OK);
	}

	@RequestMapping(value = "/accounts/{accountName}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
	public ResponseEntity<String> validateAccounts(@PathVariable("accountName")  String accountName) throws MyTimeException {
		String response ="";
		List<AccountInfo> acounts = accountServiceImpl.validateAccounts(accountName);
		if(acounts.size()>0){
			response ="Account already exist";
		}
		return new ResponseEntity<>(response, HttpStatus.OK);
	}

}