package com.mercadopago.model;

import com.mercadopago.constants.PaymentTypes;

import java.util.List;

/**
 * Created by mreverter on 15/1/16.
 */
public class PaymentMethodSearch {

    private List<PaymentMethodSearchItem> groups;

    private List<PaymentMethod> paymentMethods;

    public List<PaymentMethodSearchItem> getGroups() {
        return groups;
    }

    public List<PaymentMethod> getPaymentMethods() {
        return paymentMethods;
    }

    public boolean hasSearchItems() {
        return this.groups != null && !this.groups.isEmpty();
    }

    public PaymentMethod getPaymentMethodBySearchItem(PaymentMethodSearchItem item) {
        PaymentMethod requiredPaymentMethod = null;
        if(paymentMethods != null && item != null && item.getId() != null) {
            for (PaymentMethod currentPaymentMethod : paymentMethods) {
                if (itemMatchesPaymentMethod(item, currentPaymentMethod)) {
                    requiredPaymentMethod = currentPaymentMethod;
                    requiredPaymentMethod.setPaymentTypeId(getPaymentTypeIdFromItem(item, currentPaymentMethod));
                }
            }
        }
        return requiredPaymentMethod;
    }

    private String getPaymentTypeIdFromItem(PaymentMethodSearchItem item, PaymentMethod paymentMethod) {

        String paymentType = "";

        //Remove payment method id from item id
        String potentialPaymentType = item.getId().replace(paymentMethod.getId(), "");
        for(String currentPaymentType : PaymentTypes.getAllPaymentTypes()) {
            if(potentialPaymentType.endsWith(currentPaymentType)){
                paymentType = currentPaymentType;
                break;
            }
        }
        if(paymentType.isEmpty()) {
            paymentType = paymentMethod.getPaymentTypeId();
        }
        return paymentType;
    }

    private boolean itemMatchesPaymentMethod(PaymentMethodSearchItem item, PaymentMethod paymentMethod) {
        return item.getId().startsWith(paymentMethod.getId());
    }

    public PaymentMethodSearchItem getSearchItemByPaymentMethod(PaymentMethod selectedPaymentMethod) {
        PaymentMethodSearchItem requiredItem = null;
        if(selectedPaymentMethod != null) {

            requiredItem = searchItemMatchingPaymentMethod(selectedPaymentMethod);

        }
        return requiredItem;
    }

    private PaymentMethodSearchItem searchItemMatchingPaymentMethod(PaymentMethod paymentMethod) {
        String potentialItemId = paymentMethod.getId() + "_" + paymentMethod.getPaymentTypeId();
        String paymentMethodId = paymentMethod.getId();
        return searchItemInList(groups, potentialItemId, paymentMethodId);
    }

    private PaymentMethodSearchItem searchItemInList(List<PaymentMethodSearchItem> list, String potentialItemId, String paymentMethodId) {
        PaymentMethodSearchItem requiredItem = null;
        for(PaymentMethodSearchItem currentItem : list) {

            if(currentItem.getId().equals(paymentMethodId)
                    || currentItem.getId().equals(potentialItemId)) {
                requiredItem = currentItem;
                break;
            }
            else if(currentItem.hasChildren()) {
                requiredItem = searchItemInList(currentItem.getChildren(), potentialItemId, paymentMethodId);
                if(requiredItem != null) {
                    break;
                }
            }
        }
        return requiredItem;
    }
}

@@ -2457,161 +2457,8 @@
 ) %7B%0A
-        String potentialItemId = paymentMethod.getId() + %22_%22 + paymentMethod.getPaymentTypeId();%0A        String paymentMethodId = paymentMethod.getId();%0A
     
@@ -2492,33 +2492,16 @@
 (groups,
- potentialItemId,
  payment
@@ -2502,26 +2502,24 @@
 aymentMethod
-Id
 );%0A    %7D%0A%0A  
@@ -2609,38 +2609,21 @@
 st, 
-String potentialItemId, String
+PaymentMethod
  pay
@@ -2632,18 +2632,16 @@
 ntMethod
-Id
 ) %7B%0A    
@@ -2760,19 +2760,153 @@
         
-if(
+//Case like %22pagofacil%22, without the payment type in the item id.%0A            if(itemMatchesPaymentMethod(currentItem, paymentMethod) && 
 currentI
@@ -2937,19 +2937,28 @@
 ntMethod
-Id)
+.getId())) %7B
 %0A       
@@ -2970,62 +2970,477 @@
     
-    %7C%7C currentItem.getId().equals(potentialItem
+requiredItem = currentItem;%0A                break;%0A            %7D%0A            //Case like %22bancomer_ticker%22, with the payment type in the item id%0A            else if(itemMatchesPaymentMethod(currentItem, paymentMethod)) %7B%0A                //Remove payment method id from item id%0A                String potentialPaymentType = currentItem.getId().replace(paymentMethod.getId(), %22%22);%0A                if(potentialPaymentType.endsWith(paymentMethod.getPaymentType
 Id
+(
 ))
+)
  %7B%0A
+    
     
@@ -3487,39 +3487,61 @@
                 
+    
 break;%0A
+                %7D%0A
             %7D%0A  
@@ -3665,25 +3665,8 @@
 n(),
- potentialItemId,
  pay
@@ -3675,18 +3675,16 @@
 ntMethod
-Id
 );%0A     

refactor of method to get a search item given a payment method