/*
 * Copyright 2014-2015 Groupon, Inc
 * Copyright 2014-2015 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.plugin.accertify.core;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import org.killbill.billing.control.plugin.api.PaymentControlPluginApi;
import org.killbill.billing.osgi.api.OSGIPluginProperties;
import org.killbill.billing.osgi.libs.killbill.KillbillActivatorBase;
import org.killbill.billing.plugin.accertify.api.AccertifyPaymentControlPluginApi;
import org.killbill.billing.plugin.accertify.client.AccertifyClient;
import org.killbill.billing.plugin.accertify.dao.AccertifyDao;
import org.killbill.billing.plugin.api.notification.PluginConfigurationEventHandler;
import org.killbill.clock.Clock;
import org.killbill.clock.DefaultClock;
import org.osgi.framework.BundleContext;

import java.util.Collection;
import java.util.Hashtable;

public class AccertifyActivator extends KillbillActivatorBase {

    public static final String PLUGIN_NAME = "killbill-accertify";

    public static final String PROPERTY_PREFIX = "org.killbill.billing.plugin.accertify.";

    private AccertifyConfigurationHandler accertifyConfigurationHandler;

    @Override
    public void start(final BundleContext context) throws Exception {
        super.start(context);

        final AccertifyClient globalAccertifyClient = accertifyConfigurationHandler.createConfigurable(configProperties.getProperties());
        accertifyConfigurationHandler.setDefaultConfigurable(globalAccertifyClient);

        // Configurable globally only
        final String paymentPluginsString = configProperties.getString(PROPERTY_PREFIX + "plugins");
        final String[] paymentPlugins = Strings.isNullOrEmpty(paymentPluginsString) ? new String[]{} : paymentPluginsString.split(",");
        final Collection<String> paymentPluginsSubjectToAutomaticRejection = ImmutableList.<String>copyOf(paymentPlugins);

        final AccertifyDao dao = new AccertifyDao(dataSource.getDataSource());
        final Clock clock = new DefaultClock();

        // Register the PaymentControlPluginApi
        final PaymentControlPluginApi paymentControlPluginApi = new AccertifyPaymentControlPluginApi(paymentPluginsSubjectToAutomaticRejection,
                dao,
                accertifyConfigurationHandler,
                killbillAPI,
                configProperties,
                logService,
                clock);
        registerPaymentControlPluginApi(context, paymentControlPluginApi);

        registerEventHandler();
    }

    private void registerEventHandler() {
        final AccertifyConfigurationHandler handler = accertifyConfigurationHandler = new AccertifyConfigurationHandler(PLUGIN_NAME, killbillAPI, logService);
        dispatcher.registerEventHandlers(new PluginConfigurationEventHandler(handler));

    }

    private void registerPaymentControlPluginApi(final BundleContext context, final PaymentControlPluginApi api) {
        final Hashtable<String, String> props = new Hashtable<String, String>();
        props.put(OSGIPluginProperties.PLUGIN_NAME_PROP, PLUGIN_NAME);
        registrar.registerService(context, PaymentControlPluginApi.class, api, props);
    }
}

@@ -17,17 +17,17 @@
 2014-201
-5
+6
  Groupon
@@ -57,9 +57,9 @@
 -201
-5
+6
  The
@@ -746,87 +746,58 @@
 ort 
-com.google.common.base.Strings;%0Aimport com.google.common.collect.ImmutableList;
+java.util.Collection;%0Aimport java.util.Hashtable;%0A
 %0Aimp
@@ -1420,56 +1420,86 @@
 ort 
-java.util.Collection;%0Aimport java.util.Hash
+com.google.common.base.Strings;%0Aimport com.google.common.collect.Immu
 table
+List
 ;%0A%0Ap
@@ -1907,24 +1907,138 @@
 (context);%0A%0A
+        accertifyConfigurationHandler = new AccertifyConfigurationHandler(PLUGIN_NAME, killbillAPI, logService);%0A%0A
         fina
@@ -2988,151 +2988,661 @@
     
-dao,%0A                accertifyConfigurationHandler,%0A                killbillAPI,%0A                configProperties,%0A                logService,%0A
+                                                                                     dao,%0A                                                                                                     accertifyConfigurationHandler,%0A                                                                                                     killbillAPI,%0A                                                                                                     configProperties,%0A                                                                                                     logService,%0A                                                                                     
     
@@ -3832,25 +3832,22 @@
   final 
-Accertify
+Plugin
 Configur
@@ -3843,32 +3843,37 @@
 ginConfiguration
+Event
 Handler handler 
@@ -3874,25 +3874,26 @@
 ndler = 
-accertify
+new Plugin
 Configur
@@ -3893,39 +3893,38 @@
 onfiguration
+Event
 Handler
- = new A
+(a
 ccertifyConf
@@ -3943,45 +3943,8 @@
 dler
-(PLUGIN_NAME, killbillAPI, logService
 );%0A 
@@ -3987,52 +3987,16 @@
 ers(
-new PluginConfigurationEventHandler(
 handler)
 );%0A%0A
@@ -3991,17 +3991,16 @@
 handler)
-)
 ;%0A%0A    %7D

fix NPE on startup