package com.knecon.fforesight.service.layoutparser.processor.services.classification;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationDocument;
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
import com.knecon.fforesight.service.layoutparser.processor.model.PageBlockType;
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
import com.knecon.fforesight.service.layoutparser.processor.services.BodyTextFrameService;
import com.knecon.fforesight.service.layoutparser.processor.services.blockification.DocuMineBlockificationService;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class ClassificationService {

    DocuMineBlockificationService docuMineBlockificationService;
    BodyTextFrameService bodyTextFrameService;
    TableOfContentsClassificationService tableOfContentsClassificationService;
    RedactManagerClassificationService redactManagerClassificationService;
    ClarifyndClassificationService clarifyndClassificationService;
    DocuMineClassificationService docuMineClassificationService;
    HeaderFooterClassificationService headerFooterClassificationService;


    public void classify(ClassificationDocument document, LayoutParsingType layoutParsingType, Map<String, String> identifier) {

        log.info("Calculating BodyTextFrame for {}", identifier);
        bodyTextFrameService.setBodyTextFrames(document, layoutParsingType);
        for (ClassificationPage page : document.getPages()) {
            document.getLayoutDebugLayer().addCleanRulingVisualization(page.getCleanRulings(), page.getPageNumber());
        }
        log.info("Classify TextBlocks for {}", identifier);

        headerFooterClassificationService.classifyHeadersAndFooters(document);

        tableOfContentsClassificationService.classifyTableOfContents(document);

        switch (layoutParsingType) {
            case REDACT_MANAGER, REDACT_MANAGER_PARAGRAPH_DEBUG, REDACT_MANAGER_OLD, CLARIFYND_PARAGRAPH_DEBUG, REDACT_MANAGER_WITHOUT_DUPLICATE_PARAGRAPH ->
                    redactManagerClassificationService.classifyDocument(document);
            case DOCUMINE_OLD, DOCUMINE -> docuMineClassificationService.classifyDocument(document);
            case CLARIFYND -> clarifyndClassificationService.classifyDocument(document);
        }

        if (layoutParsingType.equals(LayoutParsingType.DOCUMINE_OLD)) {
            for (ClassificationPage page : document.getPages()) {
                docuMineBlockificationService.mergeblocks(page, page.getCleanRulings().withoutTextRulings(), 0, 10);
            }
        }
    }

}
