package com.nisum.mytime.controller;

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

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.Account;
import com.nisum.mytime.service.AccountServiceImpl;

@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 Account account,@RequestParam(value = "action") String action) throws MyTimeException {
		String response = accountServiceImpl.addAccount(account, action);
		return new ResponseEntity<>(response, HttpStatus.OK);
	}
	
	
	@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<Account> acounts = accountServiceImpl.getAccounts();
		List<String> accountNames = new ArrayList<>();
		for (Account 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<Account> acounts = accountServiceImpl.validateAccounts(accountName);
		if(acounts.size()>0){
			response ="Account already exist";
		}
		return new ResponseEntity<>(response, HttpStatus.OK);
	}

}