/**
 *
 */
package com.braintreeb2bocc.controllers;


import com.braintree.enums.BraintreePageType;
import com.braintree.facade.BrainTreeB2BOrderFacade;
import com.braintree.facade.impl.DefaultBrainTreeCheckoutFacade;
import com.braintree.hybris.data.BraintreeCheckoutData;
import com.braintree.hybris.data.ws.BraintreeB2BAssetsWsDTO;
import com.braintree.hybris.data.ws.PayPalCheckoutB2BWsDTO;
import com.braintreeocc.controllers.BraintreeBaseController;
import de.hybris.platform.commerceservices.request.mapping.annotation.RequestMappingOverride;
import de.hybris.platform.webservicescommons.swagger.ApiBaseSiteIdParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping(value = "/{baseSiteId}")
@Api(tags = "Braintree Payment")
public class BraintreeB2BPaymentController extends BraintreeBaseController {

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

    @Resource(name = "brainTreeB2BOrderFacade")
    private BrainTreeB2BOrderFacade brainTreeB2BOrderFacade;

    @GetMapping("/users/{userId}/braintree/payment")
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    @ApiBaseSiteIdParam
    @ApiOperation(value = " Get the Braintree data for client-side payment initialization",
        notes = "Get a data for all payment methods")
    @RequestMappingOverride
    public PayPalCheckoutB2BWsDTO getPaymentData(
        @ApiParam(value = "Payment method identifier.", allowableValues = "paypal, googlepay, venmo, applepay," +
            " localpayments, creditcard, configData, full", required = true) @RequestParam final String paymentMethod,
        @ApiParam(value = "Page type", defaultValue = "MARK")
        @RequestParam(defaultValue = "MARK", required = false) final String pageType) {
        final BraintreePageType braintreePageType = BraintreePageType.valueOf(pageType);
        final BraintreeCheckoutData braintreeCheckoutData = brainTreeCheckoutFacade.getBraintreeCheckoutData(braintreePageType);
        braintreeCheckoutData.getDropIn().setShowWarningMessageAboutReplenishmentDropIn(
            brainTreeB2BOrderFacade.isCustomerHasActiveReplenishmentForSavedPayPalOrCreditCard());
        return getDataMapper().map(braintreeCheckoutData, PayPalCheckoutB2BWsDTO.class, paymentMethod);
    }
    @GetMapping("/braintree/payment/assets")
    @ResponseStatus(value = HttpStatus.OK)
    @ResponseBody
    @ApiBaseSiteIdParam
    @ApiOperation(value = " Get assets for checkout.")
    @RequestMappingOverride
    public BraintreeB2BAssetsWsDTO getAssets() {
        return getDataMapper().map(brainTreeCheckoutFacade.getBraintreeAssets(), BraintreeB2BAssetsWsDTO.class);
    }
}
