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

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTemplateTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTesterAndProvider;
import com.iqser.red.service.peristence.v1.server.integration.service.TypeProvider;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.UpdateTypeValue;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.configuration.Colors;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.type.DictionaryEntryType;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.type.Type;

public class TypeTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private TypeProvider typeProvider;

    @Autowired
    private DossierTemplateTesterAndProvider dossierTemplateTesterAndProvider;

    @Autowired
    private DossierTesterAndProvider dossierTesterAndProvider;

    @Autowired
    private DictionaryClient dictionaryClient;


    @Test
    public void testType() {

        var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();

        var type = typeProvider.testAndProvideType(dossierTemplate);

        dictionaryClient.addEntry(type.getType(), type.getDossierTemplateId(), Lists.newArrayList("aaa", "bbb", "ccc"), true, null, DictionaryEntryType.ENTRY);

        var loadedType = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null);

        assertThat(loadedType.getEntries().size()).isEqualTo(3);

        var dict = loadedType.getEntries();

        assertThat(dict.size()).isEqualTo(3);

        dictionaryClient.deleteEntries(type.getType(), type.getDossierTemplateId(), Lists.newArrayList("aaa", "bbb", "ccc"), null, DictionaryEntryType.ENTRY);

        loadedType = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null);

        dict = loadedType.getEntries();

        assertThat(dict.size()).isEqualTo(0);

        var request = new UpdateTypeValue();
        BeanUtils.copyProperties(type, request);
        request.setRank(99);
        dictionaryClient.updateType(type.getType(), type.getDossierTemplateId(), request);

        loadedType = dictionaryClient.getDictionaryForType(type.getType(), type.getDossierTemplateId(), null);
        assertThat(loadedType.getRank()).isEqualTo(99);

        dictionaryClient.deleteType(type.getType(), type.getDossierTemplateId());

        var typesForTemplate = dictionaryClient.getAllTypes(loadedType.getDossierTemplateId(), null, false).getTypes();
        assertThat(typesForTemplate).isEmpty();

        var dossier = dossierTesterAndProvider.provideTestDossier(dossierTemplate);
        var dossierType = typeProvider.testAndProvideType(dossierTemplate, dossier, "dossier-dict");

        var typesForDossier = dictionaryClient.getAllTypes(dossierType.getDossierTemplateId(), dossier.getId(), false).getTypes();
        assertThat(typesForDossier).isNotEmpty();

    }


    @Test
    public void testColors() {

        var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();

        var colors = new Colors();

        colors.setRequestAddColor("#dddddd");
        colors.setRequestRemoveColor("#dddddd");
        colors.setDictionaryRequestColor("#dddddd");
        colors.setPreviewColor("#dddddd");
        colors.setAnalysisColor("#dddddd");
        colors.setUpdatedColor("#dddddd");
        colors.setRecommendationColor("#dddddd");
        colors.setHintColor("#dddddd");
        colors.setRedactionColor("#dddddd");
        colors.setIgnoredHintColor("#dddddd");
        colors.setSkippedColor("#dddddd");
        colors.setAppliedRedactionColor("#dddddd");

        colors.setDossierTemplateId(dossierTemplate.getId());

        dictionaryClient.setColors(dossierTemplate.getId(), colors);

        var savedColors = dictionaryClient.getColors(dossierTemplate.getId());
        assertThat(savedColors).isEqualTo(colors);
        assertThat(savedColors.getRequestAddColor()).isEqualTo(colors.getRequestAddColor());
        assertThat(savedColors.getRequestRemoveColor()).isEqualTo(colors.getRequestRemoveColor());
        assertThat(savedColors.getDictionaryRequestColor()).isEqualTo(colors.getDictionaryRequestColor());
        assertThat(savedColors.getPreviewColor()).isEqualTo(colors.getPreviewColor());
        assertThat(savedColors.getAnalysisColor()).isEqualTo(colors.getAnalysisColor());
        assertThat(savedColors.getUpdatedColor()).isEqualTo(colors.getUpdatedColor());
        assertThat(savedColors.getRecommendationColor()).isEqualTo(colors.getRecommendationColor());
        assertThat(savedColors.getHintColor()).isEqualTo(colors.getHintColor());
        assertThat(savedColors.getRedactionColor()).isEqualTo(colors.getRedactionColor());
        assertThat(savedColors.getIgnoredHintColor()).isEqualTo(colors.getIgnoredHintColor());
        assertThat(savedColors.getSkippedColor()).isEqualTo(colors.getSkippedColor());
        assertThat(savedColors.getAppliedRedactionColor()).isEqualTo(colors.getAppliedRedactionColor());
    }

}
