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

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

#include "ppSub.h"


static void subDataFree(ppSubData *data)
{
    if (data->statsFile) {
        psString stats = psMetadataConfigFormat(data->stats); // Statistics to output
        if (!stats || strlen(stats) == 0) {
            psWarning("Unable to generate statistics file.");
        } else {
            fprintf(data->statsFile, "%s", stats);
        }
        psFree(stats);
        fclose(data->statsFile);
        pmConfigRunFilenameAddWrite(data->config, "STATS", data->statsName);
    }
    psFree(data->statsName);
    psFree(data->stamps);
    psFree(data->psf);
    psFree(data->stats);

    psString dump_file = psMetadataLookupStr(NULL, data->config->arguments, "-dumpconfig");
    if (dump_file) {
        pmConfigDump(data->config, dump_file);
    }
    psFree(data->config);

    return;
}

ppSubData *ppSubDataAlloc(pmConfig *config)
{
    ppSubData *data = psAlloc(sizeof(ppSubData)); // Processing data, to return
    psMemSetDeallocator(data, (psFreeFunc)subDataFree);

    data->config = config;
    data->quality = 0;
    data->photometry = false;
    data->inverse = false;
    data->stamps = NULL;
    data->psf = NULL;
    data->statsName = NULL;
    data->statsFile = NULL;
    data->stats = psMetadataAlloc();
    psMetadataAddS32(data->stats, PS_LIST_TAIL, "QUALITY", 0, "Data quality", 0);

    return data;
}


void ppSubDataQuality(ppSubData *data, psErrorCode error, ppSubFiles files)
{
    psAssert(data, "Require processing data");

    if (psMetadataLookupS32(NULL, data->stats, "QUALITY") == 0) {
        data->quality = error;
        psMetadataAddS32(data->stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, "Data quality", error);
    }

    ppSubFilesActivate(data->config, files, false);

    psErrorClear();

    return;
}
