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

import java.io.ByteArrayInputStream;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.ComponentsOverrides;
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.RevertOverrideRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

@Service
@RequiredArgsConstructor
public class ComponentOverrideService {

    private final FileManagementStorageService fileManagementStorageService;
    private final ObjectMapper objectMapper;


    @SneakyThrows
    public void addOverrides(String dossierId, String fileId, ComponentsOverrides componentsOverrides) {

        if (!fileManagementStorageService.objectExists(dossierId, fileId, FileType.COMPONENTS)) {
            fileManagementStorageService.storeObject(dossierId, fileId, FileType.COMPONENTS, new ByteArrayInputStream(objectMapper.writeValueAsBytes(componentsOverrides)));
            return;
        }

        var existingComponentsBytes = fileManagementStorageService.getStoredObjectBytes(dossierId, fileId, FileType.COMPONENTS);
        var existingComponents = objectMapper.readValue(existingComponentsBytes, ComponentsOverrides.class);
        existingComponents.getComponentOverrides().putAll(componentsOverrides.getComponentOverrides());

        fileManagementStorageService.storeObject(dossierId, fileId, FileType.COMPONENTS, new ByteArrayInputStream(objectMapper.writeValueAsBytes(existingComponents)));
    }


    @SneakyThrows
    public ComponentsOverrides getOverrides(String dossierId, String fileId) {

        var exists = fileManagementStorageService.objectExists(dossierId, fileId, FileType.COMPONENTS);
        if (!exists) {
            return ComponentsOverrides.builder().build();
        }
        var existingComponentsBytes = fileManagementStorageService.getStoredObjectBytes(dossierId, fileId, FileType.COMPONENTS);
        return objectMapper.readValue(existingComponentsBytes, ComponentsOverrides.class);
    }


    @SneakyThrows
    public void revertOverrides(String dossierId, String fileId, RevertOverrideRequest revertOverrideRequest) {

        if (!fileManagementStorageService.objectExists(dossierId, fileId, FileType.COMPONENTS)) {
            return;
        }

        var existingComponentsBytes = fileManagementStorageService.getStoredObjectBytes(dossierId, fileId, FileType.COMPONENTS);
        var existingComponents = objectMapper.readValue(existingComponentsBytes, ComponentsOverrides.class);

        revertOverrideRequest.getComponents().forEach(c -> existingComponents.getComponentOverrides().remove(c));

        fileManagementStorageService.storeObject(dossierId, fileId, FileType.COMPONENTS, new ByteArrayInputStream(objectMapper.writeValueAsBytes(existingComponents)));
    }

}
