package com.braintree.daos.impl;

import com.braintree.daos.BrainTreeBaseStoreDao;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreePayPalConfigurationModel;
import de.hybris.platform.core.model.BraintreePluginConfigurationModel;
import de.hybris.platform.servicelayer.internal.dao.AbstractItemDao;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.store.BaseStoreModel;

import java.util.List;

public class DefaultBrainTreeBaseStoreDao extends AbstractItemDao implements BrainTreeBaseStoreDao {

    private static final String FIND_BASESTORE_BY_PAYPALCONFIGURATION = "SELECT {" + BaseStoreModel.PK + "} " +
            "FROM {" + BaseStoreModel._TYPECODE + " as bs " +
            "JOIN " + BraintreePluginConfigurationModel._TYPECODE + " as pc ON {bs.braintreePluginConfiguration} = {pc.pk} " +
            "JOIN " + BraintreePayPalConfigurationModel._TYPECODE + " as ppc ON {pc.braintreePayPalConfiguration} = {ppc.pk}} " +
            "WHERE {ppc.braintreePayPalConfigurationId} = ?" + BraintreePayPalConfigurationModel.BRAINTREEPAYPALCONFIGURATIONID;

    private static final String FIND_BASE_STORE_BY_GENERAL_CONFIGURATION = "SELECT {" + BaseStoreModel.PK + "} " +
            "FROM {" + BaseStoreModel._TYPECODE + " as bs " +
            "JOIN " + BraintreePluginConfigurationModel._TYPECODE + " as pc ON {bs.braintreePluginConfiguration} = {pc.pk} " +
            "JOIN " + BraintreeGeneralConfigurationModel._TYPECODE + " as ppc ON {pc.braintreeGeneralConfiguration} = {ppc.pk}} " +
            "WHERE {ppc.braintreeGeneralConfigurationId} = ?" + BraintreeGeneralConfigurationModel.BRAINTREEGENERALCONFIGURATIONID;

    private static final String FIND_BASE_STORES_BY_PLUGIN_CONFIGURATION = "SELECT {" + BaseStoreModel.PK + "} " +
            "FROM {" + BaseStoreModel._TYPECODE + " as bs " +
            "JOIN " + BraintreePluginConfigurationModel._TYPECODE + " as pc ON {bs.braintreePluginConfiguration} = {pc.pk}} " +
            "WHERE {pc.braintreePluginConfigurationId} = ?" + BraintreePluginConfigurationModel.BRAINTREEPLUGINCONFIGURATIONID;

    @Override
    public List<BaseStoreModel> findBaseStoresByPayPalConfiguration(String braintreePayPalConfigurationId) {
        FlexibleSearchQuery query = new FlexibleSearchQuery(FIND_BASESTORE_BY_PAYPALCONFIGURATION);
        query.addQueryParameter(BraintreePayPalConfigurationModel.BRAINTREEPAYPALCONFIGURATIONID, braintreePayPalConfigurationId);

        return getFlexibleSearchService().<BaseStoreModel>search(query).getResult();
    }

    @Override
    public List<BaseStoreModel> findBaseStoresByGeneralConfiguration(String braintreeGeneralConfigurationId) {
        FlexibleSearchQuery query = new FlexibleSearchQuery(FIND_BASE_STORE_BY_GENERAL_CONFIGURATION);
        query.addQueryParameter(BraintreeGeneralConfigurationModel.BRAINTREEGENERALCONFIGURATIONID, braintreeGeneralConfigurationId);

        return getFlexibleSearchService().<BaseStoreModel>search(query).getResult();
    }

    @Override
    public List<BaseStoreModel> findBaseStoresByPluginConfiguration(BraintreePluginConfigurationModel pluginConfiguration) {
        FlexibleSearchQuery query = new FlexibleSearchQuery(FIND_BASE_STORES_BY_PLUGIN_CONFIGURATION);
        query.addQueryParameter(BraintreePluginConfigurationModel.BRAINTREEPLUGINCONFIGURATIONID, pluginConfiguration.getBraintreePluginConfigurationId());

        return getFlexibleSearchService().<BaseStoreModel>search(query).getResult();
    }
}
