#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 "shared_bb_examview.h"
#include "shared_survey_string.h"
#include "shared_strtrm.h"
#include "shared_cs_util.h"

char *buffer_big;
int max_buffer_big = 1000;
int j;
char *feedbuffer;

int body_text =0 ;
int answer_text =0;
int q_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_MULTIPLECHOICE") == 0)
    {
        ptr = initialize();
	strncpy(ptr->type, "mc", 10);
        if(!list->head)
	    list->head = ptr;
	else
	    list->current->next =ptr;
        list->current = ptr;
        j = 0;
        q_flag = 1;
    
    }
    else if(strcmp(tag, "QUESTION_TRUEFALSE") == 0)
    {
        ptr = initialize();
	strncpy(ptr->type, "mc", 10);
        if(!list->head)
	    list->head = ptr;
	else
	    list->current->next =ptr;
        list->current = ptr;
        j = 0;
        q_flag = 1;
    }
    else if(strcmp(tag, "QUESTION_FILLINBLANK") == 0)
    {
        ptr = initialize();
	strncpy(ptr->type, "bk", 10);
        if(!list->head)
	    list->head = ptr;
	else
	    list->current->next =ptr;
        list->current = ptr;
        j = 0;
        q_flag = 1;
    }
    else if(strcmp(tag, "BODY") ==0)
    {
        if(q_flag) 
	   body_text =1;
    }
    else if(strcmp(tag, "ANSWER") ==0)
    {
	if(q_flag)
	{
	  answer_text =1;
	  list->current->choices[j].id = malloc_str(atts[1]);
	}

    }
    else if(strcmp(tag, "CORRECTANSWER") ==0)
    {
       if(q_flag)
       {
	 for(i =0; i<MAX_CHOICE; i++)
	 {
	     if(list->current->choices[i].id && strcmp(list->current->choices[i].id, atts[1]) ==0)
		  list->current->choices[i].correct = 1;
	 }
       }
    }
    else if(strcmp(tag, "TITLE") ==0)
    {
         list->title = malloc_str(atts[1]);
    } 
    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);

    if(strcmp(tag, "TEXT") ==0)
    {
       if(answer_text == 1 && q_flag == 1)
       {
         list->current->choices[j].one_choice = malloc_str(buffer_big);
	 answer_text =0;
	 j++;
       }
       else if(body_text ==1 && q_flag)
       {
         list->current->quest = malloc_str(buffer_big);
	 body_text = 0;
       }
    }
    else if(strcmp(tag, "QUESTION_MULTIPLECHOICE") == 0)
        q_flag = 0;
    else if(strcmp(tag, "QUESTION_TRUEFALSE") == 0)
        q_flag = 0;
    else if(strcmp(tag, "QUESTION_FILLINBLANK") == 0)
        q_flag = 0;

    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);
}



static void *
xml_bb_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_STAT_FAILED, filename);
    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, filename);
    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_XML_PARSER, error_buf);
      free(error_buf);
    }
    XML_ParserFree(parser);
    free(buf);
    free(buffer_big);
    free(feedbuffer);
    return list;

}
      
void convert_bb_to_jbc(char *dirname, char * dat_filename, char *new_file)
{
     char *ptr;
     QUEST_LIST *list;
     char wholename[MAX_PATH +1];

     snprintf(wholename, MAX_PATH +1, "%s/%s", dirname, dat_filename);
     list = xml_bb_parser(wholename);

     ptr = strchr(dat_filename, '.');
     *ptr = '\0';
     snprintf(new_file, MAX_PATH +1, "%s.jbc", dat_filename);

     snprintf(wholename, MAX_PATH +1, "%s/%s", dirname, new_file);
     dat_to_jbc (list, wholename); /*shared_bb_examview.c */
}



