/*
 * detselect.h
 *
 * Copyright (C) 2006  Joshua Hoblitt
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef DETSELECT_H
#define DETSELECT_H 1

#include "pxtools.h"

typedef enum {
    DETSELECT_MODE_NONE      = 0x0,
    DETSELECT_MODE_SEARCH,
    DETSELECT_MODE_SELECT,
} detselectMode;

pxConfig *detselectConfig(pxConfig *config, int argc, char **argv);

#define addWhereStr(name) \
{ \
    psString str = NULL; \
    bool status = false; \
    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
        if (!psMetadataAddStr(config->where, PS_LIST_TAIL, #name, 0, "==", str)) {\
            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
    } \
}

#define addWhereStrAlias(name,realname) \
{ \
    psString str = NULL; \
    bool status = false; \
    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
        if (!psMetadataAddStr(config->where, PS_LIST_TAIL, #realname, 0, "==", str)) {\
            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
    } \
}

#define addWhereS32(name) \
{ \
    psS32 s32 = 0; \
    bool status = false; \
    if ((s32= psMetadataLookupS32(&status, config->args, "-" #name))) { \
        if (!psMetadataAddS32(config->where, PS_LIST_TAIL, #name, 0, "==", s32)) { \
            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
    } \
}

#define addWhereF32(name) \
{ \
    psF32 var = 0; \
    bool status = false; \
    if ((var = psMetadataLookupF32(&status, config->args, "-" #name))) { \
        if (!isnan(var)) { \
            if (!psMetadataAddF32(config->where, PS_LIST_TAIL, #name, 0, "==", var)) { \
                psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
                psFree(config); \
                return NULL; \
            } \
        } \
    } \
}

#define addWhereF64(name) \
{ \
    psF64 var = 0; \
    bool status = false; \
    if ((var = psMetadataLookupF64(&status, config->args, "-" #name))) { \
        if (!isnan(var))  { \
            if (!psMetadataAddF64(config->where, PS_LIST_TAIL, #name, 0, "==", var)) { \
                psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
                psFree(config); \
                return NULL; \
            } \
        } \
    } \
}

#define addWhereTimeStr(name) \
{ \
    psString str = NULL; \
    bool status = false; \
    if ((str = psMetadataLookupStr(&status, config->args, "-" #name))) { \
        psTime *time = psTimeFromISO(str, PS_TIME_UTC); \
        if (!time) { \
            psError(PS_ERR_UNKNOWN, false, "failed to convert " #name " into a psTime object"); \
            psFree(config); \
            return NULL; \
        } \
        psMetadataItem *item = psMetadataLookup(config->args, "-" #name); \
        if (item) { \
            str = item->comment; \
        } else { \
            str = NULL; \
        } \
        if (!psMetadataAddTime(config->where, PS_LIST_TAIL, #name, 0, str, time)) {\
            psError(PS_ERR_UNKNOWN, false, "failed to add item " #name); \
            psFree(config); \
            return NULL; \
        } \
        psFree(time); \
    } \
}

#endif // DETSELECT_H
