#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "../global.h"
#include "../custom.h"
#include "../survey.h"

#include "shared_cs_util.h"
#include "shared_survey_util.h"
#include "shared_survey_string.h"
#include "shared_lock.h"
#include "shared_login.h"
#include "shared_file_util.h"
#include "shared_server_string.h"
#include "shared_spelling_util.h"




/** Prior to version 3.0, the word 'likert' was
*** misspelled as "lickert" in the survey XML files
*** and throughout the Manhattan code.  The next function
*** is used to maintain backward compatibility with survey
*** xml data that spelled 'likert' incorrectly 
***/

int is_likert_tag(const char *tag)
{
  return (!strcmp(tag,"lickert") ||
          !strcmp(tag,"likert") ) ;

}



SURVEY_DATA *
initialize_survey_data(SURVEY_DATA *data)
{
     data->tag_name = 0;
     data->id = 0;
     data->caption = 0;
     data->steps_bgcolor =0;
     data->left_cols = 0;
     data->right_rows = 0;
     data->head = 0;
     data->current = 0;
     data->next = 0;
     return data;
}





SURVEY_DATA *
get_clipboard_data(SURVEY_LIST *list)
{
   SURVEY_DATA *data = 0;
   ELE_CHOICE *ptr;

   if(list->head)
   {
     list->current = list->head;
     data = (SURVEY_DATA *)malloc(sizeof(SURVEY_DATA ));
     data = initialize_survey_data(data);
     if(is_likert_tag(list->current->tag_name))
     {
	data->id = list->current->id;
	data->tag_name = malloc_str(list->current->tag_name);
	data->caption = malloc_str(list->current->caption);
	data->steps_bgcolor = malloc_str(list->current->steps_bgcolor);
	data->left_cols = malloc_str(list->current->left_cols);
	data->right_rows = malloc_str(list->current->right_rows);
	return data;
          
     }
     else if(strcmp(list->current->tag_name, "choice") ==0)
     {
	data->id = list->current->id;
	data->tag_name = malloc_str(list->current->tag_name);
	data->caption = malloc_str(list->current->caption);
	data->steps_bgcolor = malloc_str(list->current->steps_bgcolor);
	if(list->current->head)
	{
	   list->current->current = list->current->head;
	   while(list->current->current)
	   {
	      ptr = (ELE_CHOICE*)malloc(sizeof(ELE_CHOICE));
	      ptr->choice = malloc_str(list->current->current->choice);
	      ptr->next =0;
	      if(!data->head)
		 data->head = ptr;
	      else
		 data->current->next = ptr;
	       data->current = ptr;
	       list->current->current=list->current->current->next;
	   }
	}
        return data; 
     }
     else if(strcmp(list->current->tag_name, "text") ==0 ||
             strcmp(list->current->tag_name, "custom") ==0)
     {
	data->id = list->current->id;
	data->tag_name = malloc_str(list->current->tag_name);
	data->caption = malloc_str(list->current->caption);
	return data;
     }
     else if(strcmp(list->current->tag_name, "memo") == 0)
     {
	data->id = list->current->id;
	data->tag_name = malloc_str(list->current->tag_name);
	data->caption = malloc_str(list->current->caption);
	data->left_cols = malloc_str(list->current->left_cols);
	data->right_rows = malloc_str(list->current->right_rows);
	return data;
     }
   }
   return data;
}
     

