package com.braintree.converters.populators.configuration;

import com.braintree.enums.BraintreePaymentMethodLimitData;
import com.braintree.hybris.data.BraintreeACHConfigurationData;
import de.hybris.platform.converters.Populator;
import de.hybris.platform.core.model.BraintreeACHConfigurationModel;
import de.hybris.platform.core.model.c2l.CurrencyModel;
import de.hybris.platform.servicelayer.dto.converter.ConversionException;
import de.hybris.platform.servicelayer.session.SessionService;

import static com.braintree.constants.BraintreeConstants.CURRENCY;
import static com.braintree.constants.BraintreeConstants.USD;

public class BraintreeACHConfigurationPopulator implements Populator<BraintreeACHConfigurationModel, BraintreeACHConfigurationData> {

    private SessionService sessionService;

    @Override
    public void populate(BraintreeACHConfigurationModel source, BraintreeACHConfigurationData target) throws ConversionException {
        final CurrencyModel currencyModel = sessionService.getCurrentSession().getAttribute(CURRENCY);
        target.setEnabled(source.isEnabled() && currencyModel.getIsocode().equalsIgnoreCase(USD));
        target.setMerchantName(source.getMerchantName());
        target.setSavedACHAccountLimit(BraintreePaymentMethodLimitData.valueOf(source.getSavedACHAccountLimit().getCode()));
    }

    public void setSessionService(SessionService sessionService) {
        this.sessionService = sessionService;
    }
}
