package com.knecon.fforesight.service.layoutparser.processor.services.classification;

import static com.knecon.fforesight.service.layoutparser.processor.model.PageBlockType.getHeadlineNumber;

import java.util.List;

import com.knecon.fforesight.service.layoutparser.processor.model.PageBlockType;
import com.knecon.fforesight.service.layoutparser.processor.model.SectionIdentifier;
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class HeadlineClassificationService {

    TextPageBlock lastHeadline;
    PageBlockType originalClassifiedBlockType;
    TextPageBlock lastHeadlineFromOutline;


    public void setLastHeadlineFromOutline(TextPageBlock lastHeadlineFromOutline) {

        this.lastHeadlineFromOutline = lastHeadlineFromOutline;
        this.setLastHeadline(lastHeadlineFromOutline);
    }


    public void classifyHeadline(TextPageBlock textBlock, PageBlockType initialHeadlineType) {

        PageBlockType finalHeadlineType = initialHeadlineType;

        if (lastHeadline != null) {

            finalHeadlineType = decideOnClassification(textBlock, initialHeadlineType);
        }

        lastHeadline = textBlock;
        originalClassifiedBlockType = initialHeadlineType;
        textBlock.setClassification(finalHeadlineType);
    }


    private PageBlockType decideOnClassification(TextPageBlock textBlock, PageBlockType initialHeadlineType) {

        SectionIdentifier identifier = SectionIdentifier.fromSearchText(textBlock.getText());
        TextPageBlock lastHeadlineFromOutline = getLastHeadlineFromOutline();
        PageBlockType originalClassifiedBlockType = getOriginalClassifiedBlockType();

        if (!identifier.isEmpty()) {
            return PageBlockType.getHeadlineType(identifier.level());
        }

        if (lastHeadline.equals(lastHeadlineFromOutline) && lastHeadline.getMostPopularWordFontSize() >= textBlock.getMostPopularWordFontSize()) {

            return PageBlockType.getHeadlineType(getHeadlineNumber(lastHeadline.getClassification()) + 1);

        } else if (originalClassifiedBlockType != null && lastHeadline.getClassification() != originalClassifiedBlockType) {

            return adjustInitialLevelToLastHeadlineLevel(initialHeadlineType);
        }
        return initialHeadlineType;
    }


    private PageBlockType adjustInitialLevelToLastHeadlineLevel(PageBlockType initialHeadlineType) {

        int difference = getHeadlineNumber(originalClassifiedBlockType) - getHeadlineNumber(lastHeadline.getClassification());
        return PageBlockType.getHeadlineType(Math.max(1, getHeadlineNumber(initialHeadlineType) - difference));
    }


    public static PageBlockType headlineClassByFontSize(TextPageBlock textBlock, List<Double> fontSizeGroups) {

        List<Double> distances = fontSizeGroups.stream()
                .map(fontSize -> Math.abs(fontSize - textBlock.getMostPopularWordFontSize()))
                .toList();
        double min = Double.MAX_VALUE;
        int argMin = -1;
        for (int i = 0; i < distances.size(); i++) {
            if (distances.get(i) < min) {
                min = distances.get(i);
                argMin = i;
            }
        }
        return PageBlockType.getHeadlineType(argMin);
    }

}