/*
 * 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 androidx.annotation.NonNull;

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.common.State;
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.Collection;
import java.util.List;

import javax.inject.Inject;

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;
    private final DataValueStore dataValueStore;

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

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

    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));

                    markPartitionsAsUploading(dataValues);

                    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();
                }));
            }
        });
    }

    private void markPartitionsAsUploading(Collection<DataValue> dataValues) {
        for (DataValue dataValue : dataValues) {
            dataValueStore.setState(dataValue, State.UPLOADING);
        }
    }
}

@@ -3818,25 +3818,16 @@
 itionsAs
-Uploading
 (dataVal
@@ -3821,36 +3821,83 @@
 onsAs(dataValues
+, State.UPLOADING
 );%0A%0A
+                    try %7B%0A    
                 
@@ -3958,17 +3958,20 @@
 alues);%0A
-%0A
+    
         
@@ -4085,32 +4085,36 @@
                 
+    
 dataValueService
@@ -4158,32 +4158,36 @@
                 
+   
+ 
 dataValueImportH
@@ -4251,16 +4251,182 @@
 ummary);
+%0A                    %7D catch (Exception e) %7B%0A                        markPartitionsAs(dataValues, State.ERROR);%0A                        throw e;%0A                    %7D
 %0A%0A      
@@ -4645,17 +4645,8 @@
 nsAs
-Uploading
 (Col
@@ -4666,32 +4666,45 @@
 alue%3E dataValues
+, State state
 ) %7B%0A        for 
@@ -4787,31 +4787,21 @@
 aValue, 
-S
+s
 tate
-.UPLOADING
 );%0A     

Mark data values as error if sync fails