#include <stdlib.h>
#include <stdio.h>
#include <string.h>


#include "../global.h"
#include "../survey.h"


#include "shared_cs_util.h"
#include "shared_survey_string.h"
#include "shared_survey_submit.h"
#include "shared_survey_util.h"


static void
write_result( FILE *fp, NAME_VALUE_LIST *list)
{

    NAME_VALUE *ptr;
    for(ptr = list->head; ptr; ptr = ptr->next)
    {
       fprintf(fp, "      <result name=\"%d\" value=\"%s\" />\n",ptr->id, ptr->value);
    }
}


void write_result_file(FILE *fp, const char *time_string,
      const char *course_string,  NAME_VALUE_LIST *list)
{
   char timestring[MAX_TIMESTRING + 1];
   time_t now = 1000;   
       strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
		                                          localtime (&now));

   if(fseek(fp, -strlen(SURVEY_RESULT_END_TAG), SEEK_END))
        cs_critical_error( ERR_FSEEK_FAILED, "");
   fprintf(fp,"   <record time=\"%s\">\n", timestring);
   fprintf(fp,"      <course_list>%s</course_list>\n", course_string);
   write_result( fp, list);
   fprintf(fp, "   </record>\n");
   fprintf(fp, SURVEY_RESULT_END_TAG);
}


void
free_name_value_list(NAME_VALUE_LIST *list)
{
    NAME_VALUE *ptr;
    list->current = list->head;
    while(list->current)
    {
       ptr = list->current;
       if(list->current->value)
	   free(list->current->value);
       list->current = list->current->next;
       free(ptr);
    }
    free(list);
}
 
static void  get_multi_select_value(int id, char *temp)
{
   HDF *obj;
   char *str = 0;
   char name[MAX_PATH];

   snprintf(name, MAX_PATH, "Query.%d.0", id);
   str = hdf_get_value(global_cgi->hdf,name, 0);
   if(str)
   {
      snprintf(name, MAX_PATH, "Query.%d", id);
      obj = hdf_get_child(global_cgi->hdf, name);
      while(obj)
      {
	  str = obj->value;
	  if(str && strlen(str))
	  {
	    strcat(temp, str);
	    strcat(temp, "|");
	  }
	  obj = obj->next;
      }
   }
   else
   {
      snprintf(name, MAX_PATH, "Query.%d", id);
      str = hdf_get_value(global_cgi->hdf,name, 0);
      if(str && strlen(str))
      {
	  strncpy(temp, str, MAX_PATH +1);
	  strcat(temp, "|");
      }
      else
	  temp[0] = '\0';
   }
    


}



NAME_VALUE_LIST *
build_name_value_list(SURVEY_LIST *survey_list)
{
#define MAX_TMP_NAME 50

  NAME_VALUE_LIST *list;
  char *str, *str2;
   SURVEY_DATA *ptr;
   NAME_VALUE *name_ptr;
   char name[MAX_TMP_NAME];
   char temp[MAX_PATH +1];


    list = (NAME_VALUE_LIST *)malloc(sizeof(NAME_VALUE_LIST));
    list->current = 0;
    list->head = 0;

   for(ptr = survey_list->head; ptr; ptr = ptr->next)
   {
          if(strcmp(ptr->tag_name, "memo") ==0 ||
             strcmp(ptr->tag_name, "text") == 0 ||
             is_likert_tag(ptr->tag_name)       ||
             strcmp(ptr->tag_name, "choice") == 0)
          {
              name_ptr = (NAME_VALUE*)malloc(sizeof(NAME_VALUE));
              name_ptr->next =0;
	      name_ptr->id = ptr->id;

	      if(strcmp(ptr->tag_name, "choice") ==0 && ptr->steps_bgcolor && strcmp(ptr->steps_bgcolor, "yes") ==0)
              {
                  temp[0] = '\0';
		  get_multi_select_value(ptr->id, temp);
		  if(strlen(temp))
	              name_ptr->value = malloc_str(temp);
		  else
	             name_ptr->value = malloc_str("-1|");
	      }
	      else
	      {
	        snprintf(name, MAX_TMP_NAME, "Query.%d", ptr->id);
	        str = hdf_get_value(global_cgi->hdf, name, 0);
	        if(str && strlen(str))
	        {
	           str2 = replace_sp(str);
	           name_ptr->value = malloc_str(str2);
                   free(str2);
	        }
	       else
	         name_ptr->value = malloc_str("-1");
	      }
	     if(!list->head)
	             list->head = name_ptr;
	     else
	          list->current->next = name_ptr;
	     list->current = name_ptr;

            }

  }
     return list;

#undef MAX_TMP_NAME
}

