#include <stdio.h>
#include "../global.h"
#include "shared_survey_util.h"
#include "shared_survey_page.h"
#define MAX_TEMP_NAME 50

#define MAX_HTML_TAG 32 
#define MAX_HTML_TAG_WITH_ATT 3 

char *valid_html[MAX_HTML_TAG] = {
	"<b>", "</b>", "<i>", "</i>", "<table>", "</table>", "<tr>", "</tr>", 
         "<td>", "</td>", "<th>", "</th>","<h2>", "</h2>", "<H2>", "</H2>",
        "<B>", "</B>", "<I>", "</I>", "<TABLE>", "</TABLE>", "<TR>", "</TR>", 
         "<TD>", "</TD>", "<TH>", "</TH>", "<H3>", "</H3>", "<h3>", "</h3>"
       };

char *valid_html_with_att[MAX_HTML_TAG_WITH_ATT] = {"<img", "<a href=", "<table"};


char *replace_disallowed_html_code(char *str)
{
    char *ptr, *ptr2, *new_str = 0;
    int new_len, i, found, len;


    len = strlen(str);
    new_len = len *2;

    new_str = (char*) malloc(sizeof(char)* new_len +1);
    if(!new_str)
	cs_critical_error(ERR_MALLOC_FAILED, "replace_disalollowed_html_code");

    memset(new_str, '\0', new_len+1);
    for(ptr = str; *ptr; ptr++)
    {
      found = 0;
      if(*ptr == '<')
      {
	   for(i = 0; i< MAX_HTML_TAG && !found; i++)
	   {
	     ptr2 = strstr(ptr,  valid_html[i]);
	     if(ptr2 && ptr2 == ptr)
	     {
	       len = strlen(valid_html[i]) + strlen(new_str);
               if(len >= new_len)
	       {
		  new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
		  new_len = new_len *2;
	       } 
               strncat(new_str, valid_html[i], strlen(valid_html[i]));
	       ptr = ptr + strlen(valid_html[i]) -1;
	       found =1;
	     }
	   }
	   if(!found)
	   {
	       for(i = 0; i< MAX_HTML_TAG_WITH_ATT && !found; i++)
	       {
	          ptr2 = strstr(ptr,  valid_html_with_att[i]);
	          if(ptr2 && ptr2 == ptr)
		  {
		    ptr2 = strchr(ptr, '>');
		    if(ptr2)
		    {
		       len =  strlen(new_str) + ptr2 - ptr +1;
		       if(len >= new_len)
		       {
		         new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
		         new_len = new_len *2;
		       } 
		       strncat(new_str, ptr, (ptr2 - ptr ) +1);
		       ptr = ptr2;
		       found = 1;
		    } 
		  }

	       }  


	   if(!found)
	   {
	      len = strlen("&lt;") + strlen(new_str);
               if(len >= new_len)
	       {
		  new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
		  new_len = new_len *2;
	       } 
              strncat(new_str,  "&lt;", strlen("&lt;"));	 
	   }
       }
    }
    else if(*ptr == '>')
    {
       len = strlen("&gt;") + strlen(new_str);
       if(len >= new_len)
       {
        new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
        new_len = new_len *2;
       } 
      strncat(new_str,  "&gt;", strlen("&gt;"));	 
    }
    else
    {
       len = 1 + strlen(new_str);
       if(len >= new_len)
       {
	  new_str = realloc(new_str, sizeof(char)*new_len *2 +1);
	  new_len = new_len *2;
       } 
       strncat(new_str, ptr, 1);	 
    }
  }
    return new_str; 
}






static void
set_common_property(int i, char *question_type,  char *action, SURVEY_DATA *data)
{
   char name[MAX_TEMP_NAME];
   //char *str;

   snprintf(name, MAX_TEMP_NAME, "item.%d.type", i);
   cs_set_value(name, question_type);

   snprintf(name, MAX_TEMP_NAME, "item.%d.modify_program", i);
   cs_set_value(name, action);
   
   snprintf(name, MAX_TEMP_NAME, "item.%d.id", i);
   cs_set_int_value(name, data->id);
   
   snprintf(name, MAX_TEMP_NAME, "item.%d.content", i);
   // str = replace_disallowed_html_code(data->caption);
   cs_set_value(name, data->caption);
   //free(str);
}






