package com.iqser.red.service.peristence.v1.server.integration.tests.performance;

import java.io.FileInputStream;
import java.util.LinkedList;
import java.util.concurrent.CompletableFuture;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.FileTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.management.v1.processor.migration.StorageToMongoCopyService;
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.service.EntityLogMongoService;
import com.iqser.red.storage.commons.service.StorageService;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.SneakyThrows;
@Disabled
public class StorageToMongoDBPerformanceTest extends AbstractPersistenceServerServiceTest {

    public static final int NUMBER_OF_FILES = 10;
    @Autowired
    private StorageToMongoCopyService storageToMongoCopyService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private FileTesterAndProvider fileTesterAndProvider;
    @Autowired
    private DossierTesterAndProvider dossierTesterAndProvider;
    @Autowired
    private EntityLogMongoService entityLogMongoService;


    @BeforeEach
    @SneakyThrows
    public void setUp() {


    }


    @Test
    @SneakyThrows
    public void testSavePerformance() {

        var dossier = dossierTesterAndProvider.provideTestDossier();
        LinkedList<CompletableFuture<Void>> futures = new LinkedList<>();
        for (int i = 0; i < NUMBER_OF_FILES; i++) {
            int number = i;
            futures.add(CompletableFuture.supplyAsync(() -> {
                prepStorageForFile(dossier, number);
                return null;
            }));
        }

        futures.forEach(CompletableFuture::join);
        TenantContext.setTenantId(TENANT_1);
        long start = System.currentTimeMillis();
        storageToMongoCopyService.copyEntityLogs();
        long end = System.currentTimeMillis();
        System.out.println("Saving of" + NUMBER_OF_FILES + " files took " + (end - start) + "ms");
    }


    @SneakyThrows
    private void prepStorageForFile(Dossier dossier, int i) {

        TenantContext.setTenantId(TENANT_1);

        var file = fileTesterAndProvider.testAndProvideFile(dossier, "file" + i);
        try (var in = new FileInputStream("/home/kschuettler/Dokumente/TestFiles/entitylogs/largeEntitylog.json")) {
            storageService.storeObject(TENANT_1, StorageIdUtils.getStorageId(dossier.getId(), file.getId(), FileType.ENTITY_LOG), in);
        }
        entityLogMongoService.deleteEntityLog(dossier.getId(), file.getId());
    }

}
