/*
 * Copyright (c) 2017, University of Oslo
 *
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.organisationunit;

import org.hisp.dhis.android.core.calls.Call;
import org.hisp.dhis.android.core.calls.EndpointCall;
import org.hisp.dhis.android.core.calls.fetchers.CallFetcher;
import org.hisp.dhis.android.core.calls.fetchers.UidsNoResourceCallFetcher;
import org.hisp.dhis.android.core.calls.processors.CallProcessor;
import org.hisp.dhis.android.core.calls.processors.TransactionalNoResourceCallProcessor;
import org.hisp.dhis.android.core.common.Payload;
import org.hisp.dhis.android.core.common.UidsQuery;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;

import java.util.List;
import java.util.Set;

import retrofit2.Retrofit;

public final class SearchOrganisationUnitCall {

    private SearchOrganisationUnitCall() {}

    public interface Factory {
        Call<List<OrganisationUnit>> create(DatabaseAdapter databaseAdapter, Retrofit retrofit,
                                            Set<String> uids, String userId);
    }

    public static final Factory FACTORY = new Factory() {
        @Override
        public Call<List<OrganisationUnit>> create(DatabaseAdapter databaseAdapter, Retrofit retrofit,
                                                   Set<String> uids, String userId) {
            return new EndpointCall<>(fetcher(retrofit, uids), processor(databaseAdapter, userId));
        }

        private static final int MAX_UID_LIST_SIZE = 120;

        CallFetcher<OrganisationUnit> fetcher(Retrofit retrofit, Set<String> uids) {
            final OrganisationUnitService service = retrofit.create(OrganisationUnitService.class);

            return new UidsNoResourceCallFetcher<OrganisationUnit>(uids, MAX_UID_LIST_SIZE) {

                @Override
                protected retrofit2.Call<Payload<OrganisationUnit>> getCall(UidsQuery query) {
                    return service.getSearchOrganisationUnits(OrganisationUnit.allFields,
                            OrganisationUnit.uid.in(query.uids()), Boolean.FALSE);
                }
            };
        }

        CallProcessor<OrganisationUnit> processor(DatabaseAdapter databaseAdapter, String userId) {
            return new TransactionalNoResourceCallProcessor<>(
                    databaseAdapter,
                    SearchOrganisationUnitHandler.create(databaseAdapter, userId),
                    new OrganisationUnitModelBuilder()
            );
        }
    };
}
@@ -1985,32 +1985,90 @@
 eCallProcessor;%0A
+import org.hisp.dhis.android.core.common.GenericCallData;%0A
 import org.hisp.
@@ -2218,16 +2218,87 @@
 dapter;%0A
+import org.hisp.dhis.android.core.user.User;%0A%0Aimport java.util.HashSet;
 %0Aimport 
@@ -2537,135 +2537,50 @@
 ate(
-DatabaseAdapter databaseAdapter, Retrofit retrofit,%0A                                            Set%3CString%3E uids, String
+GenericCallData genericCallData, User
  user
-Id
 );%0A 
@@ -2716,76 +2716,293 @@
 ate(
-DatabaseAdapter databaseAdapter, Retrofit retrofit,%0A                
+GenericCallData genericCallData, User user) %7B%0A%0A            Set%3CString%3E organisationUnitUids = new HashSet%3C%3E();%0A%0A            for (OrganisationUnit searchOrganisationUnitUid : user.teiSearchOrganisationUnits())%7B%0A                organisationUnitUids.add(searchOrganisationUnitUid.uid());%0A
     
@@ -3001,32 +3001,35 @@
 ));%0A            
+%7D%0A%0A
                 
@@ -3028,127 +3028,317 @@
     
-           Set%3CString%3E uids, String userId) %7B%0A            return new EndpointCall%3C%3E(fetcher(retrofit, uids), processor(
+for (OrganisationUnit captureOrganisationUnit : user.organisationUnits())%7B%0A                organisationUnitUids.remove(captureOrganisationUnit.uid());%0A            %7D%0A%0A            return new EndpointCall%3C%3E(fetcher(genericCallData.retrofit(), organisationUnitUids),%0A                    processor(genericCallData.
 data
@@ -3348,24 +3348,30 @@
 eAdapter
+()
 , user
-Id
+.uid()
 ));%0A    

Add SearchOrganisationUnit