package com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock;

import static java.lang.String.format;

import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import com.knecon.fforesight.service.layoutparser.processor.model.graph.TextRange;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Page;

import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ConcatenatedTextBlock implements TextBlock {

    List<AtomicTextBlock> atomicTextBlocks;
    String searchText;
    TextRange textRange;


    public static ConcatenatedTextBlock empty() {

        return new ConcatenatedTextBlock(Collections.emptyList());
    }


    public ConcatenatedTextBlock(List<AtomicTextBlock> atomicTextBlocks) {

        this.atomicTextBlocks = new LinkedList<>();
        if (atomicTextBlocks.isEmpty()) {
            textRange = new TextRange(-1, -1);
            return;
        }
        var firstTextBlock = atomicTextBlocks.get(0);
        this.atomicTextBlocks.add(firstTextBlock);
        textRange = new TextRange(firstTextBlock.getTextRange().start(), firstTextBlock.getTextRange().end());

        atomicTextBlocks.subList(1, atomicTextBlocks.size()).forEach(this::concat);
    }


    public ConcatenatedTextBlock concat(TextBlock textBlock) {

        int start = textBlock.getTextRange().start();
        int end = textBlock.getTextRange().end();
        if (this.atomicTextBlocks.isEmpty()) {
            textRange.setStart(start);
            textRange.setEnd(end);
        } else if (textRange.end() != start) {
            throw new UnsupportedOperationException(format("Can only concat consecutive TextBlocks, trying to concat %s and %s", textRange, textBlock.getTextRange()));
        }
        this.atomicTextBlocks.addAll(textBlock.getAtomicTextBlocks());
        textRange.setEnd(end);
        this.searchText = null;
        return this;
    }


    private AtomicTextBlock getAtomicTextBlockByStringIndex(int stringIdx) {

        return atomicTextBlocks.stream().filter(textBlock -> textBlock.getTextRange().contains(stringIdx)).findAny().orElseThrow(IndexOutOfBoundsException::new);
    }


    private List<AtomicTextBlock> getAllAtomicTextBlocksPartiallyInStringBoundary(TextRange textRange) {

        return atomicTextBlocks.stream().filter(tb -> tb.getTextRange().intersects(textRange)).toList();
    }


    @Override
    public String getSearchText() {

        if (searchText == null) {
            StringBuilder sb = new StringBuilder();
            getAtomicTextBlocks().forEach(atb -> sb.append(atb.getSearchText()));
            searchText = sb.toString();
        }
        return searchText;
    }


    @Override
    public int numberOfLines() {

        return atomicTextBlocks.stream().map(AtomicTextBlock::getLineBreaks).mapToInt(List::size).sum();
    }


    @Override
    public int getNextLinebreak(int fromIndex) {

        return getAtomicTextBlockByStringIndex(fromIndex).getNextLinebreak(fromIndex);
    }


    @Override
    public int getPreviousLinebreak(int fromIndex) {

        return getAtomicTextBlockByStringIndex(fromIndex).getPreviousLinebreak(fromIndex);
    }


    @Override
    public List<Integer> getLineBreaks() {

        return getAtomicTextBlocks().stream().flatMap(atomicTextBlock -> atomicTextBlock.getLineBreaks().stream()).toList();
    }


    @Override
    public Rectangle2D getPosition(int stringIdx) {

        return getAtomicTextBlockByStringIndex(stringIdx).getPosition(stringIdx);
    }


    @Override
    public List<Rectangle2D> getPositions(TextRange stringTextRange) {

        List<AtomicTextBlock> textBlocks = getAllAtomicTextBlocksPartiallyInStringBoundary(stringTextRange);

        if (textBlocks.size() == 1) {
            return textBlocks.get(0).getPositions(stringTextRange);
        }

        AtomicTextBlock firstTextBlock = textBlocks.get(0);
        List<Rectangle2D> positions = new LinkedList<>(firstTextBlock.getPositions(new TextRange(stringTextRange.start(), firstTextBlock.getTextRange().end())));

        for (AtomicTextBlock textBlock : textBlocks.subList(1, textBlocks.size() - 1)) {
            positions.addAll(textBlock.getPositions());
        }

        var lastTextBlock = textBlocks.get(textBlocks.size() - 1);
        positions.addAll(lastTextBlock.getPositions(new TextRange(lastTextBlock.getTextRange().start(), stringTextRange.end())));

        return positions;
    }


    @Override
    public Map<Page, List<Rectangle2D>> getPositionsPerPage(TextRange stringTextRange) {

        List<AtomicTextBlock> textBlocks = getAllAtomicTextBlocksPartiallyInStringBoundary(stringTextRange);

        if (textBlocks.size() == 1) {
            return textBlocks.get(0).getPositionsPerPage(stringTextRange);
        }

        AtomicTextBlock firstTextBlock = textBlocks.get(0);
        Map<Page, List<Rectangle2D>> rectanglesPerLinePerPage = firstTextBlock.getPositionsPerPage(new TextRange(stringTextRange.start(), firstTextBlock.getTextRange().end()));

        for (AtomicTextBlock textBlock : textBlocks.subList(1, textBlocks.size() - 1)) {
            rectanglesPerLinePerPage = mergeEntityPositionsWithSamePageNode(rectanglesPerLinePerPage, textBlock.getPositionsPerPage(textBlock.getTextRange()));
        }

        AtomicTextBlock lastTextBlock = textBlocks.get(textBlocks.size() - 1);
        rectanglesPerLinePerPage = mergeEntityPositionsWithSamePageNode(rectanglesPerLinePerPage,
                lastTextBlock.getPositionsPerPage(new TextRange(lastTextBlock.getTextRange().start(), stringTextRange.end())));

        return rectanglesPerLinePerPage;
    }


    private Map<Page, List<Rectangle2D>> mergeEntityPositionsWithSamePageNode(Map<Page, List<Rectangle2D>> map1, Map<Page, List<Rectangle2D>> map2) {

        Map<Page, List<Rectangle2D>> mergedMap = new HashMap<>(map1);
        map2.forEach((pageNode, rectangles) -> mergedMap.merge(pageNode, rectangles, (l1, l2) -> Stream.concat(l1.stream(), l2.stream()).toList()));
        return mergedMap;
    }


    @Override
    public String toString() {

        return getSearchText();
    }


    @Override
    public List<TextRange> getBoldTextBoundaries() {

        return getAtomicTextBlocks().stream().map(AtomicTextBlock::getBoldTextBoundaries).flatMap(Collection::stream).toList();
    }


    @Override
    public List<TextRange> getItalicTextBoundaries() {

        return getAtomicTextBlocks().stream().map(AtomicTextBlock::getItalicTextBoundaries).flatMap(Collection::stream).toList();
    }


    @Override
    public String getOrientation() {

        if (atomicTextBlocks.isEmpty()) {
            return "";
        }
        return atomicTextBlocks.get(0).getOrientation();
    }


    @Override
    public int getTextDirection() {

        if (atomicTextBlocks.isEmpty()) {
            return 0;
        }
        return atomicTextBlocks.get(0).getTextDirection();
    }

}
