/**
 * 
 */
package com.sqsoft.mars.controller;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.sqsoft.mars.domain.Account;
import com.sqsoft.mars.domain.Bookmark;
import com.sqsoft.mars.repository.AccountRepository;
import com.sqsoft.mars.repository.BookmarkRepository;
import com.sqsoft.mars.resource.BookmarkResource;

/**
 * @author lenovo
 *
 */
@RestController
@RequestMapping("/data/account/{account}/bookmarks")
public class BookmarkRestController {

	private static final Logger LOGGER = Logger
			.getLogger(BookmarkRestController.class);

	@Autowired
	private AccountRepository accountRepository;

	@Autowired
	private BookmarkRepository bookmarkRepository;

	public AccountRepository getAccountRepository() {
		return accountRepository;
	}

	public void setAccountRepository(AccountRepository accountRepository) {
		this.accountRepository = accountRepository;
	}

	public BookmarkRepository getBookmarkRepository() {
		return bookmarkRepository;
	}

	public void setBookmarkRepository(BookmarkRepository bookmarkRepository) {
		this.bookmarkRepository = bookmarkRepository;
	}

	@RequestMapping(value = "/init", method = RequestMethod.GET)
	public Map<String, Object> init(@PathVariable String account) {
		Map<String, Object> map = new HashMap<String, Object>();
		LOGGER.info("init");
		List<String> accounts = Arrays
				.asList("jhoeller,dsyer,pwebb,ogierke,rwinch,mfisher,mpollack,jlong"
						.split(","));
		for (String temp : accounts) {
			Account a = getAccountRepository().save(
					new Account(temp, "password"));
			getBookmarkRepository().save(
					new Bookmark(a, "http://bookmark.com/1/" + a,
							"A description"));
			getBookmarkRepository().save(
					new Bookmark(a, "http://bookmark.com/2/" + a,
							"A description"));
		}
		return map;
	}

	@RequestMapping(value = "/{bookmarkId}", method = RequestMethod.GET)
	BookmarkResource readBookmark(@PathVariable String account,
			@PathVariable Long bookmarkId) {
		this.validateUser(account);
		return new BookmarkResource(bookmarkRepository.findOne(bookmarkId));
	}

	@RequestMapping(method = RequestMethod.GET)
	Collection<Bookmark> readBookmarks(@PathVariable String account) {
		this.validateUser(account);
		return this.bookmarkRepository.findByAccountUsername(account);
	}

	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	private void validateUser(String account) {
		LOGGER.info("validateUser " + account);
		List<Account> accounts = getAccountRepository().findByUsername(account);
		for (Account temp : accounts) {
			if (temp.getUsername().equals(account)) {
				return;
			}
		}
		throw new UserNotFoundException(account);
	}

	@ResponseStatus(HttpStatus.NOT_FOUND)
	class UserNotFoundException extends RuntimeException {

		public UserNotFoundException(String userId) {
			super("could not find user '" + userId + "'.");
		}
	}
}
