// =============================================================== //
//                                                                 //
//   File      : adChangeKey.cxx                                   //
//   Purpose   : Changekey management                              //
//                                                                 //
//   Coded by Elmar Pruesse and Ralf Westram in May 2009           //
//   Institute of Microbiology (Technical University Munich)       //
//   http://www.arb-home.de/                                       //
//                                                                 //
// =============================================================== //

#include <arbdbt.h>
#include <arb_global_defs.h>
#include "gb_local.h"

GBDATA *GBT_get_changekey(GBDATA *gb_main, const char *key, const char *change_key_path) {
    // get the container of an item key description

    // @@@ check if search for CHANGEKEY_NAME should be case-sensitive!
    GBDATA *gb_key      = NULp;
    GBDATA *gb_key_data = GB_search(gb_main, change_key_path,
                                    GB_CREATE_CONTAINER);

    if (gb_key_data) {
        GBDATA *gb_key_name = GB_find_string(gb_key_data, CHANGEKEY_NAME, key, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
        if (gb_key_name) {
            gb_key = GB_get_father(gb_key_name);
        }
    }
    return gb_key;
}

GB_TYPES GBT_get_type_of_changekey(GBDATA *gb_main, const char *field_name, const char *change_key_path) {
    // get the type of an item key
    GB_TYPES  type = GB_NONE;
    GBDATA   *gbd  = GBT_get_changekey(gb_main, field_name, change_key_path);

    if (gbd) {
        long *typePtr     = GBT_read_int(gbd, CHANGEKEY_TYPE);
        if (typePtr) {
            type = (GB_TYPES)*typePtr;
        }
    }

    return type;
}

static GB_ERROR gbt_set_type_of_changekey(GBDATA *gb_main, const char *field_name, GB_TYPES type, const char *change_key_path) {
    GB_ERROR  error = NULp;
    GBDATA   *gbd   = GBT_get_changekey(gb_main, field_name, change_key_path);

    if (!gbd) {
        error = GBS_global_string("Can't set type of nonexistent changekey \"%s\"", field_name);
    }
    else {
        error = GBT_write_int(gbd, CHANGEKEY_TYPE, type);
    }
    return error;
}

GBDATA *GBT_searchOrCreate_itemfield_according_to_changekey(GBDATA *gb_item, const char *field_name, const char *change_key_path) {
    /*! search or create an item entry.
     * If the entry exists, the result is identical to GB_search(gb_item, field_name, GB_FIND).
     * If the entry does not exist, an entry with the type stored in the changekey-table will be created.
     * @return created itemfield or NULp in case of error (which is exported in that case)
     */

    gb_assert(!GB_have_error());
    GBDATA *gb_entry = GB_search(gb_item, field_name, GB_FIND);
    if (!gb_entry) {
        GB_clear_error();

        GB_TYPES type = GBT_get_type_of_changekey(GB_get_root(gb_item), field_name, change_key_path);
        if (type == GB_NONE) {
            GB_export_errorf("Cannot create field '%s' (no type information available)", field_name);
        }
        else {
            gb_entry = GB_search(gb_item, field_name, type);
        }
    }
    gb_assert(gb_entry || GB_have_error());
    return gb_entry;
}


GB_ERROR GBT_add_new_changekey_to_keypath(GBDATA *gb_main, const char *name, GB_TYPES type, const char *keypath) {
    /*! Add a new or check an existing changekey for items using 'keypath'
     * @param gb_main database
     * @param name name of changekey. may be hierarchical (e.g. 'ali_16s/data')
     * @param type field type of the key (yields error on mismatch!)
     * @param keypath location in database where changekeys are stored.
     * @return error
     */
    GB_ERROR    error  = NULp;
    GBDATA     *gb_key = GBT_get_changekey(gb_main, name, keypath);
    const char *c      = GB_first_non_key_char(name);

    if (c) {
        char *new_name = ARB_strdup(name);

        *(char*)GB_first_non_key_char(new_name) = 0;

        if   (*c == '/') error = GBT_add_new_changekey_to_keypath(gb_main, new_name, GB_DB, keypath);
        else             error = GBS_global_string("Cannot add '%s' to your key list (illegal character '%c')", name, *c);

        free(new_name);
    }

    if (!error) {
        if (!gb_key) {          // create new key
            GBDATA *gb_key_data = GB_search(gb_main, keypath, GB_CREATE_CONTAINER);
            gb_key              = gb_key_data ? GB_create_container(gb_key_data, CHANGEKEY) : NULp;

            if (!gb_key) error = GB_await_error();
            else {
                error             = GBT_write_string(gb_key, CHANGEKEY_NAME, name);
                if (!error) error = GBT_write_int(gb_key, CHANGEKEY_TYPE, type);
            }
        }
        else {                  // check type of existing key
            long *elem_type = GBT_read_int(gb_key, CHANGEKEY_TYPE);

            if (!elem_type)              error = GB_await_error();
            else if (*elem_type != type) error = GBS_global_string("Key '%s' exists, but has different type", name);
        }
    }

    gb_assert(gb_key || error);

    return error;
}

GB_ERROR GBT_add_new_species_changekey(GBDATA *gb_main, const char *name, GB_TYPES type) {
    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH);
}

GB_ERROR GBT_add_new_gene_changekey(GBDATA *gb_main, const char *name, GB_TYPES type) {
    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_GENES);
}

GB_ERROR GBT_add_new_experiment_changekey(GBDATA *gb_main, const char *name, GB_TYPES type) {
    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_EXPERIMENTS);
}

