package com.braintree.facade.order.converters.populator;

import com.braintree.model.BrainTreePaymentInfoModel;
import de.hybris.platform.b2bacceleratorfacades.order.populators.B2BCartPopulator;
import de.hybris.platform.commercefacades.order.data.AbstractOrderData;
import de.hybris.platform.commercefacades.order.data.CCPaymentInfoData;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import de.hybris.platform.core.model.order.payment.CreditCardPaymentInfoModel;
import de.hybris.platform.core.model.order.payment.PaymentInfoModel;
import de.hybris.platform.servicelayer.dto.converter.Converter;

public class B2BBrainTreeCartPopulator extends B2BCartPopulator {

    private Converter<BrainTreePaymentInfoModel, CCPaymentInfoData> brainTreePaymentInfoConverter;

    @Override
    protected void addPaymentInformation(final AbstractOrderModel source, final AbstractOrderData prototype) {
        final PaymentInfoModel paymentInfo = source.getPaymentInfo();
        if (paymentInfo instanceof CreditCardPaymentInfoModel) {
            final CCPaymentInfoData paymentInfoData = (CCPaymentInfoData) getCreditCardPaymentInfoConverter()
                    .convert((CreditCardPaymentInfoModel) paymentInfo);
            prototype.setPaymentInfo(paymentInfoData);
        }
        if (paymentInfo instanceof BrainTreePaymentInfoModel) {
            final CCPaymentInfoData paymentInfoData = getBrainTreePaymentInfoConverter()
                    .convert((BrainTreePaymentInfoModel) paymentInfo);
            prototype.setPaymentInfo(paymentInfoData);
        }
    }

    public Converter<BrainTreePaymentInfoModel, CCPaymentInfoData> getBrainTreePaymentInfoConverter() {
        return brainTreePaymentInfoConverter;
    }

    public void setBrainTreePaymentInfoConverter(
            final Converter<BrainTreePaymentInfoModel, CCPaymentInfoData> brainTreePaymentInfoConverter) {
        this.brainTreePaymentInfoConverter = brainTreePaymentInfoConverter;
    }
}
