package com.braintree.facade.impl;

import com.braintree.enums.BraintreePageType;
import com.braintree.facade.BrainTreeStylesConfigurationFacade;
import com.braintree.hybris.data.ApplePayButtonStyleData;
import com.braintree.hybris.data.GooglePayButtonStyleData;
import com.braintree.hybris.data.PayPalButtonStyleData;
import com.braintree.services.BrainTreeStylesConfigurationService;
import de.hybris.platform.core.model.BraintreeGooglePayStylesConfigurationModel;
import de.hybris.platform.core.model.BraintreeApplePayStylesConfigurationModel;
import de.hybris.platform.core.model.BraintreePayPalStylesConfigurationModel;
import de.hybris.platform.servicelayer.dto.converter.Converter;

import java.util.Optional;

public class DefaultBrainTreeStylesConfigurationFacade implements BrainTreeStylesConfigurationFacade {

    private BrainTreeStylesConfigurationService stylesConfigurationService;
    private Converter<BraintreeApplePayStylesConfigurationModel, ApplePayButtonStyleData>
            applePayButtonStyleDataConverter;
    private Converter<BraintreeGooglePayStylesConfigurationModel, GooglePayButtonStyleData>
            googlePayButtonStyleDataConverter;
    private Converter<BraintreePayPalStylesConfigurationModel, PayPalButtonStyleData>
            payPalButtonStyleDataConverter;

    @Override
    public Optional<ApplePayButtonStyleData> getApplePayStyleConfigurationFor(BraintreePageType page) {
        return stylesConfigurationService.getApplePayStyleConfigurations().stream()
                        .filter(style -> style.getLocation().name().equalsIgnoreCase(page.name()))
                        .findFirst()
                        .map(applePayButtonStyleDataConverter::convert);
    }

    public Optional<GooglePayButtonStyleData> getGooglePayStyleConfiguration(BraintreePageType page) {
        return stylesConfigurationService.getGooglePayStyleConfigurations()
                        .stream()
                        .filter(style -> style.getLocation().name().equalsIgnoreCase(page.name()))
                        .findFirst()
                        .map(googlePayButtonStyleDataConverter::convert);
    }

    @Override
    public Optional<PayPalButtonStyleData> getPayPalStyleConfiguration(BraintreePageType page) {
        return stylesConfigurationService.getPayPalStyleConfigurations().stream()
                .filter(style -> style.getLocation().name().equalsIgnoreCase(page.name()))
                .findFirst()
                .map(payPalButtonStyleDataConverter::convert);
    }

    public void setStylesConfigurationService(
            BrainTreeStylesConfigurationService stylesConfigurationService) {
        this.stylesConfigurationService = stylesConfigurationService;
    }

    public void setApplePayButtonStyleDataConverter(
            Converter<BraintreeApplePayStylesConfigurationModel, ApplePayButtonStyleData> applePayButtonStyleDataConverter) {
        this.applePayButtonStyleDataConverter = applePayButtonStyleDataConverter;
    }

    public void setGooglePayButtonStyleDataConverter(Converter<BraintreeGooglePayStylesConfigurationModel, GooglePayButtonStyleData> googlePayButtonStyleDataConverter) {
        this.googlePayButtonStyleDataConverter = googlePayButtonStyleDataConverter;
    }

    public void setPayPalButtonStyleDataConverter(Converter<BraintreePayPalStylesConfigurationModel, PayPalButtonStyleData> payPalButtonStyleDataConverter) {
        this.payPalButtonStyleDataConverter = payPalButtonStyleDataConverter;
    }
}
