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

import java.awt.geom.Rectangle2D;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Page;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class IdBuilder {

    private final HashFunction hashFunction = Hashing.murmur3_128();


    public String buildId(Set<Page> pages, List<Rectangle2D> rectanglesPerLine) {

        return buildId(pages.stream().map(Page::getNumber).collect(Collectors.toList()), rectanglesPerLine);
    }


    public String buildId(List<Integer> pageNumbers, List<Rectangle2D> rectanglesPerLine) {

        StringBuilder sb = new StringBuilder();
        List<Integer> sortedPageNumbers = pageNumbers.stream().sorted(Comparator.comparingInt(Integer::intValue)).toList();
        sortedPageNumbers.forEach(sb::append);
        rectanglesPerLine.forEach(rectangle2D -> sb.append(Math.round(rectangle2D.getX()))
                .append(Math.round(rectangle2D.getY()))
                .append(Math.round(rectangle2D.getWidth()))
                .append(Math.round(rectangle2D.getHeight())));

        return hashFunction.hashString(sb.toString(), StandardCharsets.UTF_8).toString();
    }

}
