package com.knecon.fforesight.service.layoutparser.processor.utils;

import com.iqser.red.service.persistence.service.v1.api.shared.model.redactionlog.Rectangle;
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;

import lombok.experimental.UtilityClass;

@UtilityClass
@SuppressWarnings("all")
public final class PositionUtils {

    // TODO This currently uses pdf coord system. In the futher this should use java coord system.
    // Note: DirAdj (TextDirection Adjusted) can not be user for this.
    public boolean isWithinBodyTextFrame(Rectangle btf, TextPageBlock textBlock) {

        if (btf == null || textBlock == null) {
            return false;
        }

        double threshold = textBlock.getMostPopularWordHeight() * 3;

        if (textBlock.getPdfMinX() + threshold > btf.getTopLeft().getX()
                && textBlock.getPdfMaxX() - threshold < btf.getTopLeft().getX() + btf.getWidth()
                && textBlock.getPdfMinY() + threshold > btf.getTopLeft().getY()
                && textBlock.getPdfMaxY() - threshold < btf.getTopLeft().getY() + btf.getHeight()) {
            return true;
        } else {
            return false;
        }

    }


    // TODO This currently uses pdf coord system. In the futher this should use java coord system.
    // Note: DirAdj (TextDirection Adjusted) can not be user for this.
    public boolean isOverBodyTextFrame(Rectangle btf, TextPageBlock textBlock, int rotation) {

        if (btf == null || textBlock == null) {
            return false;
        }

        if (rotation == 90 && textBlock.getPdfMaxX() < btf.getTopLeft().getX()) {
            return true;
        }

        if (rotation == 180 && textBlock.getPdfMaxY() < btf.getTopLeft().getY()) {
            return true;
        }

        if (rotation == 270 && textBlock.getPdfMinX() > btf.getTopLeft().getX() + btf.getWidth()) {
            return true;
        }

        if (rotation == 0 && textBlock.getPdfMinY() > btf.getTopLeft().getY() + btf.getHeight()) {
            return true;
        } else {
            return false;
        }

    }


    // TODO This currently uses pdf coord system. In the futher this should use java coord system.
    // Note: DirAdj (TextDirection Adjusted) can not be user for this.
    public boolean isUnderBodyTextFrame(Rectangle btf, TextPageBlock textBlock, int rotation) {

        if (btf == null || textBlock == null) {
            return false;
        }

        if (rotation == 90 && textBlock.getPdfMinX() > btf.getTopLeft().getX() + btf.getWidth()) {
            return true;
        }

        if (rotation == 180 && textBlock.getPdfMinY() > btf.getTopLeft().getY() + btf.getHeight()) {
            return true;
        }

        if (rotation == 270 && textBlock.getPdfMaxX() < btf.getTopLeft().getX()) {
            return true;
        }

        if (rotation == 0 && textBlock.getPdfMaxY() < btf.getTopLeft().getY()) {
            return true;
        } else {
            return false;
        }

    }


    // TODO This currently uses pdf coord system. In the futher this should use java coord system.
    // Note: DirAdj (TextDirection Adjusted) can not be user for this.
    public boolean isTouchingUnderBodyTextFrame(Rectangle btf, TextPageBlock textBlock) {

        //TODO Currently this is not working for rotated pages.

        if (btf == null || textBlock == null) {
            return false;
        }

        if (textBlock.getMinY() < btf.getTopLeft().getY()) {
            return true;
        } else {
            return false;
        }

    }


    public float getHeightDifferenceBetweenChunkWordAndDocumentWord(TextPageBlock textBlock, Float documentMostPopularWordHeight) {

        return textBlock.getMostPopularWordHeight() - documentMostPopularWordHeight;
    }


    public Float getApproxLineCount(TextPageBlock textBlock) {

        return textBlock.getHeight() / textBlock.getMostPopularWordHeight();
    }

}