SURVEY_DATA *
get_one_survey_data(SURVEY_LIST *list, int count)
{
   SURVEY_DATA *data =0;
   ELE_CHOICE *ptr;

    if(list->head)
    {
       list->current = list->head;
       data = (SURVEY_DATA *)malloc(sizeof(SURVEY_DATA ));
       if(!data)
          cs_critical_error(ERR_MALLOC_FAILED, "");
          
       data = initialize_survey_data(data);

       while(list->current)
       {
	  if(list->current->id == count)
	  {
	     if(strcmp(list->current->tag_name, "title") ==0)
	     {
		data->id = list->current->id;
		data->tag_name = malloc_str(list->current->tag_name);
		data->left_cols = malloc_str(list->current->left_cols);
		data->caption = malloc_str(list->current->caption);
		data->steps_bgcolor = malloc_str(list->current->steps_bgcolor);
		return data;
	     }
	     else if(is_likert_tag(list->current->tag_name))
	     {
		data->id = list->current->id;
		data->tag_name = malloc_str(list->current->tag_name);
		data->caption = malloc_str(list->current->caption);
		data->steps_bgcolor = malloc_str(list->current->steps_bgcolor);
		data->left_cols = malloc_str(list->current->left_cols);
		data->right_rows = malloc_str(list->current->right_rows);
		return data;
	          
	     }
	     else if(strcmp(list->current->tag_name, "choice") ==0)
	     {
		data->id = list->current->id;
		data->tag_name = malloc_str(list->current->tag_name);
		data->caption = malloc_str(list->current->caption);
		data->steps_bgcolor = malloc_str(list->current->steps_bgcolor);
		if(list->current->head)
		{
		   list->current->current = list->current->head;
		   while(list->current->current)
		   {
		      ptr = (ELE_CHOICE*)malloc(sizeof(ELE_CHOICE));
		      ptr->choice = malloc_str(list->current->current->choice);
		      ptr->next =0;
		      if(!data->head)
			 data->head = ptr;
		      else
			 data->current->next = ptr;
		       data->current = ptr;
		       list->current->current=list->current->current->next;
		   }
		}
	        return data; 
	     }
	     else if(strcmp(list->current->tag_name, "text") ==0 ||
	             strcmp(list->current->tag_name, "custom") ==0)
	     {
		data->id = list->current->id;
		data->tag_name = malloc_str(list->current->tag_name);
		data->caption = malloc_str(list->current->caption);
		return data;
	     }
	     else if(strcmp(list->current->tag_name, "memo") == 0)
	     {
		data->id = list->current->id;
		data->tag_name = malloc_str(list->current->tag_name);
		data->caption = malloc_str(list->current->caption);
		data->left_cols = malloc_str(list->current->left_cols);
		data->right_rows = malloc_str(list->current->right_rows);
		return data;
	     }
	  }
	  list->current = list->current->next;
       }
    }
    return data;
}






void
free_survey_data(SURVEY_DATA *data)
{
    ELE_CHOICE *ptr;
    if(data)
    {
      if(data->tag_name)
	 free(data->tag_name);
      if(data->caption)
	 free(data->caption);
      if(data->steps_bgcolor)
	 free(data->steps_bgcolor);
      if(data->left_cols)
	 free(data->left_cols);
      if(data->right_rows)
	 free(data->right_rows);
      if(data->head)
      {
	data->current = data->head;
	while(data->current)
	{
          ptr = data->current;
          if(ptr->choice)
	     free(ptr->choice);
          data->current = data->current->next;
          free(ptr);
        }
      }
    }
    free(data);
}




   
SURVEY_INFO*
initialize_survey_info(SURVEY_INFO *survey_info)
{
      survey_info->id =0;
      survey_info->title = 0;
      survey_info->state = 0;
      survey_info->create_time = 0;
      survey_info->finish_time = 0;
      survey_info->release_time = 0;
      survey_info->close_time = 0;
      survey_info->author_id = 0;
      survey_info->author_name = 0;
      survey_info->survey_domain = 0;
      survey_info->next =0;
      return survey_info;
}

char *
get_survey_time_string()
{
    char *time_string;
    time_t now;

     now = time (NULL);
     time_string = (char *)malloc(sizeof(char)*(MAX_TIMESTRING +1));
     strftime (time_string, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
                            localtime (&now));
     return time_string;
}

