/*
 * (C) Copyright 2005- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
 */

#include "grib_api_internal.h"

/*
   This is used by make_class.pl

   START_CLASS_DEF
   CLASS      = expression
   IMPLEMENTS = destroy
   IMPLEMENTS = native_type
   IMPLEMENTS = get_name
   IMPLEMENTS = evaluate_long
   IMPLEMENTS = evaluate_double
   IMPLEMENTS = evaluate_string
   IMPLEMENTS = print
   IMPLEMENTS = add_dependency
   MEMBERS    = char *name
   MEMBERS    = long start
   MEMBERS    = size_t length
   END_CLASS_DEF

 */
/* START_CLASS_IMP */

/*

Don't edit anything between START_CLASS_IMP and END_CLASS_IMP
Instead edit values between START_CLASS_DEF and END_CLASS_DEF
or edit "expression.class" and rerun ./make_class.pl

*/

typedef const char* string; /* to keep make_class.pl happy */

static void    destroy(grib_context*,grib_expression* e);
static void    print(grib_context*, grib_expression*, grib_handle*, FILE*);
static void    add_dependency(grib_expression* e, grib_accessor* observer);
static string  get_name(grib_expression* e);
static int     native_type(grib_expression*,grib_handle*);
static int     evaluate_long(grib_expression*,grib_handle*,long*);
static int     evaluate_double(grib_expression*,grib_handle*,double*);
static string  evaluate_string(grib_expression*,grib_handle*,char*,size_t*,int*);

typedef struct grib_expression_accessor{
  grib_expression base;
    /* Members defined in accessor */
    char *name;
    long start;
    size_t length;
} grib_expression_accessor;


static grib_expression_class _grib_expression_class_accessor = {
    0,                      /* super */
    "accessor",                      /* name  */
    sizeof(grib_expression_accessor),/* size of instance */
    0,                           /* inited */
    0,                       /* constructor */
    &destroy,                    /* destructor */
    &print,
    &add_dependency,
    &native_type,
    &get_name,
    &evaluate_long,
    &evaluate_double,
    &evaluate_string,
};

grib_expression_class* grib_expression_class_accessor = &_grib_expression_class_accessor;

/* END_CLASS_IMP */

static const char* get_name(grib_expression* g)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    return e->name;
}

static int evaluate_long(grib_expression* g, grib_handle* h, long* result)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    return grib_get_long_internal(h, e->name, result);
}

static int evaluate_double(grib_expression* g, grib_handle* h, double* result)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    return grib_get_double_internal(h, e->name, result);
}

static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, size_t* size, int* err)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    char mybuf[1024]            = {0,};
    long start = e->start;
    if (e->length > sizeof(mybuf)) {
        *err = GRIB_INVALID_ARGUMENT;
        return NULL;
    }

    Assert(buf);
    if ((*err = grib_get_string_internal(h, e->name, mybuf, size)) != GRIB_SUCCESS)
        return NULL;

    if (e->start < 0)
        start += *size;

    if (e->length != 0) {
        if (start >= 0)
            memcpy(buf, mybuf + start, e->length);
        buf[e->length] = 0;
    }
    else {
        memcpy(buf, mybuf, *size);
        if (*size == 1024)
            *size = *size - 1; /* ECC-336 */
        buf[*size] = 0;
    }
    return buf;
}

static void print(grib_context* c, grib_expression* g, grib_handle* hand, FILE* out)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    int err = 0;
    fprintf(out, "access('%s", e->name);
    if (hand) {
        const int ntype = native_type(g, hand);
        if (ntype == GRIB_TYPE_STRING) {
            char buf[256] = {0,};
            size_t len = sizeof(buf);
            err = grib_get_string(hand, e->name, buf, &len);
            if (!err) fprintf(out, "=%s", buf);
        }
        else if (ntype == GRIB_TYPE_LONG) {
            long lVal = 0;
            err = grib_get_long(hand, e->name, &lVal);
            if (!err) fprintf(out, "=%ld", lVal);
        }
    }
    fprintf(out, "')");
}

static void destroy(grib_context* c, grib_expression* g)
{
    grib_expression_accessor* e = (grib_expression_accessor*)g;
    grib_context_free_persistent(c, e->name);
}

static void add_dependency(grib_expression* g, grib_accessor* observer)
{
    const grib_expression_accessor* e = (grib_expression_accessor*)g;
    grib_accessor* observed = grib_find_accessor(grib_handle_of_accessor(observer), e->name);

    if (!observed) {
        /* grib_context_log(observer->context, GRIB_LOG_ERROR, */
        /* "Error in accessor_add_dependency: cannot find [%s]", e->name); */
        /* Assert(observed); */
        return;
    }

    grib_dependency_add(observer, observed);
}

grib_expression* new_accessor_expression(grib_context* c, const char* name, long start, size_t length)
{
    grib_expression_accessor* e = (grib_expression_accessor*)grib_context_malloc_clear_persistent(c, sizeof(grib_expression_accessor));
    e->base.cclass              = grib_expression_class_accessor;
    e->name                     = grib_context_strdup_persistent(c, name);
    e->start                    = start;
    e->length                   = length;
    return (grib_expression*)e;
}

static int native_type(grib_expression* g, grib_handle* h)
{
    grib_expression_accessor* e = (grib_expression_accessor*)g;
    int type = 0;
    int err;
    if ((err = grib_get_native_type(h, e->name, &type)) != GRIB_SUCCESS) {
        grib_context_log(h->context, GRIB_LOG_ERROR,
                         "Error in evaluating the type of '%s': %s", e->name, grib_get_error_message(err));
    }
    return type;
}
