#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>

#include "xmlparse.h"
#include "global.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_bb_examview.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_strtrm.h"

char *buffer_big;
int max_buffer_big = 1000;
int j;
char *feedbuffer;
char letter = 'a';
int text_flag = 0;


static void startElement(void *userData, const char *name, const char **atts)
{
    QUEST_LIST *list = userData;
    char *tag = malloc_str(name);
    int i;
   XML_QUESTION *ptr;
    if(strcmp(tag, "question") == 0)
    {
        ptr = initialize();
	text_flag =1;
        for(i =0; atts[i]; i=i+2)
        {
	   if(strcmp(atts[i], "type") ==0)
	     strncpy(ptr->type, atts[i+1], 10);
        }
        if(!list->head)
	    list->head = ptr;
	else
	    list->current->next =ptr;
        list->current = ptr;
        j = 0;
    }
    if(strcmp(tag, "choices") ==0)
	 letter = 'a';
	    
    free(tag);
    memset(buffer_big, 0, strlen(buffer_big));
}

static void endElement(void *userData, const char *name)
{
    QUEST_LIST *list = userData;
    char *tag = malloc_str(name);
    char choice_name[10];

    snprintf(choice_name, 10, "choice-%c", letter);
    if(strcmp(tag, "title") == 0)
    {
       strtrim(buffer_big);
       list->title = malloc_str(buffer_big);
    }
    if(strcmp(tag, "answer") ==0)
    {
       if(strcmp(list->current->type, "tf") ==0)
       {
          list->current->choices[0].one_choice = malloc_str("True");
          list->current->choices[1].one_choice = malloc_str("False");
	  if(strcmp(buffer_big, "T") ==0)
	      list->current->choices[0].correct = 1;
	  else
	      list->current->choices[1].correct = 1;
       }
       else if(strcmp(list->current->type, "mc") ==0)
       {
	  if(strcmp(buffer_big, "A") ==0 )
	      list->current->choices[0].correct = 1;
	  else if(strcmp(buffer_big, "B") ==0)
	      list->current->choices[1].correct = 1;
	  else if(strcmp(buffer_big, "C") == 0)
	      list->current->choices[2].correct = 1;
	  else
	      list->current->choices[3].correct = 1;
       }
    }              
    if(strcmp(tag, "text") ==0 && text_flag)
	   list->current->quest = malloc_str(buffer_big);
    if(strcmp(tag,choice_name) ==0)
    {
	  list->current->choices[j].one_choice = malloc_str(buffer_big);
	  letter++;
	  j++;
    }

    memset(buffer_big, 0, strlen(buffer_big));
    free(tag);
}

static void startCharacter(void *userData, const char *text, int len)
{

	if((strlen(text) + strlen(buffer_big))>= max_buffer_big)
        {
              buffer_big = realloc(buffer_big, sizeof(char)*(max_buffer_big *2 +1));
	      max_buffer_big = max_buffer_big *2;
	}
	strncat(buffer_big, text, len);
}



void *
xml_data_parser(char *filename)
{
   char *buf, *error_buf;
   QUEST_LIST *list;
   FILE *fp;
   struct stat file_stat;
   XML_Parser parser;

   buffer_big = (char *)malloc(sizeof(char)*(max_buffer_big +1));
   feedbuffer = (char *)malloc(sizeof(char)*(max_buffer_big +1));
    /*initilze list */
    list = (QUEST_LIST *)malloc(sizeof(QUEST_LIST));
    list->head = 0;
    list->current = 0;
    list->title =0;

    if(stat(filename, &file_stat))
       cs_critical_error( ERR_FOPEN_READ_FAILED, "");
    buf = (char*)malloc(file_stat.st_size);
    if(!buf)
      cs_critical_error( ERR_MALLOC_FAILED, "");

    fp = fopen(filename, "r");
    if(!fp)
      cs_critical_error( ERR_FOPEN_READ_FAILED, "");
    if(fread(buf, file_stat.st_size, 1, fp) != 1)
       cs_critical_error(ERR_FREAD_FAILED, "");
    fclose(fp);
    parser = XML_ParserCreate(NULL);
    XML_SetUserData(parser, list);
    XML_SetElementHandler(parser, startElement, endElement);
    XML_SetCharacterDataHandler(parser, startCharacter);
    if(!XML_Parse(parser, buf, file_stat.st_size, 1))
    {
      error_buf = malloc_str(XML_ErrorString(XML_GetErrorCode(parser)));
      cs_critical_error( ERR_MALLOC_FAILED, "");
      free(error_buf);
    }
    XML_ParserFree(parser);
    free(buf);
    free(buffer_big);
    free(feedbuffer);
    return list;

}
      
int main()
{
    char xml_org_file[MAX_PATH +1];
    char xml_file[MAX_PATH +1];
    char jbc_file[MAX_PATH +1];
    QUEST_LIST *list;

    cs_cgi_init();

    get_xml_file(xml_org_file, xml_file); /* shared_bb_examview.h */
    list = xml_data_parser(xml_file);
    xml_to_jbc (list, jbc_file); /* shared_bb_examview.h */
    send_jbc_file(jbc_file, xml_org_file); /* shared_bb_examview.h */
    cs_cgi_destroy();
    return 0;
}




