/*******************************************************************************
 *  Copyright FUJITSU LIMITED 2016 
 *******************************************************************************/

package org.oscm.test.stubs;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.oscm.configurationservice.assembler.ConfigurationSettingAssembler;
import org.oscm.configurationservice.local.ConfigurationServiceLocal;
import org.oscm.converter.DateConverter;
import org.oscm.domobjects.ConfigurationSetting;
import org.oscm.types.constants.Configuration;
import org.oscm.internal.intf.ConfigurationService;
import org.oscm.internal.types.enumtypes.ConfigurationKey;
import org.oscm.internal.vo.VOConfigurationSetting;

/**
 * Simple stub that hold config settings in-memory.
 * 
 * @author hoffmann
 */
public class ConfigurationServiceStub implements ConfigurationService,
        ConfigurationServiceLocal {

    private final Map<ConfigurationKey, ConfigurationSetting> settings = new HashMap<ConfigurationKey, ConfigurationSetting>();

    @Override
    public VOConfigurationSetting getVOConfigurationSetting(
            ConfigurationKey informationId, String contextId) {
        ConfigurationSetting setting = getConfigurationSetting(informationId,
                contextId);
        return ConfigurationSettingAssembler.toValueObject(setting);
    }

    @Override
    public ConfigurationSetting getConfigurationSetting(
            ConfigurationKey informationId, String contextId) {
        final ConfigurationSetting vo = settings.get(informationId);

        if (vo != null) {
            return vo;
        }

        return new ConfigurationSetting(informationId,
                Configuration.GLOBAL_CONTEXT, informationId.getFallBackValue());
    }

    @Override
    public long getLongConfigurationSetting(ConfigurationKey informationId,
            String contextId) {
        long configValue = 0;

        if (informationId != null) {
            final ConfigurationSetting setting = getConfigurationSetting(
                    informationId, contextId);
            final String value = setting.getValue();
            if (value != null) {
                configValue = Long.parseLong(value);
            }
        }

        return configValue;
    }

    @Override
    public String getNodeName() {
        return "UnitTestNode";
    }

    @Override
    public void setConfigurationSetting(ConfigurationSetting configSetting) {
        settings.put(configSetting.getInformationId(), configSetting);
    }

    @Override
    public void setConfigurationSetting(String informationId, String value) {
        setConfigurationSetting(ConfigurationKey.valueOf(informationId), value);
    }

    public void setConfigurationSetting(ConfigurationKey key, String value) {
        ConfigurationSetting configSetting = new ConfigurationSetting(key,
                Configuration.GLOBAL_CONTEXT, value);
        settings.put(configSetting.getInformationId(), configSetting);
    }

    @Override
    public List<ConfigurationSetting> getAllConfigurationSettings() {
        return null;
    }

    @Override
    public boolean isCustomerSelfRegistrationEnabled() {
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.oscm.internal.intf.ConfigurationService#isServiceProvider()
     */
    @Override
    public boolean isServiceProvider() {
        return false;
    }

    @Override
    public long getBillingRunOffsetInMs() {
        long configuredValue = getConfiguredBillingOffsetInMs();
        return getConfiguredDaysInMs(configuredValue);
    }

    @Override
    public long getBillingRunStartTimeInMs() {
        long configuredValue = getConfiguredBillingOffsetInMs();
        long configuredDays = getConfiguredDaysInMs(configuredValue);
        return configuredValue - configuredDays;
    }

    long getConfiguredDaysInMs(long configuredBillingOffset) {
        BigDecimal configuredDays = null;
        if (configuredBillingOffset < 0) {
            configuredDays = new BigDecimal(0);
        } else {
            BigDecimal oneDay = new BigDecimal(
                    DateConverter.MILLISECONDS_PER_DAY);
            configuredDays = new BigDecimal(configuredBillingOffset).divide(
                    oneDay, RoundingMode.DOWN);
        }
        return configuredDays.longValue() * DateConverter.MILLISECONDS_PER_DAY;
    }

    long getConfiguredBillingOffsetInMs() {
        long configuredValue = getLongConfigurationSetting(
                ConfigurationKey.TIMER_INTERVAL_BILLING_OFFSET,
                Configuration.GLOBAL_CONTEXT);

        long maxAllowedValue = 28 * DateConverter.MILLISECONDS_PER_DAY;

        if (configuredValue < 0) {
            configuredValue = 0;
        } else if (configuredValue > maxAllowedValue) {
            configuredValue = maxAllowedValue;
        }
        return configuredValue;
    }

    @Override
    public String getBaseURL() {
        return getConfigurationSetting(ConfigurationKey.BASE_URL,
                Configuration.GLOBAL_CONTEXT).getValue();
    }

    @Override
    public boolean isPaymentInfoAvailable() {
        return true;
    }

}

@@ -597,55 +597,8 @@
 ng;%0A
-import org.oscm.types.constants.Configuration;%0A
 impo
@@ -755,16 +755,63 @@
 Setting;
+%0Aimport org.oscm.types.constants.Configuration;
 %0A%0A/**%0A *
@@ -1573,32 +1573,240 @@
 ng contextId) %7B%0A
+        if (ConfigurationKey.SSO_DEFAULT_TENANT_ID.equals(informationId)) %7B%0A            return new ConfigurationSetting(informationId,%0A                    Configuration.GLOBAL_CONTEXT, %228f96dede%22);%0A        %7D%0A
         final Co
@@ -3551,23 +3551,16 @@
   * @see
-%0A     *
  org.osc

Return default tenant