package com.braintree.controllers.cms;

import com.braintree.constants.BraintreeControllerConstants;
import com.braintree.enums.BraintreePageType;
import com.braintree.facade.impl.DefaultBrainTreeCheckoutFacade;
import com.braintree.model.PayPalCreditMessageComponentB2BModel;
import com.braintree.facade.BraintreeConfigurationFacade;
import de.hybris.platform.addonsupport.controllers.cms.AbstractCMSAddOnComponentController;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.braintree.constants.Braintreeb2baddonWebConstants.IS_CREDIT_MESSAGE_ENABLED;
import static com.braintree.constants.Braintreeb2baddonWebConstants.BRAINTREE_CHECKOUT_DATA;
import static com.braintree.constants.Braintreeb2baddonWebConstants.BRAINTREE_CONFIG_DATA;


@Controller("PayPalCreditMessageComponentB2BController")
@Scope("tenant")
@RequestMapping(value = BraintreeControllerConstants.Actions.Cms.PayPalCreditMessageComponent)
public class BraintreePayPalCreditMessageComponentController extends
    AbstractCMSAddOnComponentController<PayPalCreditMessageComponentB2BModel> {

    @Resource(name = "brainTreeCheckoutFacade")
    private DefaultBrainTreeCheckoutFacade brainTreeCheckoutFacade;

    @Resource(name = "defaultBraintreeConfigurationFacade")
    private BraintreeConfigurationFacade braintreeConfigurationFacade;

    /*
     * (non-Javadoc)
     *
     * @see
     * de.hybris.platform.addonsupport.controllers.cms.AbstractCMSAddOnComponentController#fillModel(javax.servlet.http
     * .HttpServletRequest, org.springframework.ui.Model,
     * de.hybris.platform.cms2.model.contents.components.AbstractCMSComponentModel)
     */
    @Override
    protected void fillModel(final HttpServletRequest request, final Model model,
        final PayPalCreditMessageComponentB2BModel component) {
        model.addAttribute(IS_CREDIT_MESSAGE_ENABLED, component.getEnabled());
        model.addAttribute(BRAINTREE_CONFIG_DATA, braintreeConfigurationFacade.getBraintreePluginConfigurationData());
        model.addAttribute(BRAINTREE_CHECKOUT_DATA, brainTreeCheckoutFacade.generateConfigurationData(BraintreePageType.OTHER));
    }
}

