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

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.FilteredRedactionLogRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.RedactionLog;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.RedactionLogEntry;
import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.section.SectionGrid;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class RedactionLogService {

    private final FileManagementStorageService fileManagementStorageService;
    private final FileStatusService fileStatusService;


    public RedactionLog getRedactionLog(String dossierId, String fileId) {

        return getRedactionLog(dossierId, fileId, Collections.emptyList());
    }


    public RedactionLog getRedactionLog(String dossierId, String fileId, List<String> excludedTypes) {

        var fileStatus = fileStatusService.getStatus(fileId);

        RedactionLog redactionLog;

        redactionLog = fileManagementStorageService.getRedactionLog(dossierId, fileId);

        if (fileStatus.isExcluded()) {
            redactionLog.setRedactionLogEntry(new ArrayList<>());
        }

        if (excludedTypes != null) {
            redactionLog.getRedactionLogEntry().removeIf(nextEntry -> excludedTypes.contains(nextEntry.getType()));
        }

        return redactionLog;
    }


    public SectionGrid getSectionGrid(String dossierId, String fileId) {

        return fileManagementStorageService.getSectionGrid(dossierId, fileId);
    }


    public RedactionLog getFilteredRedactionLog(String dossierId, String fileId, FilteredRedactionLogRequest filteredRedactionLogRequest) {

        if (filteredRedactionLogRequest.getSpecifiedDate() == null) {
            filteredRedactionLogRequest.setSpecifiedDate(OffsetDateTime.MIN);
        }

        var redactionLog = getRedactionLog(dossierId, fileId, filteredRedactionLogRequest.getExcludedTypes());
        var redactionLogEntries = redactionLog.getRedactionLogEntry();

        Iterator<RedactionLogEntry> it = redactionLogEntries.iterator();
        while (it.hasNext()) {
            var redactionLogEntry = it.next();
            boolean isAfterSpecifiedDate = false;
            for (var change : redactionLogEntry.getChanges()) {
                if (change.getDateTime() != null && change.getDateTime().isAfter(filteredRedactionLogRequest.getSpecifiedDate())) {
                    isAfterSpecifiedDate = true;
                }
            }
            for (var manualChange : redactionLogEntry.getManualChanges()) {
                if (manualChange.getProcessedDate() != null && manualChange.getProcessedDate()
                        .isAfter(filteredRedactionLogRequest.getSpecifiedDate()) || manualChange.getRequestedDate() != null && manualChange.getRequestedDate()
                        .isAfter(filteredRedactionLogRequest.getSpecifiedDate())) {
                    isAfterSpecifiedDate = true;
                }
            }
            var comments = redactionLogEntry.getComments();
            if (comments != null) {
                for (var comment : comments) {
                    if (comment.getSoftDeletedTime() != null && comment.getSoftDeletedTime()
                            .isAfter(filteredRedactionLogRequest.getSpecifiedDate()) || comment.getDate() != null && comment.getDate()
                            .isAfter(filteredRedactionLogRequest.getSpecifiedDate())) {
                        isAfterSpecifiedDate = true;
                    }
                }
            }

            if (!isAfterSpecifiedDate) {
                it.remove();
            }

        }

        return redactionLog;
    }

}
