package com.braintree.order.strategy;

import de.hybris.platform.commerceservices.order.impl.DefaultCommerceCartRestorationStrategy;
import de.hybris.platform.commerceservices.service.data.CommerceCartParameter;
import de.hybris.platform.core.model.order.CartModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultBraintreeCommerceCartRestorationStrategy extends DefaultCommerceCartRestorationStrategy {

    private static final Logger LOG = LoggerFactory.getLogger(DefaultBraintreeCommerceCartRestorationStrategy.class);

    @Override
    protected void recalculateCartIfNotIgnored(CommerceCartParameter parameter) {
        if (!parameter.isIgnoreRecalculation()) {
            final CartModel cartModel = parameter.getCart();
            cartModel.setCalculated(Boolean.FALSE);
            cartModel.setCurrency(getCommerceCommonI18NService().getCurrentCurrency());
            getModelService().save(cartModel);
            try {
                getCommerceCartCalculationStrategy().recalculateCart(parameter);
            } catch (final IllegalStateException ex) {
                LOG.error("Failed to recalculate order [{}]", cartModel.getCode(), ex);
            }
        }
    }
}
