package com.knecon.fforesight.service.layoutparser.server.utils;

import java.io.File;
import java.nio.file.Path;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingRequest;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;
import com.knecon.fforesight.service.layoutparser.processor.LayoutParsingPipeline;
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationDocument;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Document;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.TableServiceResponse;
import com.knecon.fforesight.service.layoutparser.processor.python_api.model.table.VisualLayoutParsingResponse;
import com.knecon.fforesight.service.layoutparser.processor.services.factory.DocumentGraphFactory;

import lombok.SneakyThrows;

public abstract class BuildDocumentTest extends AbstractTest {

    @Autowired
    protected LayoutParsingPipeline layoutParsingPipeline;


    @SneakyThrows
    protected ClassificationDocument parseLayout(String filename, LayoutParsingType layoutParsingType) {

        File fileResource = new ClassPathResource(filename).getFile();
        prepareStorage(filename);
        return layoutParsingPipeline.parseLayout(layoutParsingType,
                                                 fileResource,
                                                 layoutParsingStorageService.getImagesFile(IMAGE_FILE_ID),
                                                 new TableServiceResponse(),
                                                 new VisualLayoutParsingResponse(),
                                                 Map.of("file", filename, "debug", "true"));
    }


    @SneakyThrows
    protected Document buildGraph(String filename) {

        return buildGraph(filename, LayoutParsingType.REDACT_MANAGER_OLD);
    }


    @SneakyThrows
    protected Document buildGraph(String filename, LayoutParsingType layoutParsingType) {

        if (!filename.startsWith("files") && filename.startsWith("/")) {
            LayoutParsingRequest layoutParsingRequest = buildDefaultLayoutParsingRequest(Path.of(filename).getFileName().toString(), LayoutParsingType.REDACT_MANAGER, true);
            prepareStorage(layoutParsingRequest, new File(filename));
            return DocumentGraphFactory.buildDocumentGraph(layoutParsingType,
                                                           layoutParsingPipeline.parseLayout(layoutParsingType,
                                                                                             new File(filename),
                                                                                             layoutParsingStorageService.getImagesFile(layoutParsingRequest.imagesFileStorageId().get()),
                                                                                             new TableServiceResponse(),
                                                                                             new VisualLayoutParsingResponse(),
                                                                                             layoutParsingRequest.identifier()));
        } else {
            if (filename.equals("files/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf")) {
                prepareStorage(filename, "cv_table_parsing_response/empty.json", "image_service_response/S-Metolachlor_RAR_01_Volume_1_2018-09-06.IMAGE_INFO.json");
            } else {
                prepareStorage(filename);
            }
            return DocumentGraphFactory.buildDocumentGraph(layoutParsingType, parseLayout(filename, layoutParsingType));
        }

    }

}

