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

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

#include "pmConfig.h"
#include "pmDetrendDB.h"

#include "pmHDU.h"
#include "pmFPA.h"
#include "pmFPALevel.h"
#include "pmFPAview.h"
#include "pmFPAfile.h"
#include "pmConceptsPhotcode.h"

psString pmConceptsPhotcodeForView(pmConfig *config, pmFPAfile *file, const pmFPAview *view)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);
    PS_ASSERT_PTR_NON_NULL(file, NULL);
    PS_ASSERT_PTR_NON_NULL(view, NULL);

    if (view->chip < -1) {
        psError(PS_ERR_IO, true, "Photcodes undefined for FPA: defined by chip\n");
        return NULL;
    }

    // select recipe options supplied on command line
    bool mdok;                          // Status of MD lookup
    psMetadata *recipe  = psMetadataLookupPtr(&mdok, config->recipes, "PPIMAGE");
    if (!mdok || !recipe) {
        psError(PS_ERR_IO, true, "Recipe PPIMAGE not found\n");
        return NULL;
    }

    // select photcode rule from recipe
    char *rule = psMetadataLookupStr(&mdok, recipe, "PHOTCODE.RULE");
    if (!mdok || !rule || strlen(rule) == 0) {
        psError(PS_ERR_IO, true, "PHOTCODE.RULE not found in PPIMAGE recipe\n");
        return NULL;
    }

    // convert rule to real photcode
    psString photcode = pmFPAfileNameFromRule(rule, file, view);

    return photcode;
}
