package io.ljunggren.exchange.rate.api;

import org.apache.commons.lang3.StringUtils;

import io.ljunggren.exchange.rate.api.exception.ExchangeRateException;
import io.ljunggren.exchange.rate.api.model.ExchangeRate;
import io.ljunggren.exchange.rate.api.model.SupportedCodes;
import io.ljunggren.exchange.rate.api.service.ExchangeRateService;
import io.ljunggren.exchange.rate.api.service.SupportedCodesService;

public class ExchangeRateApi {
	
	private ExchangeRateService exchangeRateService;
	private SupportedCodesService supportedCodesService;
	
	public ExchangeRateApi(String apiKey) {
		this.exchangeRateService = new ExchangeRateService();
		this.supportedCodesService = new SupportedCodesService(apiKey);
	}
	
	public ExchangeRate getByCurrency(String currency) throws ExchangeRateException {
		if (StringUtils.isBlank(currency)) {
			throw new ExchangeRateException("Currency is required");
		}
		if (!currency.matches("^[a-zA-Z]{3}$")) {
			throw new ExchangeRateException("Currency must be 3 letters");
		}
		ExchangeRate exchangeRate = exchangeRateService.getByCurrency(currency);
		return exchangeRateService.addCurrencyMappings(exchangeRate);
	}
	
	public SupportedCodes getSupportedCodes() throws ExchangeRateException {
		SupportedCodes supportedCodes = supportedCodesService.getSupportedCodes();
		return supportedCodesService.addCurrencyMappings(supportedCodes);
	}
	
}
