package com.braintree.customersupportbackoffice.facade.impl;

import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.customersupportbackoffice.facade.BraintreeCheckConnectionFacade;
import com.braintree.facade.BrainTreeConnectWithPayPalFacade;
import com.braintree.method.BrainTreePaymentService;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
import de.hybris.platform.servicelayer.config.ConfigurationService;
import de.hybris.platform.store.BaseStoreModel;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.stream.Collectors;

import static com.braintree.constants.BraintreeConstants.CLIENT_ID;
import static com.braintree.constants.BraintreeConstants.SECRET_KEY;
import static com.braintree.constants.BraintreeConstants.PropertyConfiguration.BRAINTREE_MERCHANT_ID;
import static com.braintree.constants.BraintreeConstants.PropertyConfiguration.BRAINTREE_PUBLIC_KEY;
import static com.braintree.constants.BraintreeConstants.PropertyConfiguration.BRAINTREE_PRIVATE_KEY;
import static com.braintree.constants.BraintreeConstants.PropertyConfiguration.BRAINTREE_ENVIRONMENT;
import static com.braintree.customersupportbackoffice.constants.BraintreeCustomerSupportBackofficeConstants.PONG;

public class DefaultBraintreeCheckConnectionFacade implements BraintreeCheckConnectionFacade {

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

    private ConfigurationService configurationService;
    private BrainTreePaymentService brainTreePaymentService;
    private BrainTreeConfigService brainTreeConfigService;
    private BrainTreeConnectWithPayPalFacade connectWithPayPalFacade;

    @Override
    public boolean isMerchantIDPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(BRAINTREE_MERCHANT_ID));
    }

    @Override
    public boolean isPublicKeyPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(BRAINTREE_PUBLIC_KEY));
    }

    @Override
    public boolean isPrivateKeyPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(BRAINTREE_PRIVATE_KEY));
    }

    @Override
    public boolean isEnvironmentPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(BRAINTREE_ENVIRONMENT));
    }

    @Override
    public boolean canCheckConnectWithBraintree() {
        return isMerchantIDPresented() && isPublicKeyPresented() && isPrivateKeyPresented() && isEnvironmentPresented();
    }

    @Override
    public boolean isBraintreePingPresented() {
        boolean result;
        try {
            String ping = brainTreePaymentService.getBraintreePing();
            result = Strings.isNotEmpty(ping) && ping.equals(PONG);
        } catch (Exception e) {
            LOG.error(String.format("Braintre is not present : %s", e.getMessage()), e);
            result = false;
        }
        return result;
    }

    @Override
    public List<String> getBaseStoreNames(BraintreePluginConfigurationModel pluginConfiguration) {
        return brainTreeConfigService.getBaseStoresByPluginConfiguration(pluginConfiguration)
                .stream()
                .map(BaseStoreModel::getName)
                .filter(StringUtils::isNotEmpty)
                .collect(Collectors.toList());
    }

    @Override
    public boolean isPayPalClientIdPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(CLIENT_ID));
    }

    @Override
    public boolean isPayPalSecretKeyPresented() {
        return Strings.isNotEmpty(configurationService.getConfiguration().getString(SECRET_KEY));
    }

    @Override
    public boolean canCheckConnectWithPayPal() {
        return isPayPalClientIdPresented() && isPayPalSecretKeyPresented();
    }

    @Override
    public boolean canEstablishConnectWithPayPal() {
        boolean result;
        try {
            String token = connectWithPayPalFacade.getAccessToken();
            result = Strings.isNotEmpty(token);
        } catch (Exception e) {
            LOG.error(String.format("Can't establish connect with payPal : %s", e.getMessage()), e);
            result = false;
        }
        return result;
    }

    public void setConfigurationService(ConfigurationService configurationService) {
        this.configurationService = configurationService;
    }

    public void setBrainTreePaymentService(BrainTreePaymentService brainTreePaymentService) {
        this.brainTreePaymentService = brainTreePaymentService;
    }

    public void setBrainTreeConfigService(BrainTreeConfigService brainTreeConfigService) {
        this.brainTreeConfigService = brainTreeConfigService;
    }

    public void setConnectWithPayPalFacade(BrainTreeConnectWithPayPalFacade connectWithPayPalFacade) {
        this.connectWithPayPalFacade = connectWithPayPalFacade;
    }

}
