package com.iqser.red.service.persistence.management.v1.processor.migration.migrations.model.reducetext;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RedRectangle2D {

    public static final double THRESHOLD = 0.01;

    private double x;
    private double y;
    private double width;
    private double height;


    public boolean contains(double x, double y, double w, double h) {

        if (isEmpty() || w <= 0 || h <= 0) {
            return false;
        }
        double x0 = getX();
        double y0 = getY();
        return round(x) >= round(x0) && round(y) >= round(y0) && (x + w) - (x0 + getWidth()) <= THRESHOLD && (y + h) - (y0 + getHeight()) <= THRESHOLD;
    }


    @JsonIgnore
    public boolean isEmpty() {

        return width <= 0.0f || height <= 0.0f;
    }


    private double round(double value) {

        double d = Math.pow(10, 2);
        return Math.round(value * d) / d;
    }

}
