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

#include "ppImage.h"

int main(int argc, char **argv) {

    psLibInit(NULL);

    psTimerStart(TIMERNAME);

    // Parse the configuration and arguments
    // Open the input image(s)
    // Determine camera, format from header if not already defined
    // Construct camera in preparation for reading
    pmConfig *config = ppImageArguments(argc, argv);
    if (config == NULL) {
        psErrorStackPrint(stderr, "Unable to parse command-line arguments.");
        exit(PS_EXIT_CONFIG_ERROR);
    }

    // define recipe options
    // define the active I/O files
    ppImageOptions *options = ppImageParseCamera(config);
    if (options == NULL) {
        psErrorStackPrint(stderr, "Unable to parse camera.");
        exit(PS_EXIT_CONFIG_ERROR);
    }

    // Image Arithmetic Loop
    if (!ppImageLoop(config, options)) {
        psErrorStackPrint(stderr, "Unable to loop over input");
        exit(PS_EXIT_SYS_ERROR);
    }

    psLogMsg ("ppImage", 3, "Complete ppImage run: %f sec\n", psTimerMark(TIMERNAME));

    // Cleaning up
    ppImageCleanup(config, options);

    return PS_EXIT_SUCCESS;
}

void ppImageFileCheck (pmConfig *config) {

    // add the output names to the output-type files
    psMetadataItem *item = NULL;
    psMetadataIterator *iter = psMetadataIteratorAlloc (config->files, PS_LIST_HEAD, NULL);
    while ((item = psMetadataGetAndIncrement (iter)) != NULL) {
        pmFPAfile *file = item->data.V;
        pmFPA *fpa = file->fpa;
        fprintf (stderr, "file %s\n", file->name);
        if (!fpa) {
            fprintf (stderr, "  has no FPA\n");
            continue;
        }
        if (fpa->hdu) {
            if (fpa->hdu->images) fprintf (stderr, "  (%d,%d) images\n", -1, -1);
            if (fpa->hdu->weights) fprintf (stderr, "  (%d,%d) weights\n", -1, -1);
            if (fpa->hdu->masks) fprintf (stderr, "  (%d,%d) masks\n", -1, -1);
            if (fpa->hdu->header) fprintf (stderr, "  (%d,%d) header\n", -1, -1);
            continue;
        }
        fprintf (stderr, "  has no fpa data (%d,%d)\n", -1, -1);
        for (int i = 0; i < fpa->chips->n; i++) {
            pmChip *chip = fpa->chips->data[i];
            if (chip->hdu) {
                if (chip->hdu->images) fprintf (stderr, "  (%d,%d) images\n", i, -1);
                if (chip->hdu->weights) fprintf (stderr, "  (%d,%d) weights\n", i, -1);
                if (chip->hdu->masks) fprintf (stderr, "  (%d,%d) masks\n", i, -1);
                if (chip->hdu->header) fprintf (stderr, "  (%d,%d) header\n", i, -1);
                continue;
            }
            fprintf (stderr, "  has no chip data (%d,%d)\n", i, -1);
            for (int j = 0; j < chip->cells->n; j++) {
                pmCell *cell = chip->cells->data[j];
                if (cell->hdu) {
                    if (cell->hdu->images) fprintf (stderr, "  (%d,%d) images\n", i, j);
                    if (cell->hdu->weights) fprintf (stderr, "  (%d,%d) weights\n", i, j);
                    if (cell->hdu->masks) fprintf (stderr, "  (%d,%d) masks\n", i, j);
                    if (cell->hdu->header) fprintf (stderr, "  (%d,%d) header\n", i, j);
                    continue;
                }
                fprintf (stderr, "  has no cell data (%d,%d)\n", i, j);
            }
        }
    }
    psFree (iter);
}
