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

import java.awt.geom.Rectangle2D;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.LayoutEngineProto.LayoutEngine;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.DocumentTree;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.entity.TextEntity;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlock;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public abstract class AbstractSemanticNode implements GenericSemanticNode {

    @Builder.Default
    Set<LayoutEngine> engines = new HashSet<>(Set.of(LayoutEngine.ALGORITHM));
    List<Integer> treeId;

    TextBlock textBlock;
    @EqualsAndHashCode.Exclude
    DocumentTree documentTree;

    @Builder.Default
    @EqualsAndHashCode.Exclude
    Set<TextEntity> entities = new HashSet<>();

    @EqualsAndHashCode.Exclude
    Map<Page, Rectangle2D> bBoxCache;


    @Override
    public TextBlock getTextBlock() {

        if (textBlock == null) {
            textBlock = GenericSemanticNode.super.getTextBlock();
        }
        return textBlock;
    }


    @Override
    public String toString() {

        return treeId.toString() + ": " + getType() + ": " + this.getTextBlock().buildSummary();
    }


    @Override
    public Map<Page, Rectangle2D> getBBox() {

        if (bBoxCache == null) {
            bBoxCache = GenericSemanticNode.super.getBBox();
        }
        return bBoxCache;
    }

}
