class PortofolioService {

	public Portofolio getPortofolio(String login) {
		PortofolioEntity portofolio = portofolioRepo.findByUserUsername(login);

		AssetInfo baseCurrencyAsset = new AssetInfo(portofolio.getBaseCurrency(), null, portofolio.getAmount().doubleValue());
		
		List<AssetInfo> assets = new ArrayList<>();

		double globalBaseCurrencyValueChange = 0f;
		double globalBaseCurrencyInvestedValue = 0f;

		for (AssetEntity ae : portofolio.getAssets()) {

			AssetInfo asset = getAsset(ae);

			String currencyCode = asset.getCurrency().getCode();

			BigDecimal assetCurrencyToBaseCurrencyRate = rateService.getLatest(currencyCode, portofolio.getBaseCurrency().getCode()).getValue();
			BigDecimal baseCurrencyValueChange = assetCurrencyToBaseCurrencyRate.multiply(BigDecimal.valueOf(asset.getValueChange()));
			BigDecimal baseCurrencyInvestedValue = assetCurrencyToBaseCurrencyRate.multiply(BigDecimal.valueOf(asset.getValue()));

			globalBaseCurrencyValueChange += baseCurrencyValueChange.doubleValue();
			globalBaseCurrencyInvestedValue += baseCurrencyInvestedValue.doubleValue();

			assets.add(asset);
		}

		double globalPercentChange = 0;

		if (globalBaseCurrencyInvestedValue != 0) {
			globalPercentChange = globalBaseCurrencyValueChange / globalBaseCurrencyInvestedValue * 100d;
		}

		double globalBaseCurrencyValue = globalBaseCurrencyInvestedValue + globalBaseCurrencyValueChange;

		return new Portofolio(baseCurrencyAsset, assets, globalBaseCurrencyValue, globalPercentChange, globalBaseCurrencyValueChange);
	}

}