#include "InitBatch.h"

/**
  Destructor
  */
static void destroy(InitBatch* this) {

    // call superclass destructor
    this->base.destroy(&this->base);
    free (this);
}

/**
  Does the work.
  */
static int run(InitBatch* this) {

    if (this->base.exitCode != PS_EXIT_SUCCESS) return this->base.exitCode;

    if (!this->base.fitsGenerator->populateFromFile(this->base.fitsGenerator, this->base.fitsOut)) 
        this->base.exitCode = PS_EXIT_CONFIG_ERROR;
    else this->base.exitCode = PS_EXIT_SUCCESS;

    return this->base.exitCode;
}

/**
  Print-out this class. Calls base-calls print method first.
  */
static void print(InitBatch* this) {

    this->base.print(&this->base);
}

/**
  Reads command-line arguments.
  */
static bool parseArguments(InitBatch* this, int argc, char **argv) {

    this->base.parseArguments(&this->base, argc, argv, "/init", "00000000.FITS");

    if (
            !this->base.gotFitsOutPath() ||
            !this->base.gotConfig() ) {

        this->base.logger->print(this->base.logger, MSG_ERROR, "InitBatch", "Problem with supplied arguments\n");
        this->base.exitCode = PS_EXIT_CONFIG_ERROR;
        return false;
    }

    return true;
}

/**
  Constructor
  */
InitBatch* new_InitBatch(Logger* logger, int *argc, char **argv) {

    logger->print(logger, MSG_DEBUG, "InitBatch", "Constructor\n");
    InitBatch *this = (InitBatch*)calloc(1, sizeof(InitBatch));

    // call base-class constructor
    new_Batch(logger, &this->base);

    // method pointers
    this->print = print;
    this->destroy = destroy;
    this->base.run = run;

    parseArguments(this, *argc, argv);

    this->print(this);

    return this;
}

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

//    ippToPsps_VersionPrint();

    Logger* logger = new_Logger(NULL, false);
    logger->print(logger, MSG_INFO, "main", "Creating new initialization batch\n");

    InitBatch* initBatch = new_InitBatch(logger, &argc, argv);
    initBatch->base.run(initBatch);
    int exitCode = initBatch->base.exitCode;

    initBatch->destroy(initBatch);

    // tidy up
    psLibFinalize();
    logger->destroy(logger);

    return exitCode;
}


