#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <string.h>

#include "global.h"
#include "survey.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_read_infofile.h"
#include "manhat-lib/shared_clipboard_util.h"
#include "manhat-lib/shared_time.h"
#include "manhat-lib/shared_spelling_util.h"




static void
read_parameters (char *course, char *key, int *is_public, 
				char *survey_dirname, char *survey_title, int *paste)
{
#define MAX_TMP 300

  char tmp[MAX_TMP + 1];
  char *ptr, *ptr2;
  

  cs_get_required_parameter ("crs", course, MAX_FILENAME);   /* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("survey_id", tmp , MAX_TMP);
  
  ptr = strchr(tmp, ':');
  if(!ptr)
      cs_critical_error(ERR_PARAM_MISSING, "survey_id");
  *ptr = '\0';
  
  *is_public = !strcmp(tmp,"public");
  
  ptr2 = ptr + 1;
  ptr = strchr(ptr2, ':');
  if(!ptr)
      cs_critical_error(ERR_PARAM_MISSING, "survey_id");
  *ptr = '\0';
  strncpy(survey_dirname, ptr2, MAX_PATH + 1);
  
  strncpy(survey_title, ptr + 1, MAX_SURVEY_TITLE + 1);
      
  cs_get_optional_parameter ("paste", tmp, 19);
  if(strlen(tmp) ==0)
    *paste = 0;			/* not pasting */
  else
    *paste = 1;			/* we are pasting from clipboard */

#undef MAX_TMP
}





static void
new_memo_header (char *course,  char *key,  CONFIG_STRUCT *conf, SESSION *user, 
                                            int is_public,  char *survey_dirname,  char *survey_title)
{
 int empty_clipboard;
 char url[MAX_PATH +1];

   /** clipboard_empty() is in shared_clipboard_utils.c */
  empty_clipboard = (user->group != FACULTY)
			    || clipboard_empty (user->username, conf->course_path, user);
  cs_set_course_info(conf);
  cs_set_current_time();
  insert_spell_check_button(conf);

  cs_set_int_value("msg.textarea.cols", editor_width_config(conf->misc));   /* shared_news_util.c */
  cs_set_int_value("msg.textarea.wrap", USE_TEXTAREA_HARD_WRAP);
  
  snprintf (url, MAX_PATH +1, "%s?admin=0&amp;id=%s&amp;crs=%s", 
                           "survey_read_info",
                           key,
                           course);
  cs_set_value("back_url", url); 
  if (!empty_clipboard)
  {
   cs_set_int_value("paste_action", 1);
   snprintf (url, MAX_PATH +1, "%s:%s:%s",is_public? "public" : "private",survey_dirname, survey_title);
   cs_set_value( "survey_id", url);
  }    
  cs_set_value( "crs", course);
  cs_set_value("id", key);
  cs_set_int_value("public", is_public);
  cs_set_value( "dir", survey_dirname);
 
}

static int
do_paste (SESSION *user, CONFIG_STRUCT *conf)
{
  CLIPBOARD_INFO clipboard_info;
  char clipboard_path[MAX_PATH + 1];
  char *clip_subject =0;


  if (user->group != FACULTY)
    cs_critical_error (ERR_REQUEST_DENIED, "");

  if (get_clipboard_info (&clipboard_info, clipboard_path, user))   /* shared_read_infofile.c */
    return 1;			/* clipboard is empty */
  cs_set_int_value("do_clipboard", 1);

  /* print clipboard subject but must replace quotes " with &quot; */
  clip_subject = replace_quot(clipboard_info.subject);  /* shared_news_util.h */
  cs_set_value("msg.subject.value", clip_subject);
  free(clip_subject);

  /* handle textarea */
  do_paste_textarea (clipboard_path, clipboard_info.msg_fname,
		     clipboard_info.sender_username,
		     clipboard_info.sender_realname, user, conf);  /* shared_news_util.h */

  return 0;			/* paste successful */

}


static
void print_result_time_options()
{
 time_t now;
 struct tm current_time;
 
 now = time(NULL);
 current_time = *localtime(&now);
 set_hour_minute_ampm(&current_time, "current");  /* shared_time.h */

}


static void
display_new_memo_form ( char *course,  char *key, int is_public, 
  			 char *survey_dirname,  char *survey_title, 
  	                       CONFIG_STRUCT *conf, SESSION *user, int paste)
{
 int paste_failed = 1;
    
  new_memo_header (course, key,conf, user, is_public, survey_dirname, survey_title);
  if(is_public)
     cs_set_value("survey_type", "public");
  else
     cs_set_value("survey_type", "private");

  cs_set_value("survey_title", survey_title);
  
  print_result_time_options();
  if (paste)
      paste_failed = do_paste (user, conf);
   
  cs_set_int_value("msg.subject.max", MAX_SUBJECT -1);

}




int
main (void)
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  char survey_dirname[MAX_PATH + 1];
  char survey_title[MAX_SURVEY_TITLE + 1];
  int is_public;
  int paste;
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  cs_cgi_init();

  read_parameters (course, key, &is_public, survey_dirname, survey_title, &paste);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

     
  if((user.group != FACULTY)|| !has_write_permission(&user, &conf))   /* shared_access.c */
      access_denied_error();
      
  set_discussion_names (conf.course_path, user.username, SURVEYS);	/* shared_news_util.c */

  display_new_memo_form (course, key, is_public, survey_dirname, survey_title, &conf, &user, paste);
  cs_cgi_display("survey_new_memo", 1);
  cs_cgi_destroy();
  return 0;
}



