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

import java.util.Comparator;
import java.util.HashMap;

public class TextPositionSequenceComparator implements Comparator<Word> {

    private HashMap<Word, TextBlockOnPage> lookup;


    public TextPositionSequenceComparator(HashMap<Word, TextBlockOnPage> lookup) {

        this.lookup = lookup;
    }


    @Override
    public int compare(Word number1, Word number2) {

        int page1 = lookup.get(number1).page().getPageNumber();
        int page2 = lookup.get(number2).page().getPageNumber();

        if (page1 != page2) {
            return Integer.compare(page1, page2);
        }

        if (number1.getY() != number2.getY()) {
            return Double.compare(number1.getY(), number2.getY());
        }

        return Integer.compare(Integer.parseInt(number1.toString()), Integer.parseInt(number2.toString()));
    }

}
