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

import java.util.Comparator;
import java.util.List;

import com.knecon.fforesight.service.layoutparser.processor.utils.RectangleTransformations;

import lombok.Data;

@Data
public class Zone extends BoundingBox {

    private List<Line> lines;


    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
    public Zone(List<Line> lines) {

        lines.sort(Comparator.comparingDouble(Line::getY0));
        this.lines = lines;
        buildBBox();
    }


    public void buildBBox() {

        this.setBBox(getLines().stream()
                             .map(BoundingBox::getBBox)
                             .collect(RectangleTransformations.collectBBox()));
    }


    public String toString() {

        StringBuilder sb = new StringBuilder();
        lines.forEach(line -> sb.append(line.toString()).append("\n"));
        return sb.toString().trim();
    }

}
