/*
 * Copyright 2014-2018 Groupon, Inc
 * Copyright 2014-2018 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.payment.api.svcs;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.killbill.billing.payment.api.PaymentOptions;
import org.killbill.billing.payment.invoice.InvoicePaymentControlPluginApi;

public class InvoicePaymentPaymentOptions implements PaymentOptions {

    private final boolean isExternalPayment;
    private final List<String> paymentControlPluginNames;

    public InvoicePaymentPaymentOptions(final boolean isExternalPayment, final List<String> getPaymentControlPluginNames) {
        this.isExternalPayment = isExternalPayment;
        this.paymentControlPluginNames = getPaymentControlPluginNames;
    }

    public static InvoicePaymentPaymentOptions create(final PaymentOptions paymentOptions) {
        final List<String> controlPluginNamesFromUser = paymentOptions.getPaymentControlPluginNames();
        final List<String> paymentControlPluginNames = addInvoicePaymentControlPlugin(controlPluginNamesFromUser);
        return new InvoicePaymentPaymentOptions(paymentOptions.isExternalPayment(), paymentControlPluginNames);
    }

    public static List<String> addInvoicePaymentControlPlugin(final Collection<String> controlPluginNamesFromUser) {
        final List<String> paymentControlPluginNames = new LinkedList<String>();
        // Remove InvoicePaymentControlPluginApi.PLUGIN_NAME if already in the list
        if (paymentControlPluginNames.contains(InvoicePaymentControlPluginApi.PLUGIN_NAME)) {
            paymentControlPluginNames.remove(InvoicePaymentControlPluginApi.PLUGIN_NAME);
        }
        // ... and add it FIRST
        paymentControlPluginNames.add(InvoicePaymentControlPluginApi.PLUGIN_NAME);
        paymentControlPluginNames.addAll(controlPluginNamesFromUser);
        return paymentControlPluginNames;
    }

    @Override
    public boolean isExternalPayment() {
        return isExternalPayment;
    }

    @Override
    public List<String> getPaymentControlPluginNames() {
        return paymentControlPluginNames;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("InvoicePaymentPaymentOptions{");
        sb.append("isExternalPayment=").append(isExternalPayment);
        sb.append(", paymentControlPluginNames=").append(paymentControlPluginNames);
        sb.append('}');
        return sb.toString();
    }
}

@@ -2017,14 +2017,11 @@
  // 
-Remove
+Add
  Inv
@@ -2064,30 +2064,13 @@
 AME 
-if already in the list
+FIRST
 %0A   
@@ -2078,12 +2078,8 @@
     
-if (
 paym
@@ -2104,16 +2104,11 @@
 mes.
-contains
+add
 (Inv
@@ -2147,19 +2147,17 @@
 IN_NAME)
-) %7B
+;
 %0A       
@@ -2161,44 +2161,96 @@
     
-    paymentControlPluginNames.remove
+for (String userPlugin : controlPluginNamesFromUser) %7B%0A            if (userPlugin.equals
 (Inv
@@ -2281,33 +2281,35 @@
 Api.PLUGIN_NAME)
-;
+) %7B
 %0A        %7D%0A     
@@ -2301,18 +2301,16 @@
         
-%7D%0A
         
@@ -2316,181 +2316,155 @@
  // 
-... and add it FIRST%0A        paymentControlPluginNames.add(InvoicePaymentControlPluginApi.PLUGIN_NAME);%0A        paymentControlPluginNames.addAll(controlPluginNamesFromUser);
+skip in case it was part of the input list%0A                continue;%0A            %7D%0A            paymentControlPluginNames.add(userPlugin);%0A        %7D
 %0A   

Fix issue with control plugins insertion list introduced in 3d411ecee93. See #1276