#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <pslib.h>
#include <psmodules.h>

#include "ppStack.h"
#include "ppStackLoop.h"

bool ppStackCombineInitial(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config)
{
    psAssert(stack, "Require stack");
    psAssert(options, "Require options");
    psAssert(config, "Require configuration");

    if (!options->convolve) {
        // No need to do initial combination when we haven't convolved
        return true;
    }

    psTimerStart("PPSTACK_INITIAL");

    psMetadata *ppsub = psMetadataLookupMetadata(NULL, config->recipes, "PPSUB"); // PPSUB recipe
    int overlap = 2 * psMetadataLookupS32(NULL, ppsub,
                                          "KERNEL.SIZE"); // Overlap by kernel size between consecutive scans


    bool status;                    // Status of read
    int numChunk;                   // Number of chunks
    for (numChunk = 0; true; numChunk++) {
        ppStackThread *thread = ppStackThreadRead(&status, stack, config, numChunk, overlap);
        if (!status) {
            // Something went wrong
            psError(PS_ERR_UNKNOWN, false, "Unable to read chunk %d", numChunk);
            return false;
        }
        if (!thread) {
            // Nothing more to read
            break;
        }

        psThreadJob *job = psThreadJobAlloc("PPSTACK_INITIAL_COMBINE"); // Job to start
        psArrayAdd(job->args, 1, thread);
        psArrayAdd(job->args, 1, options);
        psArrayAdd(job->args, 1, config);
        if (!psThreadJobAddPending(job)) {
            psFree(job);
            return false;
        }
        psFree(job);
    }

    if (!psThreadPoolWait(false)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to do initial combination.");
        return false;
    }

    // Harvest the jobs, gathering the inspection lists
    options->inspect = psArrayAlloc(options->num);
    for (int i = 0; i < options->num; i++) {
        if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
            continue;
        }
        options->inspect->data[i] = psArrayAllocEmpty(numChunk);
    }
    psThreadJob *job;               // Completed job
    while ((job = psThreadJobGetDone())) {
        psAssert(strcmp(job->type, "PPSTACK_INITIAL_COMBINE") == 0,
                 "Job has incorrect type: %s", job->type);
        psArray *results = job->results; // Results of job
        for (int i = 0; i < options->num; i++) {
            if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
                continue;
            }
            options->inspect->data[i] = psArrayAdd(options->inspect->data[i], 1, results->data[i]);
        }
        psFree(job);
    }

    ppStackMemDump("initial");

#ifdef TESTING
    ppStackWriteImage("combined_initial.fits", NULL, outRO->image, config);
    pmStackVisualPlotTestImage(outRO->image, "combined_initial.fits");
#endif

    psFree(options->matchChi2); options->matchChi2 = NULL;


    if (options->stats) {
        psMetadataAddF32(options->stats, PS_LIST_TAIL, "TIME_INITIAL", 0,
                         "Time to make final stack", psTimerMark("PPSTACK_INITIAL"));
    }

    return true;
}
