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

import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.knecon.fforesight.service.layoutparser.processor.model.text.TextDirection;
import com.knecon.fforesight.service.layoutparser.processor.utils.RectangleTransformations;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public abstract class TextBoundingBox extends BoundingBox {

    protected Rectangle2D bBoxDirAdj;

    protected TextDirection dir;


    @Override
    public void setToBBoxOfComponents(List<? extends BoundingBox> components) {

        super.setToBBoxOfComponents(components);
        this.bBoxDirAdj = components.stream()
                .filter(c -> c instanceof TextBoundingBox)
                .map(c -> (TextBoundingBox) c)
                .map(TextBoundingBox::getBBoxDirAdj)
                .collect(RectangleTransformations.collectBBox());

        Set<TextDirection> textDirections = components.stream()
                .filter(c -> c instanceof TextBoundingBox)
                .map(c -> (TextBoundingBox) c)
                .map(TextBoundingBox::getDir)
                .collect(Collectors.toSet());

        if (textDirections.isEmpty()) {
            dir = TextDirection.ZERO;
        } else if (textDirections.size() > 1) {
            throw new IllegalArgumentException("More than one text direction found");
        } else {
            dir = textDirections.iterator().next();
        }
    }


    public double getXDirAdj() {

        return this.bBoxDirAdj.getX();
    }


    public double getYDirAdj() {

        return this.bBoxDirAdj.getY();
    }


    public double getWidthDirAdj() {

        return this.bBoxDirAdj.getWidth();
    }


    public double getHeightDirAdj() {

        return this.bBoxDirAdj.getHeight();
    }


    public double getMaxXDirAdj() {

        return this.bBoxDirAdj.getMaxX();
    }


    public double getMaxYDirAdj() {

        return this.bBoxDirAdj.getMaxY();
    }


    public double getCenterYDirAdj() {

        return this.bBoxDirAdj.getCenterY();
    }


    public double getCenterXDirAdj() {

        return this.bBoxDirAdj.getCenterX();
    }

}
