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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iqser.red.service.peristence.v1.server.integration.client.DictionaryClient;
import com.iqser.red.service.persistence.service.v1.api.shared.model.CreateTypeValue;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierTemplateModel;
import com.iqser.red.service.persistence.service.v1.api.shared.model.TypeValue;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;

@Service
public class TypeProvider {

    @Autowired
    private DictionaryClient dictionaryClient;


    public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate) {

        return testAndProvideType(dossierTemplate, null, "test", false);
    }


    public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate, Dossier dossier, String typeName) {

        return testAndProvideType(dossierTemplate, dossier, typeName, false);
    }


    public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate, Dossier dossier, String typeName, boolean dossierDictionaryOnly) {

        return testAndProvideType(dossierTemplate, dossier, typeName, dossierDictionaryOnly, 100);
    }


    public TypeValue testAndProvideType(DossierTemplateModel dossierTemplate, Dossier dossier, String typeName, boolean dossierDictionaryOnly, int rank) {

        var type = new CreateTypeValue();
        type.setType(typeName);
        type.setDescription("test");
        type.setAddToDictionaryAction(true);
        type.setHexColor("#dddddd");
        type.setRecommendationHexColor("#aaaaaa");
        type.setSkippedHexColor("#aaaaaa");
        type.setHint(false);
        type.setRank(rank);
        type.setRecommendation(false);
        type.setCaseInsensitive(true);
        type.setDossierTemplateId(dossierTemplate.getId());
        type.setHasDictionary(true);
        type.setDossierDictionaryOnly(dossierDictionaryOnly);

        dictionaryClient.addType(type);
        var allTypes = dictionaryClient.getAllTypes(dossierTemplate.getDossierTemplateId(), dossier != null ? dossier.getId() : null, false);

        var foundType = allTypes.getTypes().stream().filter(t -> t.getType().equalsIgnoreCase(typeName)).findAny();
        assertThat(foundType).isPresent();

        return foundType.get();
    }

}