GB_ERROR GBT_add_new_changekey(GBDATA *gb_main, const char *name, int type) {
    // goes to header: __ATTR__DEPRECATED_TODO("obsolete. use GBT_add_new_species_changekey() instead")
    //
    // this method is required for sina 1.3 binary backward compatibility
    // (needed as long as we support ubuntu 10.04)
    //
    // Note: was broken by [19140].
    return GBT_add_new_species_changekey(gb_main, name, GB_TYPES(type));
}

GB_ERROR GBT_add_alignment_changekeys(GBDATA *gb_main, const char *ali) {
    /*! add changekeys for alignment 'ali'
     */
    char     *dataField = GBS_global_string_copy("%s/data", ali);
    GB_ERROR  error     = GBT_add_new_species_changekey(gb_main, dataField, GB_STRING);
    free(dataField);
    return error;
}

static GB_ERROR write_int_converted(GBDATA *gbfield, const char *data, bool trimmed, size_t *rounded) {
    // 'rounded' is incremented each time a float value was
    // converted to integer (even if float value can convert lossless, e.g. "4.0").

    char          *end   = NULp;
    unsigned long  i     = strtoul(data, &end, 10);
    GB_ERROR       error = NULp;

    if (end == data || end[0] != 0) {
        if (trimmed) {
            // fallback: convert to double and round

            double d = strtod(data, &end);
            if (end == data || end[0] != 0) {
                error = GBS_global_string("cannot convert '%s' to rounded numeric value", data);
            }
            else {
                (*rounded)++;
                i                = d>0 ? (int)(d+0.5) : (int)(d-0.5);
                error            = GB_write_int(gbfield, i);
                if (error) error = GBS_global_string("write error (%s)", error);
            }
        }
        else {
            char *trimmed_data = GBS_trim(data);
            error              = write_int_converted(gbfield, trimmed_data, true, rounded);
            free(trimmed_data);
        }
    }
    else {
        error = GB_write_int(gbfield, i);
        if (error) error = GBS_global_string("write error (%s)", error);
    }

    return error;
}

static GB_ERROR write_float_converted(GBDATA *gbfield, const char *data, bool trimmed) {
    char     *end   = NULp;
    float     f     = strtof(data, &end);
    GB_ERROR  error = NULp;

    if (end == data || end[0] != 0) {
        if (trimmed) {
            error = GBS_global_string("cannot convert '%s' to numeric value", data);
        }
        else {
            char *trimmed_data = GBS_trim(data);
            error              = write_float_converted(gbfield, trimmed_data, true);
            free(trimmed_data);
        }
    }
    else {
        error = GB_write_float(gbfield, f);
        if (error) error = GBS_global_string("write error (%s)", error);
    }

    return error;
}

GB_ERROR GBT_write_int_converted(GBDATA *gb_int_writable, const char *data, size_t *rounded) {
    // Note: has functional similarities to GB_write_autoconv_string
    return write_int_converted(gb_int_writable, data, false, rounded);
}
GB_ERROR GBT_write_float_converted(GBDATA *gb_float_writable, const char *data) {
    // Note: has functional similarities to GB_write_autoconv_string
    return write_float_converted(gb_float_writable, data, false);
}

GB_ERROR GBT_convert_changekey(GBDATA *gb_main, const char *name, GB_TYPES target_type) {
    GB_ERROR error        = GB_push_transaction(gb_main);
    bool     need_convert = true;

    if (!error) {
        GBDATA *gbkey = GBT_get_changekey(gb_main, name, CHANGE_KEY_PATH);
        if (gbkey) {
            GB_TYPES source_type = (GB_TYPES)*GBT_read_int(gbkey, CHANGEKEY_TYPE);
            if (source_type == target_type) need_convert = false;
        }
        else {
            if (!name[0] || strcmp(name, NO_FIELD_SELECTED) == 0) {
                error = "Please select field to convert";
            }
            else {
                error = GBS_global_string("Unknown changekey '%s'", name);
            }
        }
    }

    if (!error && need_convert) {
        GBDATA *gbspec  = GBT_first_species(gb_main);
        size_t  rounded = 0;

        for (; gbspec; gbspec = GBT_next_species(gbspec)) {
            GBDATA *gbfield = GB_entry(gbspec, name);

            // If entry does not exist, no need to convert (sparse population is valid => 'NULp' value)
            if (gbfield) {
                char *data = GB_read_as_string(gbfield);
                if (!data) {
                    error = GBS_global_string("read error (%s)", GB_await_error());
                }
                else {
                    error = GB_delete(gbfield);
                    if (!error) {
                        gbfield = GB_create(gbspec, name, target_type);
                        if (!gbfield) {
                            error = GBS_global_string("create error (%s)", GB_await_error());
                        }
                        else {
                            switch (target_type) {
                                case GB_INT:
                                    error = GBT_write_int_converted(gbfield, data, &rounded);
                                    break;

                                case GB_FLOAT:
                                    error = GBT_write_float_converted(gbfield, data);
                                    break;

                                case GB_STRING:
                                    error = GB_write_string(gbfield, data);
                                    if (error) error = GBS_global_string("write error (%s)", error);
                                    break;

                                default:
                                    error = "Conversion is not possible";
                                    break;
                            }
                        }
                    }
                    free(data);
                }
            }
            if (error) break;
        }

        if (error && gbspec) {
            const char *spname = GBT_get_name_or_description(gbspec);
            error              = GBS_global_string("%s for species '%s'", error, spname);
        }

        if (!error) error = gbt_set_type_of_changekey(gb_main, name, target_type, CHANGE_KEY_PATH);
        if (!error && rounded>0) {
            GB_warningf("%zi values were rounded (loss of precision)", rounded);
        }
    }

    if (error) error  = GBS_global_string("GBT_convert: %s", error);

    return GB_end_transaction(gb_main, error);
}

