package io.intercom.api;

import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;

class CompanyUpdateBuilder {

    /**
     * Provide restrictions on the company data that can be sent via a user update
     */
    static List<CompanyWithStringPlan> buildUserUpdateCompanies(CompanyCollection add, CompanyCollection remove) {

        final List<CompanyWithStringPlan> updatableCompanies = Lists.newArrayList();
        if (add != null) {
            final List<Company> companies = add.getPage();
            for (Company company : companies) {
                if(remove!=null && !remove.getPage().contains(company)){
                    updatableCompanies.add(prepareUpdatableCompany(company));
                }
            }
        }

        if (remove != null) {
            final List<Company> companies = remove.getPage();

            for (Company company : companies) {
                removeCompanyFromAddedList(updatableCompanies, company);
                updatableCompanies.add(prepareUpdatableCompany(company).setRemove(Boolean.TRUE));
            }
        }

        return updatableCompanies;
    }

    private static void removeCompanyFromAddedList(List<CompanyWithStringPlan> companies, Company company) {
        Iterator<CompanyWithStringPlan> companyIterator = companies.iterator();

        while (companyIterator.hasNext()) {
            CompanyWithStringPlan companyWithStringPlan = companyIterator.next();
            // TODO add equals and toHash methods to CompanyWithStringPlan and use it here
            if (companyWithStringPlan.getId() != null && company.getId() != null && companyWithStringPlan.getId().equals(company.getId())) {
                companyIterator.remove();
                break;
            }

            if (companyWithStringPlan.getCompanyID() != null && company.getCompanyID() != null && companyWithStringPlan.getCompanyID().equals(company.getCompanyID())) {
                companyIterator.remove();
                break;
            }
        }
    }

    private static CompanyWithStringPlan prepareUpdatableCompany(Company company) {
        final CompanyWithStringPlan updatableCompany = new CompanyWithStringPlan();
        updatableCompany.setId(company.getId());
        updatableCompany.setCompanyID(company.getCompanyID());
        updatableCompany.setName(company.getName());
        updatableCompany.setSessionCount(company.getSessionCount());
        updatableCompany.setMonthlySpend(company.getMonthlySpend());
        updatableCompany.setRemoteCreatedAt(company.getRemoteCreatedAt());
        if(company.getCustomAttributes() != null) {
            updatableCompany.getCustomAttributes().putAll(company.getCustomAttributes());
        }
        if (company.getPlan() != null) {
            updatableCompany.setPlan(company.getPlan().getName());
        }
        return updatableCompany;
    }
}

@@ -48,50 +48,147 @@
 mon.
-collect.Lists;%0A%0Aimport java.util.Iterator;
+base.Objects;%0Aimport com.google.common.base.Predicate;%0Aimport com.google.common.collect.Iterables;%0Aimport com.google.common.collect.Lists;%0A
 %0Aimp
@@ -694,61 +694,45 @@
   if
-(remove!=null && !remove.getPage().contains(company
+ (!isCompanyInList(company, remove
 ))
+ 
 %7B%0A  
@@ -938,25 +938,24 @@
 .getPage();%0A
-%0A
             
@@ -994,81 +994,8 @@
 ) %7B%0A
-                removeCompanyFromAddedList(updatableCompanies, company);%0A
     
@@ -1178,178 +1178,93 @@
 tic 
-void removeCompanyFromAddedList(List%3CCompanyWithStringPlan%3E companies, Company company) %7B%0A        Iterator%3CCompanyWithStringPlan%3E companyIterator = companies.iterator();%0A
+boolean isCompanyInList(final Company company, CompanyCollection companyCollection) %7B
 %0A   
@@ -1272,13 +1272,10 @@
     
-while
+if
  (co
@@ -1283,110 +1283,54 @@
 pany
-Iterator.hasNext()) %7B%0A            CompanyWithStringPlan companyWithStringPlan = companyIterator.next()
+Collection == null) %7B%0A            return false
 ;%0A  
@@ -1339,312 +1339,171 @@
     
+%7D%0A%0A
     
-// TODO add equals and toHash methods to CompanyWithStringPlan and use it here%0A            if (companyWithStringPlan.getId() != null && company.getId() != null && companyWithStringPlan.getId().equals(company.getId())) %7B%0A                companyIterator.remove();%0A                break;%0A     
+    return Iterables.any(companyCollection.getPage(), new Predicate%3CCompany%3E() %7B%0A            @Override%0A            public boolean apply(Company e) %7B%0A
        
-%7D%0A%0A
     
@@ -1511,148 +1511,54 @@
     
-   if (companyWithStringPlan.getCompanyID() != null && company.getCompanyID() != null && companyWithStringPlan.getCompanyID().equals(company
+return Objects.equal(company.getCompanyID(), e
 .get
@@ -1569,20 +1569,25 @@
 anyID())
-) %7B%0A
+%0A        
         
@@ -1598,55 +1598,52 @@
     
-companyIterator.remove();%0A                break
+%7C%7C Objects.equal(company.getId(), e.getId())
 ;%0A  
@@ -1659,24 +1659,26 @@
  %7D%0A        %7D
+);
 %0A    %7D%0A%0A    
@@ -2229,16 +2229,17 @@
       if
+ 
 (company

update CompanyUpdateBuilder