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

#include "../global.h"
#include "shared_util.h"
#include "shared_survey_table.h"
#include "shared_cs_util.h"

static int get_number_choices(ELE_CHOICE *head);

void
set_survey_table_basic_data(RECORD_LIST *list, int total, int number_takers)
{
  char temp_value[MAX_PATH +1];
  float rate;

    cs_set_int_value("survey.num_responses",  total);
    if(number_takers ==  -1)
    {
	cs_set_int_value("survey.num_expected_takers", list->expect_takers);
	rate = (float)total / (float)list->expect_takers;
    }
    else if(number_takers >0)
    {
      cs_set_int_value("survey.num_expected_takers", number_takers);
	rate = (float)total / (float)number_takers;
    }
    snprintf(temp_value, MAX_PATH +1, "%.1f%%", rate * 100);
    cs_set_value("survey.rate", temp_value);
}


void
set_survey_table_heading(RECORD *one_record, int number_takers)
{
    RESULT *ptr;
    int i =0;

    ptr = one_record->head;
    if(number_takers != -2)
      cs_set_int_value("display_course_list", 1);
    while(ptr)
    {
      ptr = ptr->next;
      i++;
    }
    cs_set_int_value("number_question", i);
}

void
set_survey_table_one_record(RECORD *one_record, int number_takers, int num)
{
   #define MAX_TMP_NAME 50
   char name[MAX_TMP_NAME];
   int i =0;

    RESULT *ptr;
    
    ptr = one_record->head;

    if(number_takers != -2)
    {
       snprintf(name, MAX_TMP_NAME, "record.%d.course_list", num);
       cs_set_value( name, one_record->course_list);
    }

    while(ptr)
    {
       snprintf(name, MAX_TMP_NAME, "record.%d.value.%d", num, i);
       cs_set_value(name,  ptr->value);
       ptr=ptr->next;
       i++;
    }

#undef MAX_TMP_NAME

}






int get_value_count(RECORD *one_record)
{
     int i =1;
     RESULT *ptr;

     for( ptr = one_record->head; ptr; ptr = ptr->next)
	     i++;
     return i;
}

char *replacequots(const char *value)
{
     char *buf;
     int i, j, len;

     buf = (char *)malloc(sizeof(char)*(strlen(value)*2 +1));

     len = strlen(value);
     for(i =0; i < len; i++)
     {
        if(value[i] == '\"')
	{
	   buf[j] = '\"';
	   buf[j+1] = '\"';
	   j+=2;  
	}
	else
	{
	 buf[j] = value[i];
          j++;
	}
     }
     buf[j] = '\0';
     return buf;
}


int  is_choice_select_question(char *name, SURVEY_LIST *list, int *choice)
{
     SURVEY_DATA *ptr;

     int id = atoi(name);

     for(ptr = list->head; ptr; ptr = ptr->next)
     {
	  if( ptr->id == id && strcmp(ptr->tag_name, "choice") == 0 && strcmp(ptr->steps_bgcolor, "yes") ==0)
	  {
	        *choice = get_number_choices(ptr->head);
		return 1;
	  }
     }
     return 0;
}

static int  get_this_value(char *value,  int j)
{
   char *ptr =0; 
   char str[10];

   snprintf(str, 10, "%d", j);
   ptr = strstr(value, str);
   if(ptr)
     return j;
   else
    return -1;
}

     
void
send_one_record(RECORD *one_record, int number_takers, SURVEY_LIST *list)
{
     RESULT *ptr;
     int count, j;
     int number_choice =0;


     count = get_value_count(one_record);

    if(number_takers != -2)
       printf("\"%s\",", one_record->course_list);
    
    ptr = one_record->head;
    while(ptr)
    {
       count--;
       
       if(is_choice_select_question(ptr->name, list, &number_choice))
       {
	   for(j = 1; j<= number_choice; j++)
	   {
		 if(count == 1 && j == number_choice)
                    printf("\"%d\"\n", get_this_value(ptr->value, j));
		 else
                    printf("\"%d\",", get_this_value(ptr->value, j));
	   }

       }
       else
       {
        if(count ==1)
         printf("\"%s\"\n", ptr->value);
        else
         printf("\"%s\",", ptr->value);
       }
       ptr=ptr->next;
    }
}

static int get_number_survey_questions(SURVEY_LIST *list)
{
    SURVEY_DATA *ptr;
    int count =0;

    for(ptr = list->head; ptr; ptr = ptr->next)
    {
	if(strcmp(ptr->tag_name, "title") != 0 && strcmp(ptr->tag_name, "custom") != 0)
		count++;
    }
    return count;
    
}
static int get_number_choices(ELE_CHOICE *head)
{
    ELE_CHOICE *ptr;
    int count = 0;
    for(ptr = head; ptr; ptr = ptr->next)
      count++;

    return count;
}


void  send_csv_heading(SURVEY_LIST *survey_list)
{
    SURVEY_DATA *ptr;
    int i = 1, j = 1, number_choice = 0;
    int number_questions = get_number_survey_questions(survey_list);

    printf("\" \",");
    for(ptr = survey_list->head; ptr; ptr = ptr->next)
    {
       if(strcmp(ptr->tag_name, "title") != 0 && strcmp(ptr->tag_name, "custom") != 0)
       {
          if(strcmp(ptr->tag_name, "choice") ==0 && strcmp(ptr->steps_bgcolor, "yes") == 0)
	  { 
	         number_choice = get_number_choices(ptr->head);
                 for(j = 1; j<= number_choice; j++)
                 {
		   if(j == number_choice && i == number_questions)
	              printf("\"%d.%d\"\n", i, j);
		   else
	              printf("\"%d.%d\",", i, j);

		 }
          }
	  else
	  {
		if(i == number_questions)
	           printf("\"%d\"\n", i);
                else
	          printf("\"%d\",", i);
	  }
	  i++;
       }
    }
}	

void
send_survey_results_csv(RECORD_LIST *list, int number_takers, const char *course, SURVEY_LIST *survey_list)
{
     

 char modified_course[MAX_PATH + 1];
 char *cptr;
 
  /* course contains a slash / separating the directories
  ** first, we convert the slash to a dot . 
  */
  course_slash_to_dot(course, modified_course);   /* shared_util.c */
  
  /* MS IE does a weird thing with an attachment that has two dots.
  ** It appears to put a [1] before the first dot.  For example, if
  ** told that the attachment is called "c102.ac10101.txt", IE will
  ** want to save the file as "c102[1].ac10101.txt".  To circumvent this,
  ** we'll replace the first dot with an underscore
  */
  cptr = strchr(modified_course, '.');
  if(cptr)
     *cptr = '_';

   printf("Content-type:application/octet-stream\n");
   printf ("Content-disposition:attachment; filename=\"%s_survey.csv\"\n\n", modified_course);
    
   send_csv_heading(survey_list); 
   list->current = list->head;
   while(list->current)
    {
          send_one_record(list->current, number_takers, survey_list);
          list->current = list->current->next;
     }
}
      
    
	
