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

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.stereotype.Service;

import com.iqser.red.service.redaction.v1.server.model.document.nodes.Document;
import com.iqser.red.service.redaction.v1.server.model.document.nodes.NodeType;
import com.iqser.red.service.redaction.v1.server.model.document.nodes.SemanticNode;
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedSectionText;
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedText;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SimplifiedSectionTextService {

    public SimplifiedText toSimplifiedText(Document document) {

        List<SimplifiedSectionText> simplifiedMainSectionsList = document.getAllSections()
                .stream()
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedHeadersList = document.streamAllSubNodesOfType(NodeType.HEADER)
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedFootersList = document.streamAllSubNodesOfType(NodeType.FOOTER)
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedText = Stream.of(simplifiedMainSectionsList, simplifiedHeadersList, simplifiedFootersList)
                .flatMap(List::stream)
                .collect(Collectors.toList());
        return SimplifiedText.builder()
                .numberOfPages(document.getNumberOfPages())
                .sectionTexts(simplifiedText)
                .mainSectionNumbers(document.getAllSections()
                                            .stream()
                                            .map(this::getSectionNumber)
                                            .toList())
                .headerSectionNumbers(document.streamAllSubNodesOfType(NodeType.HEADER)
                                              .map(this::getSectionNumber)
                                              .toList())
                .footerSectionNumbers(document.streamAllSubNodesOfType(NodeType.FOOTER)
                                              .map(this::getSectionNumber)
                                              .toList())
                .build();
    }


    private SimplifiedSectionText toSimplifiedSectionText(SemanticNode section) {

        return SimplifiedSectionText.builder()
                .sectionNumber(section.getTreeId()
                                       .stream()
                                       .map(String::valueOf)
                                       .collect(Collectors.joining(".")))
                .text(section.getTextBlock().getSearchText())
                .build();
    }


    private String getSectionNumber(SemanticNode semanticNode) {

        return semanticNode.getTreeId()
                .stream()
                .map(String::valueOf)
                .collect(Collectors.joining("."));
    }

}
