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

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

import com.knecon.fforesight.service.layoutparser.processor.model.PageBlockType;
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) {

        TextPageBlock lastHeadline = getLastHeadline();
        TextPageBlock lastHeadlineFromOutline = getLastHeadlineFromOutline();
        PageBlockType originalClassifiedBlockType = getOriginalClassifiedBlockType();
        PageBlockType finalHeadlineType = initialHeadlineType;

        if (lastHeadline != null) {

            if (lastHeadline.equals(lastHeadlineFromOutline)) {

                finalHeadlineType = PageBlockType.getHeadlineType(getHeadlineNumber(lastHeadline.getClassification()) + 1);

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

                PageBlockType lastHeadlineType = lastHeadline.getClassification();
                int difference = getHeadlineNumber(originalClassifiedBlockType) - getHeadlineNumber(lastHeadlineType);
                finalHeadlineType = PageBlockType.getHeadlineType(getHeadlineNumber(initialHeadlineType) - difference);
            }
        }

        setOriginalClassifiedBlockType(initialHeadlineType);
        textBlock.setClassification(finalHeadlineType);
        setLastHeadline(textBlock);
    }

}