#include <stdio.h>
#include <string.h>
#include <uf.h>
#include <uf_obj.h>
#include <uf_modl.h>
#include <uf_object_types.h>
#include <uf_ui.h>
#include <uf_assem.h>

#include <stdarg.h>

static void ECHO(char *format, ...)
{
    char msg[UF_UI_MAX_STRING_LEN+1];
    va_list args;
    va_start(args, format);
    vsprintf(msg, format, args);
    va_end(args);
    UF_UI_open_listing_window();
    UF_UI_write_listing_window(msg);
    UF_print_syslog(msg, FALSE);
}

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
    if (irc)
    {
        char err[133];

        UF_get_fail_message(irc, err);
        ECHO("*** ERROR code %d at line %d in %s:\n",
            irc, line, file);
        ECHO("+++ %s\n", err);
        ECHO("%s;\n", call);
    }

    return(irc);
}

static logical prompt_for_text(char *prompt, char *text)
{
    int
        n,
        resp;

    resp = uc1600(prompt, text, &n);
    if (resp == 3 || resp == 5) return TRUE;
    return FALSE;
}


static void do_it(void)
{
    logical
        nameProvided = FALSE;

    char 
        text[MAX_ENTITY_NAME_SIZE+1] = { "" };
    char
        msg[257] = { "" };

    while((nameProvided = prompt_for_text("Name to search for:", text)) != FALSE)
    {
        tag_t
            foundThis = NULL_TAG;
        sprintf_s(msg, sizeof(msg), "\nSearching for: %s\n", text);
        ECHO(msg);

        UF_CALL(UF_OBJ_cycle_by_name(text,&foundThis));

        bool foundFeature = false;
        while( foundThis != NULL_TAG )
        {
            int
                type = 0, subtype = 0;
            
            UF_CALL(UF_OBJ_ask_type_and_subtype(foundThis, &type, &subtype));
            sprintf_s(msg, sizeof(msg), "Tag %d  Type: %d  Subtype %d\n",
                      foundThis, type, subtype);
            ECHO(msg);
            UF_CALL(UF_OBJ_cycle_by_name(text,&foundThis));
            if (type == UF_feature_type)
            {
                foundFeature = true;
            }
        }
        if (!foundFeature)
        {
            tag_t  part_tag  = UF_ASSEM_ask_work_part();
            UF_CALL(UF_OBJ_cycle_by_name_and_type(part_tag, text,UF_feature_type, false, &foundThis));

            while( foundThis != NULL_TAG )
            {
                int
                    type = 0, subtype = 0;

                UF_CALL(UF_OBJ_ask_type_and_subtype(foundThis, &type, &subtype));
                sprintf_s(msg, sizeof(msg), "Tag %d  Type: %d  Subtype %d\n",
                    foundThis, type, subtype);
                ECHO(msg);
                UF_CALL(UF_OBJ_cycle_by_name(text,&foundThis));
                if (type == UF_feature_type)
                {
                    foundFeature = true;
                }
            }
        }
    }



}

/*ARGSUSED*/
void ufusr(char *param, int *retcode, int paramLen)
{
    if (UF_CALL(UF_initialize())) return;
    do_it();
    UF_terminate();
}

int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}
