package com.knecon.fforesight.service.layoutparser.processor.model.outline;

import java.awt.geom.Point2D;
import java.util.Optional;

import com.knecon.fforesight.service.layoutparser.processor.docstrum.model.BoundingBox;

import lombok.Getter;
import lombok.Setter;

public class OutlineObject {

    @Getter
    private final String title;
    @Getter
    private final int pageNumber;
    @Getter
    private final int treeDepth;

    private Point2D point; // java coordinates, (0, 0) is always top left

    @Getter
    @Setter
    private boolean found;


    public OutlineObject(String title, int pageNumber, Point2D point2D, int depth) {

        this.title = title;
        this.pageNumber = pageNumber;
        this.treeDepth = depth;
        this.point = point2D;
    }


    @Override
    public String toString() {

        return "OutlineObject{" + "title='" + title + '\'' + '}';
    }


    public Optional<Point2D> getPoint() {

        return Optional.ofNullable(point);
    }


    public boolean isAbove(BoundingBox boundingBox) {

        if (point == null) {
            return true;
        }
        return point.getY() <= boundingBox.getMaxY();
    }


    public double distance(BoundingBox boundingBox) {

        if (point == null) {
            return 0;
        }
        if (boundingBox.getBBox().contains(point)) {
            return 0;
        }
        double deltaX = Math.min(Math.abs(boundingBox.getMinX() - point.getX()), Math.abs(boundingBox.getMaxX() - point.getX()));
        double deltaY = Math.min(Math.abs(boundingBox.getMinY() - point.getY()), Math.abs(boundingBox.getMaxY() - point.getY()));
        return Math.sqrt(deltaX * deltaX + deltaY * deltaY);
    }


    public void resetPoint() {

        this.point = null;
    }

}
