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

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

import com.knecon.fforesight.service.layoutparser.processor.services.classification.NumberWord;

public class TocNumberComparator implements Comparator<NumberWord> {

    private HashMap<NumberWord, TextBlockOnPage> lookup;


    public TocNumberComparator(HashMap<NumberWord, TextBlockOnPage> lookup) {

        this.lookup = lookup;
    }


    @Override
    public int compare(NumberWord number1, NumberWord 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.word().getY() != number2.word().getY()) {
            return Double.compare(number1.word().getY(), number2.word().getY());
        }

        return Integer.compare(number1.number(), number2.number());
    }

}
