package com.knecon.fforesight.service.layoutparser.processor.python_api.adapter;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.ImageType;
import com.knecon.fforesight.service.layoutparser.processor.model.image.ClassifiedImage;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.image.ImageServiceResponse;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ImageServiceResponseAdapter {

    public Map<Integer, List<ClassifiedImage>> buildClassifiedImagesPerPage(ImageServiceResponse imageServiceResponse) {

        Map<Integer, List<ClassifiedImage>> images = new HashMap<>();
        imageServiceResponse.getData().forEach(imageMetadata -> {
            var classification = imageMetadata.getFilters().isAllPassed() ? ImageType.valueOf(imageMetadata.getClassification()
                    .getLabel()
                    .toUpperCase(Locale.ROOT)) : ImageType.OTHER;
            images.computeIfAbsent(imageMetadata.getPosition().getPageNumber(), x -> new ArrayList<>())
                    .add(new ClassifiedImage(new Rectangle2D.Double(imageMetadata.getPosition().getX1(),
                            imageMetadata.getPosition().getY1(),
                            imageMetadata.getGeometry().getWidth(),
                            imageMetadata.getGeometry().getHeight()), classification, imageMetadata.isAlpha(), imageMetadata.getPosition().getPageNumber()));
        });

        // Currently This is a copy but, it will be changed later because i don' t think that we should unclassified images.
        imageServiceResponse.getDataCV().forEach(imageMetadata -> {
            var classification = imageMetadata.getFilters().isAllPassed() ? ImageType.valueOf(imageMetadata.getClassification()
                    .getLabel()
                    .toUpperCase(Locale.ROOT)) : ImageType.OTHER;
            images.computeIfAbsent(imageMetadata.getPosition().getPageNumber(), x -> new ArrayList<>())
                    .add(new ClassifiedImage(new Rectangle2D.Double(imageMetadata.getPosition().getX1(),
                            imageMetadata.getPosition().getY1(),
                            imageMetadata.getGeometry().getWidth(),
                            imageMetadata.getGeometry().getHeight()), classification, imageMetadata.isAlpha(), imageMetadata.getPosition().getPageNumber()));
        });

        return images;
    }


    public void findOcr(ClassificationPage classificationPage) {

        classificationPage.getImages().forEach(image -> {
            if (image.getImageType().equals(ImageType.OTHER)) {
                classificationPage.getTextBlocks().forEach(textblock -> {
                    if (image.getPosition().contains(textblock.getMinX(), textblock.getMinY(), textblock.getWidth(), textblock.getHeight())) {
                        image.setImageType(ImageType.OCR);
                    }
                });
            }
        });
    }

}
