package com.imoiseyenko.spastructure.backend.service.profile.converter;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import com.imoiseyenko.spastructure.backend.repository.entity.Account;
import com.imoiseyenko.spastructure.backend.service.profile.vo.AccountVO;

/**
 * Converter implementation for the {@link AccountVO} value object to the {@link Account} entity
 * instance and vice versa.
 * 
 * @author imoiseyenko93@gmail.com
 * @version Mar 31, 2014
 */
@Component
public class AccountVOConverterBean implements AccountVOConverter {

	/**
	 * 
	 * {@inheritDoc}
	 * 
	 * @author imoiseyenko93@gmail.com
	 */
	@Override
	public AccountVO convertAccountToVO (Account account) {

		AccountVO accountVO = null;

		if (account != null) {

			accountVO = new AccountVO();
			accountVO.setId(account.getId());
			accountVO.setUsername(account.getUsername());
			accountVO.setPassword(account.getPassword());
		}

		return accountVO;
	}

	/**
	 * 
	 * {@inheritDoc}
	 * 
	 * @author imoiseyenko93@gmail.com
	 */
	@Override
	public List<AccountVO> convertAccountsToVO (List<Account> accounts) {

		List<AccountVO> accountsVO = new ArrayList<>();

		if (accounts != null && accounts.size() > 0) {
			for (Account account : accounts) {
				accountsVO.add(convertAccountToVO(account));
			}
		}

		return accountsVO;
	}

	/**
	 * 
	 * {@inheritDoc}
	 * 
	 * @author imoiseyenko93@gmail.com
	 */
	@Override
	public Account convertAccountVOToEntity (AccountVO accountVO) {

		Account account = null;

		if (accountVO != null) {

			account = new Account();
			account.setId(accountVO.getId());
			account.setUsername(accountVO.getUsername());
			account.setPassword(accountVO.getPassword());
		}

		return account;
	}

	/**
	 * 
	 * {@inheritDoc}
	 * 
	 * @author imoiseyenko93@gmail.com
	 */
	@Override
	public List<Account> convertAccountsVOToEntities (List<AccountVO> accountsVO) {

		List<Account> accounts = new ArrayList<>();

		if (accountsVO != null && accountsVO.size() > 0) {
			for (AccountVO accountVO : accountsVO) {
				accounts.add(convertAccountVOToEntity(accountVO));
			}
		}

		return accounts;
	}
}