static void 
set_title_data(SURVEY_DATA *data,  int i)
{
   char name[MAX_TEMP_NAME];

   set_common_property(i, HEADING_TYPE, "survey_title_modify_form", data);
   snprintf(name, MAX_TEMP_NAME, "item.%d.chosen_bgcolor", i);
   cs_set_value(name, data->steps_bgcolor);
   cs_set_value("bgcolor", data->steps_bgcolor);

}






static void
set_custom_data(SURVEY_DATA *data,  int i)
{
   set_common_property(i, CUSTOM_TYPE, "survey_custom_modify_form", data);
 }





static void
set_likert_data(SURVEY_DATA *data,  int i, int order)
{
   char name[MAX_TEMP_NAME];

   snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
   cs_set_int_value(name, order);
   
   set_common_property(i, LIKERT_TYPE, "survey_likert_modify_form", data);
   
   snprintf(name, MAX_TEMP_NAME, "item.%d.left_value", i);
   cs_set_value(name, data->left_cols);
   
   snprintf(name, MAX_TEMP_NAME, "item.%d.right_value", i);
   cs_set_value(name, data->right_rows);
   
   snprintf(name, MAX_TEMP_NAME, "item.%d.steps", i);
   cs_set_int_value(name, atoi(data->steps_bgcolor));
}







static void 
set_memo_data(SURVEY_DATA *data,  int i, int order)
{
   char name[MAX_TEMP_NAME];

  snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
  cs_set_int_value(name, order);
   set_common_property(i, MEMO_TYPE, "survey_memo_modify_form", data);
   snprintf(name, MAX_TEMP_NAME, "item.%d.cols", i);
   cs_set_value(name, data->left_cols);
   snprintf(name, MAX_TEMP_NAME, "item.%d.rows", i);
   cs_set_value(name, data->right_rows);
}




static void
set_choice_data(SURVEY_DATA *data,  int i, int order)
{
   char name[MAX_TEMP_NAME];
   int ele_count = 0;
   char *str;

    snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
    cs_set_int_value(name, order);

   set_common_property(i, MC_TYPE, "survey_choice_modify_form", data);
   snprintf(name, MAX_TEMP_NAME, "item.%d.multi", i);
   cs_set_value(name, data->steps_bgcolor);

   
   if(data->head)
   {
      data->current = data->head;
      while(data->current)
      {
        snprintf(name, MAX_TEMP_NAME, "item.%d.choice.%d", i, ele_count);
        str = replace_disallowed_html_code(data->current->choice);
        cs_set_value(name, str);
	free(str);
	data->current = data->current->next;
	ele_count++;
      }
   }
  if(ele_count)
  {
      snprintf(name, MAX_TEMP_NAME, "item.%d.steps", i);
      cs_set_int_value(name, ele_count +1);
  }	  
}





static void 
set_text_data(SURVEY_DATA *data,  int i, int order)
{
   char name[MAX_TEMP_NAME];
   set_common_property(i, SHORT_ANSWER_TYPE, "survey_text_modify_form", data);
   snprintf(name, MAX_TEMP_NAME, "item.%d.order", i);
   cs_set_int_value(name, order);

}






void
set_survey_content(SURVEY_LIST *list )
{
      int i =0;
      SURVEY_DATA *ptr;
      int order = 1;
       if(list)
       {
           list->current = list->head;
           while(list->current)
           {
             ptr = list->current;
            if(strcmp(ptr->tag_name, "title") ==0)
             set_title_data(ptr,  i); 
            else if(is_likert_tag(ptr->tag_name))    /* shared_survey_util.c */
	    {
	      set_likert_data(ptr,  i, order);
	      order++;
	    }
	    else if(!strcmp(ptr->tag_name, "choice"))
	    {
	      set_choice_data(ptr,  i, order);
	      order++;
	    }
	    else if(!strcmp(ptr->tag_name, "text"))
	    {
	      set_text_data(ptr,  i, order);
	      order++;
	    }
	    else if(!strcmp(ptr->tag_name, "memo"))
	    {
	       set_memo_data(ptr,  i, order);
	       order++;
	    }
	    else
	       set_custom_data(ptr,  i);
	       
	    i++;
	    list->current = list->current->next;
	   }
       }
}


