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

import java.awt.geom.Rectangle2D;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.NodeType;
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;

@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Image extends AbstractSemanticNode {

    String id;

    String representationHash;

    ImageType imageType;
    boolean transparent;
    Rectangle2D position;

    TextBlock leafTextBlock;

    boolean redaction;
    boolean ignored;
    @Builder.Default
    String redactionReason = "";
    @Builder.Default
    String legalBasis = "";
    @Builder.Default
    int matchedRule = -1;

    @EqualsAndHashCode.Exclude
    Page page;


    @Override
    public NodeType getType() {

        return NodeType.IMAGE;
    }


    @Override
    public TextBlock getTextBlock() {

        return leafTextBlock;
    }


    @Override
    public Set<Page> getPages() {

        return Collections.singleton(page);
    }


    @Override
    public String toString() {

        return getTreeId() + ": " + NodeType.IMAGE + ": " + imageType.toString() + " " + position;
    }


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

        Map<Page, Rectangle2D> bBoxPerPage = new HashMap<>();
        bBoxPerPage.put(page, position);
        return bBoxPerPage;
    }


    @Override
    public boolean isLeaf() {

        return true;
    }


    public double getArea() {

        return position.getWidth() * position.getHeight();
    }


    public boolean isFullPageImage() {

        return imageType.equals(ImageType.OCR) || getArea() >= 0.5 * page.getArea();
    }

}
