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

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.entity.RedactionEntity;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlock;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlockCollector;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldDefaults;

@Getter
@Setter
@Builder
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Page {

    Integer number;
    Integer height;
    Integer width;
    Integer rotation;

    @EqualsAndHashCode.Exclude
    List<SemanticNode> mainBody;
    @EqualsAndHashCode.Exclude
    Header header;
    @EqualsAndHashCode.Exclude
    Footer footer;

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

    @Builder.Default
    @EqualsAndHashCode.Exclude
    Set<Image> images = new HashSet<>();


    public static Page fromClassificationPage(ClassificationPage classificationPage) {

        return Page.builder()
                .height((int) classificationPage.getPageHeight())
                .width((int) classificationPage.getPageWidth())
                .number(classificationPage.getPageNumber())
                .rotation(classificationPage.getRotation())
                .mainBody(new LinkedList<>())
                .build();
    }


    public TextBlock getMainBodyTextBlock() {

        return mainBody.stream().filter(SemanticNode::isLeaf).map(SemanticNode::getLeafTextBlock).collect(new TextBlockCollector());
    }


    @Override
    public String toString() {

        return String.valueOf(number);
    }


    @Override
    public int hashCode() {

        return number;
    }


    @Override
    public boolean equals(Object o) {

        return o instanceof Page && o.hashCode() == this.hashCode();
    }

}