void
free_survey_info(SURVEY_INFO *info)
{
    if(info->id)
	free(info->id);
    if(info->title)
	free(info->title);
    if(info->state)
	free(info->state);
    if(info->create_time)
	free(info->create_time);
    if(info->finish_time)
	free(info->finish_time);
    if(info->release_time)
	free(info->release_time);
    if(info->close_time)
	free(info->close_time);
    if(info->author_id)
	free(info->author_id);
    if(info->author_name)
	free(info->author_name);
    if(info->survey_domain)
	free(info->survey_domain);
}
void
write_one_survey_info(SURVEY_INFO *info, FILE *fp)
{
    fprintf(fp, "<%s %s=\"%s\">\n", SURVEY_TAG, SURVEY_ID_ATTR, info->id);
    fprintf(fp, "  <%s>%s%s%s</%s>\n", SURVEY_TITLE_TAG, SURVEY_CDATA_START,info->title, SURVEY_CDATA_END, SURVEY_TITLE_TAG);
    fprintf(fp, "  <%s>%s</%s>\n", SURVEY_STATE_TAG, info->state, SURVEY_STATE_TAG);
    fprintf(fp, "  <%s>%s</%s>\n", SURVEY_CREATE_TIME_TAG, info->create_time,
		    SURVEY_CREATE_TIME_TAG);
    fprintf(fp, "  <%s>%s</%s>\n",
		    SURVEY_FINISH_TIME_TAG, info->finish_time, SURVEY_FINISH_TIME_TAG);
    fprintf(fp, "  <%s>%s</%s>\n",
		    SURVEY_RELEASE_TIME_TAG, info->release_time, SURVEY_RELEASE_TIME_TAG);
    fprintf(fp, "  <%s>%s</%s>\n",
		    SURVEY_CLOSE_TIME_TAG, info->close_time, SURVEY_CLOSE_TIME_TAG);
    fprintf(fp, "  <%s %s=\"%s\">\n",
		    SURVEY_AUTHOR_TAG,SURVEY_ID_ATTR, info->author_id);
    fprintf(fp, "  <%s>%s</%s>\n",
		    SURVEY_AUTHOR_NAME_TAG, info->author_name, SURVEY_AUTHOR_NAME_TAG);
    fprintf(fp, "  </%s>\n",SURVEY_AUTHOR_TAG);
    fprintf(fp, "  <%s>%s</%s>\n",
		    SURVEY_PATH_TAG, info->survey_domain, SURVEY_PATH_TAG);
    fprintf(fp, "</%s>\n", SURVEY_TAG );
}
int
test_file_exist(const char *file_name)
{
    struct stat buf;
    if(stat(file_name, &buf))
       return 0;
    else
       return 1;
}
void
write_head_info_xml(const char *info_file, SURVEY_INFO *info)
{
   FILE *fp;
   fp = fopen(info_file, "w");
   if(!fp)
      cs_critical_error(ERR_FREAD_FAILED, "");
   get_exclusive_lock(fileno(fp),1);
   fprintf(fp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n");
   fprintf(fp, "<%s>\n", SURVEY_LIST_TAG);
   write_one_survey_info(info, fp);
   fprintf(fp, "%s", SURVEY_INFO_END_TAG);
   release_lock(fileno(fp));
   fclose(fp);
}
void
write_survey_info_xml(SURVEY_INFO *info, const char *info_file)
{
    FILE *fp;
    fp = fopen(info_file, "r+");
    if(!fp)
      cs_critical_error(ERR_FREAD_FAILED, "");
    get_exclusive_lock(fileno(fp),1);
    if(fseek(fp, -strlen(SURVEY_INFO_END_TAG), SEEK_END))
       cs_critical_error( ERR_FSEEK_FAILED, "");
    write_one_survey_info(info, fp);
    fprintf(fp, "%s", SURVEY_INFO_END_TAG);
    release_lock(fileno(fp));
    fclose(fp);
}




void
get_survey_id(char *survey_id, const char *base)
{
   char path[MAX_PATH +1], template[MAX_PATH +1];
   char *ptr;
   int fd;

   get_key_template(template);
   snprintf(path, MAX_PATH +1, "%s/%s", base, template);
   fd = mkstemp (path);

   if(fd == -1)
       cs_critical_error (ERR_MKSTEMP_FAILED, "");

  close(fd);

//  change_mkstemp_permission(path);   removed by sn : Why was this here if the file is being deleted? 
  unlink(path);
 
  if(mkdir(path, 0770))
     cs_critical_error (ERR_CANT_OPEN_NEWS_DIR, "");
 

  ptr = strrchr(path, '/');
  if(ptr)
  {
      *ptr = '\0';
      ptr++;
      strncpy(survey_id, ptr, MAX_PATH+1);
   }
   else
      strncpy(survey_id, path, MAX_PATH+1);

}




void
insert_survey_spell_check_button()
{
    if(SPELL_CHECK_ENABLED)
          cs_set_int_value("check_spelling", 1);   /* shared_cs_util.c */
     cs_set_server_name();   /* shared_cs_util.c */
     cs_set_bin_alias();     /* shared_cs_util.c */
}

