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.Map;

import org.springframework.stereotype.Service;

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.table.VisualLayoutParsingBox;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.VisualLayoutParsingResponse;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.VisualLayoutParsingResult;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class VisualLayoutParsingAdapter {

    private static String SIGNATURES = "signature";


    public Map<Integer, List<VisualLayoutParsingResult>> buildExtractedTablesPerPage(VisualLayoutParsingResponse visualLayoutParsingResponse) {

        Map<Integer, List<VisualLayoutParsingResult>> tableCells = new HashMap<>();
        visualLayoutParsingResponse.getData()
                .forEach(tableData -> tableCells.computeIfAbsent(tableData.getPage_idx(), tableCell -> new ArrayList<>()).addAll(convertTableCells(tableData.getBoxes())));

        return tableCells;

    }


    public Map<Integer, List<ClassifiedImage>> buildExtractedSignaturesPerPage(VisualLayoutParsingResponse visualLayoutParsingResponse) {

        Map<Integer, List<ClassifiedImage>> signatures = new HashMap<>();
        if (visualLayoutParsingResponse.getData() != null) {
            visualLayoutParsingResponse.getData()
                    .forEach(tableData -> signatures.computeIfAbsent(tableData.getPage_idx() + 1, tableCell -> new ArrayList<>())
                            .addAll(convertSignatures(tableData.getPage_idx()+1, tableData.getBoxes())));
        }
        return signatures;

    }


    public List<VisualLayoutParsingResult> convertTableCells(List<VisualLayoutParsingBox> tableObjects) {

        List<VisualLayoutParsingResult> parsedTableCells = new ArrayList<>();

        tableObjects.stream().forEach(t -> {
            VisualLayoutParsingResult result = new VisualLayoutParsingResult();
            result.setX0(t.getBox().getX1());
            result.setX1(t.getBox().getX2());
            result.setY0(t.getBox().getY1());
            result.setY1(t.getBox().getY2());
            result.setWidth(result.getX1() - result.getX0());
            result.setHeight(result.getY1() - result.getY0());
            result.setLabel(t.getLabel());
            parsedTableCells.add(result);
        });

        return parsedTableCells;

    }


    public List<ClassifiedImage> convertSignatures(int pageNumber, List<VisualLayoutParsingBox> tableObjects) {

        List<ClassifiedImage> signatures = new ArrayList<>();

        tableObjects.stream().forEach(t -> {
            if (t.getLabel().equals(SIGNATURES)) {
                ClassifiedImage signature = new ClassifiedImage(new Rectangle2D.Float(t.getBox().getX1(),
                        t.getBox().getY1(),
                        t.getBox().getX2() - t.getBox().getX1(),
                        t.getBox().getY2() - t.getBox().getY1()), ImageType.SIGNATURE, true, false, false, pageNumber);

                signatures.add(signature);
            }
        });

        return signatures;

    }

}