package de.hftstuttgart.dtabackend.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Volume;
import de.hftstuttgart.dtabackend.models.ResultSummary;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;

@Component
public class ExecuteTestUtil  {
    private static final Logger LOG = LogManager.getLogger(ExecuteTestUtil.class);

    private final JGitUtil jGitUtil;
    private final DockerUtil dockerUtil;
    private final String assignmentBasePath;
    private final Path testTmpPathHost;
    private final Path testTmpPath;

    public ExecuteTestUtil(
        Environment env,
        JGitUtil jGitUtil,
        DockerUtil dockerUtil
    ) {
        this.jGitUtil = jGitUtil;
        this.dockerUtil = dockerUtil;

        // set base path for assignments to be stored
        Path p = Paths.get(
            env.getProperty("data.dir"),
            env.getProperty("data.dir.test.folder.name"));
        this.assignmentBasePath = p.toAbsolutePath().toString();

        // set path of temporary directory on host and inside our container
        this.testTmpPathHost = Paths.get(env.getProperty("host.tests.tmp.dir"));
        this.testTmpPath = Paths.get(env.getProperty("tests.tmp.dir"));
    }

    public ResultSummary runTests(String assignmentId, Path workDirectory) throws IOException, InterruptedException {

        // define paths for the test, the submission and where the result is to be expected afterwards
        Path testPath   = Paths.get(workDirectory.toAbsolutePath().toString(), "/test");
        Path srcPath    = Paths.get(workDirectory.toAbsolutePath().toString(), "/src");
        Path resultPath = Paths.get(workDirectory.toAbsolutePath().toString(), "/result");

        // clone stored test to tmpdir
        LOG.debug("copying pre-downloaded unitttest repo");
        FileUtil.copyFolder(
            Paths.get(
                assignmentBasePath,
                assignmentId
            ),
            testPath
        );

        LOG.debug("copy test config");
        Files.copy(
            Paths.get(
                assignmentBasePath,
                assignmentId + ".txt"
            ),
            Paths.get(
                workDirectory.toAbsolutePath().toString(),
                "config.txt"
            )
        );

        Files.createDirectory(resultPath);

        LOG.info("reading test config");
        Matcher config = RegexUtil.findProfessorConfig(
            new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()));

        // define the paths to mount as Binds from Host to the test-container
        Path testPathHost = Paths.get(
            testTmpPathHost.toAbsolutePath().toString(),
            workDirectory.getName(workDirectory.getNameCount()-1).toString(),
            testPath.getName(testPath.getNameCount()-1).toString()
        );
        Path srcPathHost = Paths.get(
            testTmpPathHost.toAbsolutePath().toString(),
            workDirectory.getName(workDirectory.getNameCount()-1).toString(),
            srcPath.getName(srcPath.getNameCount()-1).toString()
        );
        Path resultPathHost = Paths.get(
            testTmpPathHost.toAbsolutePath().toString(),
            workDirectory.getName(workDirectory.getNameCount()-1).toString(),
            resultPath.getName(resultPath.getNameCount()-1).toString()
        );

        // start test-container with professor given image and bind mounts for test, submission and result
        dockerUtil.runContainer(
            config.group(4),
            new Bind(testPathHost.toAbsolutePath().toString(), new Volume("/data/test")),
            new Bind(srcPathHost.toAbsolutePath().toString(), new Volume("/data/src")),
            new Bind(resultPathHost.toAbsolutePath().toString(), new Volume("/data/result"))
        );

        // define expected result file
        File resultFile = Paths.get(resultPath.toAbsolutePath().toString(), "result.json").toFile();

        // check if result file is there
        if (!resultFile.exists() || !resultFile.isFile()) {
            LOG.error(String.format("couln't find result file in %s", resultFile.getAbsolutePath()));
            throw new RuntimeException("no resultfile found");
        }

        LOG.debug("parse results json");
        ObjectMapper objectMapper = new ObjectMapper();
        ResultSummary resultSummary = objectMapper.readValue(
            resultFile.toURI().toURL(),
            ResultSummary.class);

        return resultSummary;
    }
}