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

#include <ppStats.h>
#include "ppImage.h"
#include "ppImageDetrendFringe.h"
#include "ppImageVersion.h"

bool ppImageLoop (pmConfig *config, ppImageOptions *options) {

    bool mdok;                      // Status of MD lookup
    bool status;
    pmChip *chip;
    pmCell *cell;
    pmReadout *readout;
    psMetadata *stats = NULL;

    const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); // Filename for statistics
    FILE *statsFile = NULL;             // File stream for statistics
    if (mdok && statsName && strlen(statsName) > 0) {
        psString resolved = pmConfigConvertFilename(statsName, config, true); // Resolved filename
        statsFile = fopen(resolved, "w");
        if (!statsFile) {
            psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
            psFree(resolved);
            return false;
        }
        stats = psMetadataAlloc();
        psFree(resolved);
    }

    pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "PPIMAGE.INPUT");
    if (!status) {
        psErrorStackPrint(stderr, "Can't find input data!\n");
        ppImageCleanup(config, options);
        exit(PS_EXIT_PROG_ERROR);
    }

    pmFPAview *view = pmFPAviewAlloc(0);// View for level of interest
    pmHDU *lastHDU = NULL;              // Last HDU that was updated

    // files associated with the science image
    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
        psFree (view);
        return false;
    }

    while ((chip = pmFPAviewNextChip(view, input->fpa, 1)) != NULL) {
        psLogMsg ("ppImageLoop", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
        if (!chip->process || !chip->file_exists) {
            continue;
        }

        if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
            psFree (view);
            return false;
        }

        while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
            psLogMsg ("ppImageLoop", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
            if (!cell->process || !cell->file_exists) {
                continue;
            }
            if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
                psFree (view);
                return false;
            }

            // Put version information into the header
            pmHDU *hdu = pmHDUFromCell(cell);
            if (hdu && hdu != lastHDU) {
                ppImageVersionMetadata(hdu->header);
                lastHDU = hdu;
            }


            // process each of the readouts
            while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
                if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
                    psFree (view);
                    return false;
                }
                if (!readout->data_exists) {
                    continue;
                }

                // perform the detrend analysis
                if (!ppImageDetrendReadout(config, options, view)) {
                    psError(PS_ERR_UNKNOWN, false, "Unable to detrend readout.\n");
                    psFree (view);
                    return false;
                }

            }
        }

        // Solve the fringe system
        if (options->doFringe) {
            pmChip *fringe = pmFPAfileThisChip(config->files, view, "PPIMAGE.FRINGE");
            if (!ppImageDetrendFringeSolve(chip, fringe, options)) {
                psFree (view);
                return false;
            }
        }

        // Go back over the cells to apply the fringe and do statistics
        view->cell = view->readout = -1;
        while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
            if (!cell->process || !cell->file_exists) {
                continue;
            }

            // Apply the fringe correction
            if (options->doFringe) {
                psTrace("ppImage", 3, "Applying fringe correction...\n");
                pmCell *fringeCell = pmFPAfileThisCell(config->files, view, "PPIMAGE.FRINGE");
                if (!ppImageDetrendFringeGenerate(cell, fringeCell)) {
                    psFree (view);
                    return false;
                }
            }

            // Perform statistics on the detrended cell
            if (stats) {
                bool mdok;              // Status of MD lookup
                pmFPAfile *output = psMetadataLookupPtr(&mdok, config->files, "PPIMAGE.OUTPUT");
                if (!mdok || !output) {
                    psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find file PPIMAGE.OUTPUT.\n");
                    psFree (view);
                    return false;
                }

                if (!ppStats(stats,
                             output->fpa,
                             view,
                             options->satMask | options->badMask | options->maskValue,
                             config)) {
                    psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to generate stats for image.\n");
                    psFree (stats);
                    psFree (view);
                    return false;
                }
            }

            // Add MD5 information for cell
            pmHDU *hdu = pmHDUFromCell(cell); // HDU that owns the cell
            while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
                const char *chipName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
                const char *cellName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME");

                psString headerName = NULL; // Header name for MD5
                psStringAppend(&headerName, "MD5_%s_%s_%d", chipName, cellName, view->readout);

                psVector *md5 = psImageMD5(readout->image); // md5 hash
                psString md5string = psMD5toString(md5); // String
                psFree(md5);
                psMetadataAddStr(hdu->header, PS_LIST_TAIL, headerName, PS_META_REPLACE,
                                 "Image MD5", md5string);
                psFree(md5string);
                psFree(headerName);
            }
        }

        if (!ppImageMosaicChip(config, options, view, "PPIMAGE.CHIP", "PPIMAGE.OUTPUT")) {
            psError(PS_ERR_UNKNOWN, false, "Unable to mosaic chip.\n");
            psFree (view);
            return false;
        }
        if (!ppImageRebinChip(config, view, "PPIMAGE.BIN1")) {
            psError(PS_ERR_UNKNOWN, false, "Unable to bin chip.\n");
            psFree (view);
            return false;
        }
        if (!ppImageRebinChip(config, view, "PPIMAGE.BIN2")) {
            psError(PS_ERR_UNKNOWN, false, "Unable to bin chip.\n");
            psFree (view);
            return false;
        }

        // we perform photometry on the readouts of this chip in the output
        if (options->doPhotom) {
            if (!ppImagePhotom(config, view)) {
                psError(PS_ERR_UNKNOWN, false, "error running photometry.\n");
                psFree (view);
                return false;
            }
        }

        // Close cells
        view->cell = -1;
        while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
            if (!cell->process || !cell->file_exists) {
                continue;
            }

            if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
                psFree(view);
                return false;
            }
        }

        // Close chip
        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
            psFree(view);
            return false;
        }
    }

    // Write out summary statistics
    if (stats) {
        char *statsMDC = psMetadataConfigFormat(stats);
        if (!statsMDC || strlen(statsMDC) == 0) {
            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
        } else {
            fprintf(statsFile, "%s", statsMDC);
        }
        psFree(statsMDC);
        fclose(statsFile);
        psFree(stats);
    }

    // XXX an optional full-scale FPA mosaic could happen here
    // it would require us to NOT free PPIMAGE.CHIP until here
    // ppImageMosaicFPA (config, "PPIMAGE.OUTPUT.FPA", "PPIMAGE.CHIP");
    ppImageMosaicFPA(config, options, "PPIMAGE.OUTPUT.FPA1", "PPIMAGE.BIN1");
    ppImageMosaicFPA(config, options, "PPIMAGE.OUTPUT.FPA2", "PPIMAGE.BIN2");

    // we perform astrometry on all chips after sources have been detected
    if (options->doAstromChip || options->doAstromMosaic) {
        if (!ppImageAstrom(config)) {
            psError(PS_ERR_UNKNOWN, false, "error running astrometry.\n");
            psFree(view);
            return false;
        }
    }

    // Close FPA
    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
        psFree(view);
        return false;
    }

    psFree(view);
    return true;
}
