package com.iqser.red.service.persistence.management.v1.processor.migration.migrations;

import static com.iqser.red.service.persistence.management.v1.processor.utils.TypeIdUtils.toTypeId;

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

import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DictionaryPersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.DossierTemplatePersistenceService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.TypeRepository;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
@Service
public class V19AddGraphicDictionaryType extends Migration {

    @Autowired
    private DossierTemplatePersistenceService dossierTemplatePersistenceService;

    @Autowired
    private DictionaryPersistenceService dictionaryPersistenceService;

    @Autowired
    private TypeRepository typeRepository;

    private static final String NAME = "Migration to add experimental Graphic Type";
    private static final long VERSION = 19;


    public V19AddGraphicDictionaryType() {

        super(NAME, VERSION);
    }


    @Override
    protected void migrate() {

        dossierTemplatePersistenceService.getAllDossierTemplates()
                .forEach(dossierTemplateEntity -> {

                    String typeId = toTypeId("graphic", dossierTemplateEntity.getId(), null);

                    typeRepository.findById(typeId).ifPresentOrElse(type -> {
                        dictionaryPersistenceService.setExperimentalTrue(typeId);
                        log.info("Updated Graphic Type {} to experimental", typeId);
                    }, () -> {
                        dictionaryPersistenceService.addType("graphic",
                                                             dossierTemplateEntity.getId(),
                                                             "#ffbdfd",
                                                             "#8df06c",
                                                             "#c498fa",
                                                             998,
                                                             true,
                                                             true,
                                                             false,
                                                             "Empty dictionary used to configure graphic colors.",
                                                             false,
                                                             null,
                                                             false,
                                                             "Graphic",
                                                             null,
                                                             true,
                                                             false,
                                                             false,
                                                             false,
                                                             true);
                        log.info("Added Graphic Type {}", typeId);
                    });
                });
    }

}
