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

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

import com.knecon.fforesight.service.layoutparser.processor.model.table.Cell;
import com.knecon.fforesight.service.layoutparser.processor.model.table.Rectangle;
import com.knecon.fforesight.service.layoutparser.processor.model.table.Ruling;

public class GeometricComparators {

    private static final int COMPARATOR_ROUNDING = 2;

    public static final Comparator<Point2D> X_FIRST_POINT_COMPARATOR = (point1, point2) -> {

        int rv = 0;
        float point1X = DoubleComparisons.round(point1.getX(), COMPARATOR_ROUNDING);
        float point1Y = DoubleComparisons.round(point1.getY(), COMPARATOR_ROUNDING);
        float point2X = DoubleComparisons.round(point2.getX(), COMPARATOR_ROUNDING);
        float point2Y = DoubleComparisons.round(point2.getY(), COMPARATOR_ROUNDING);

        if (point1X > point2X) {
            rv = 1;
        } else if (point1X < point2X) {
            rv = -1;
        } else if (point1Y > point2Y) {
            rv = 1;
        } else if (point1Y < point2Y) {
            rv = -1;
        }
        return rv;
    };

    public static final Comparator<Point2D> Y_FIRST_POINT_COMPARATOR = (point1, point2) -> {

        int rv = 0;
        float point1X = DoubleComparisons.round(point1.getX(), COMPARATOR_ROUNDING);
        float point1Y = DoubleComparisons.round(point1.getY(), COMPARATOR_ROUNDING);
        float point2X = DoubleComparisons.round(point2.getX(), COMPARATOR_ROUNDING);
        float point2Y = DoubleComparisons.round(point2.getY(), COMPARATOR_ROUNDING);

        if (point1Y > point2Y) {
            rv = 1;
        } else if (point1Y < point2Y) {
            rv = -1;
        } else if (point1X > point2X) {
            rv = 1;
        } else if (point1X < point2X) {
            rv = -1;
        }
        return rv;
    };

    public static final Comparator<Cell> CELL_SIZE_COMPARATOR = (cell1, cell2) -> {

        Double cell1Size = cell1.getHeight() * cell1.getWidth();
        Double cell2Size = cell2.getHeight() * cell2.getWidth();
        return cell1Size.compareTo(cell2Size);
    };

    public static final Comparator<Rectangle> RECTANGLE_SIZE_COMPARATOR = (rect1, rect2) -> {

        Double rect1Size = rect1.getHeight() * rect1.getWidth();
        Double rect2Size = rect2.getHeight() * rect2.getWidth();
        return rect1Size.compareTo(rect2Size);
    };

    public static final Comparator<Ruling> X_FIRST_RULING_COMPARATOR = (ruling1, ruling2) -> {

        int rv = 0;
        float point1X = DoubleComparisons.round(Math.min(ruling1.getLeft(), ruling1.getRight()), COMPARATOR_ROUNDING);
        float point1Y = DoubleComparisons.round(Math.min(ruling1.getTop(), ruling1.getBottom()), COMPARATOR_ROUNDING);
        float point2X = DoubleComparisons.round(Math.min(ruling2.getLeft(), ruling2.getRight()), COMPARATOR_ROUNDING);
        float point2Y = DoubleComparisons.round(Math.min(ruling2.getTop(), ruling2.getBottom()), COMPARATOR_ROUNDING);

        if (point1X > point2X) {
            rv = 1;
        } else if (point1X < point2X) {
            rv = -1;
        } else if (point1Y > point2Y) {
            rv = 1;
        } else if (point1Y < point2Y) {
            rv = -1;
        }
        return rv;
    };

}
