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

import android.support.annotation.NonNull;

import org.hisp.dhis.android.core.D2InternalModules;
import org.hisp.dhis.android.core.maintenance.D2Error;
import org.hisp.dhis.android.core.common.D2CallExecutor;
import org.hisp.dhis.android.core.common.SyncCall;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceListDownloadAndPersistCall;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceStore;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceStoreImpl;

import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;

public final class TrackedEntityInstanceSyncDownCall extends SyncCall<List<TrackedEntityInstance>> {

    private final DatabaseAdapter databaseAdapter;
    private final Retrofit retrofit;
    private final D2InternalModules internalModules;
    private final TrackedEntityInstanceStore trackedEntityInstanceStore;

    private TrackedEntityInstanceSyncDownCall(@NonNull DatabaseAdapter databaseAdapter,
                                              @NonNull Retrofit retrofit,
                                              @NonNull D2InternalModules internalModules,
                                              @NonNull TrackedEntityInstanceStore trackedEntityInstanceStore) {
        this.databaseAdapter = databaseAdapter;
        this.retrofit = retrofit;
        this.internalModules = internalModules;
        this.trackedEntityInstanceStore = trackedEntityInstanceStore;
    }

    @Override
    public List<TrackedEntityInstance> call() throws D2Error {
        setExecuted();
        Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.querySynced();
        Call<List<TrackedEntityInstance>> call = TrackedEntityInstanceListDownloadAndPersistCall
                .create(databaseAdapter, retrofit, internalModules, trackedEntityInstances.keySet());
        return new D2CallExecutor(databaseAdapter).executeD2Call(call);
    }

    public static TrackedEntityInstanceSyncDownCall create(DatabaseAdapter databaseAdapter, Retrofit retrofit,
                                                           D2InternalModules internalModules) {
        return new TrackedEntityInstanceSyncDownCall(
                databaseAdapter,
                retrofit,
                internalModules,
                new TrackedEntityInstanceStoreImpl(databaseAdapter)
        );
    }
}

@@ -137,63 +137,8 @@
 es;%0A
-import org.hisp.dhis.android.core.maintenance.D2Error;%0A
 impo
@@ -306,16 +306,71 @@
 dapter;%0A
+import org.hisp.dhis.android.core.maintenance.D2Error;%0A
 import o
@@ -712,30 +712,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -1738,21 +1738,22 @@
     
-Map
+List
 %3CString
-, T
+%3E t
 rack
@@ -1768,17 +1768,23 @@
 Instance
-%3E
+sUids =
  tracked
@@ -1797,21 +1797,34 @@
 Instance
-s = t
+Store.querySyncedT
 rackedEn
@@ -1835,33 +1835,20 @@
 Instance
-Store.querySynced
+Uids
 ();%0A    
@@ -2034,17 +2034,12 @@
 nces
-.keySet()
+Uids
 );%0A 
@@ -2485,20 +2485,16 @@
         
-new 
 TrackedE
@@ -2515,16 +2515,23 @@
 toreImpl
+.create
 (databas
@@ -2558,9 +2558,8 @@
 %0A    %7D%0A%7D
-%0A

Fix TEISyncDownCall