/*
 * Copyright (c) 2004-2019, 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.datavalue.internal;

import org.hisp.dhis.android.core.arch.api.executors.internal.APICallExecutor;
import org.hisp.dhis.android.core.arch.call.D2Progress;
import org.hisp.dhis.android.core.arch.call.internal.D2ProgressManager;
import org.hisp.dhis.android.core.datavalue.DataValue;
import org.hisp.dhis.android.core.imports.internal.DataValueImportSummary;
import org.hisp.dhis.android.core.systeminfo.SystemInfo;
import org.hisp.dhis.android.core.systeminfo.internal.SystemInfoModuleDownloader;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import dagger.Reusable;
import io.reactivex.Observable;

@Reusable
public final class DataValuePostCall {

    private final DataValueService dataValueService;
    private final DataValueImportHandler dataValueImportHandler;
    private final APICallExecutor apiCallExecutor;
    private final SystemInfoModuleDownloader systemInfoDownloader;

    @Inject
    DataValuePostCall(@NonNull DataValueService dataValueService,
                      @NonNull DataValueImportHandler dataValueImportHandler,
                      @NonNull APICallExecutor apiCallExecutor,
                      @NonNull SystemInfoModuleDownloader systemInfoDownloader) {

        this.dataValueService = dataValueService;
        this.dataValueImportHandler = dataValueImportHandler;
        this.apiCallExecutor = apiCallExecutor;
        this.systemInfoDownloader = systemInfoDownloader;
    }

    public Observable<D2Progress> uploadDataValues(List<DataValue> dataValues) {
        return Observable.defer(() -> {
            if (dataValues.isEmpty()) {
                return Observable.empty();
            } else {
                D2ProgressManager progressManager = new D2ProgressManager(2);

                return systemInfoDownloader.downloadMetadata().andThen(Observable.create(emitter -> {
                    emitter.onNext(progressManager.increaseProgress(SystemInfo.class, false));

                    DataValueSet dataValueSet = new DataValueSet(dataValues);

                    DataValueImportSummary dataValueImportSummary = apiCallExecutor.executeObjectCall(
                            dataValueService.postDataValues(dataValueSet));

                    dataValueImportHandler.handleImportSummary(dataValueSet, dataValueImportSummary);

                    emitter.onNext(progressManager.increaseProgress(DataValue.class, true));
                    emitter.onComplete();
                }));
            }
        });
    }
}

@@ -1607,16 +1607,53 @@
 ernal;%0A%0A
+import androidx.annotation.NonNull;%0A%0A
 import o
@@ -1723,16 +1723,16 @@
 ecutor;%0A
-
 import o
@@ -1851,16 +1851,64 @@
 anager;%0A
+import org.hisp.dhis.android.core.common.State;%0A
 import o
@@ -2169,16 +2169,45 @@
 oader;%0A%0A
+import java.util.Collection;%0A
 import j
@@ -2255,44 +2255,8 @@
 t;%0A%0A
-import androidx.annotation.NonNull;%0A
 impo
@@ -2593,16 +2593,65 @@
 nloader;
+%0A    private final DataValueStore dataValueStore;
 %0A%0A    @I
@@ -2942,16 +2942,78 @@
 wnloader
+,%0A                      @NonNull DataValueStore dataValueStore
 ) %7B%0A%0A   
@@ -3223,24 +3223,70 @@
 Downloader;%0A
+        this.dataValueStore = dataValueStore;%0A
     %7D%0A%0A    p
@@ -3778,24 +3778,84 @@
 , false));%0A%0A
+                    markPartitionsAsUploading(dataValues);%0A%0A
             
@@ -4374,16 +4374,16 @@
       %7D%0A
-
         
@@ -4391,11 +4391,221 @@
 );%0A    %7D
+%0A%0A    private void markPartitionsAsUploading(Collection%3CDataValue%3E dataValues) %7B%0A        for (DataValue dataValue : dataValues) %7B%0A            dataValueStore.setState(dataValue, State.UPLOADING);%0A        %7D%0A    %7D
 %0A%7D%0A

Mark dataValue partitions as uploading