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

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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_survey_info_list_parser.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_super_list.h"
#include "manhat-lib/shared_survey_util.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_survey_path_util.h"
#include "manhat-lib/shared_cs_util.h"




typedef struct teacher_list
{
     char username[MAX_USERNAME +1];
     struct teacher_list *next;
}TEACHER_LIST;




void
read_parameters(char *key, char *count, int *oper, int *is_admin, 
		int *domain, char *username, char *course, char *win, char *www_count)
{
   char tmp[10];

   cs_get_required_parameter("id", key, MAX_KEY);
   cs_get_required_parameter("count", count, MAX_PATH);

   cs_get_optional_parameter("admin", tmp, 9);
   *is_admin = !(*tmp)? 0 : atoi(tmp)? 1 : 0;

    cs_get_optional_parameter("crs", course, MAX_PATH);
    
    cs_get_optional_parameter ("oper",tmp,  9);
    *oper = *tmp? atoi(tmp) : -1;
   
       
       
    cs_get_optional_parameter ("domain",tmp,  9);
    *domain = *tmp? atoi(tmp) : -1;

    cs_get_optional_parameter ("username", username,  MAX_USERNAME);
    cs_get_optional_parameter ("win", win,  MAX_PATH);
    cs_get_optional_parameter ("www_count", www_count,  MAX_PATH);

}





SURVEY_INFO *
get_survey_info(const char *count, SESSION *user, int domain, int is_admin, const char *username)
{
    SURVEY_INFO_LIST *list;
    char survey_path[MAX_PATH +1];
    char survey_base[MAX_PATH +1];
    SURVEY_INFO *info =0;
    if(is_admin == 0 && domain ==1)
       snprintf(survey_path, MAX_PATH +1,"../%s/_%s_/%s",SURVEY_DIR, ADMIN_DIR,  SURVEY_INFO_FNAME);
    else
    { 
      if(strlen(username) ==0 || domain ==1)
      {
         get_survey_base(user, survey_base); 
         snprintf(survey_path, MAX_PATH +1,"%s/%s",survey_base,  SURVEY_INFO_FNAME);
      }
      else
	snprintf(survey_path, MAX_PATH +1, "../%s/%s/%s", SURVEY_DIR, username, SURVEY_INFO_FNAME);
    } 
    list = survey_info_parser(survey_path);
    if(list->head)
    {
       info = (SURVEY_INFO *)malloc(sizeof( SURVEY_INFO ));
       list->current = list->head;
       while(list->current)
       {
	   if(!strcmp(list->current->id,count))
	   {
	       info->id = malloc_str(list->current->id);
               info->title  = malloc_str(list->current->title);
               info->state = malloc_str(list->current->state);
	       info->create_time = malloc_str(list->current->create_time);
	       info->finish_time = malloc_str(list->current->finish_time);
	       info->release_time = malloc_str(list->current->release_time);
	       info->close_time = malloc_str(list->current->close_time);
	       info->author_id = malloc_str(list->current->author_id);
	       info->author_name = malloc_str(list->current->author_name);
	       info->survey_domain = malloc_str(list->current->survey_domain);
	       break; 
	   }
	   list->current = list->current->next;
       }
    }

   free_survey_info_list(list);
   return info;
}




void
set_survey_info(SURVEY_INFO *info, int is_admin)
{
    cs_set_value("info.title", info->title);
    cs_set_value("info.id", info->id);
    cs_set_value("info.create_time", info->create_time);
    cs_set_value("info.state", info->state);
    if( !is_admin && !strcmp(info->author_name, "Admin"))
        cs_set_value("info.author_id", "Admin");
    else
        cs_set_value("info.author_id", info->author_id);
    cs_set_value("info.author", info->author_name);

    cs_set_value("info.survey_domain", info->survey_domain);
}





void
get_deliver_info(const char *count, CENTRAL_SURVEY_RECORD *del_info, SESSION *user)
{
   char file[MAX_PATH +1];
   char survey_path[MAX_PATH +1];
   FILE *fp;
  
   get_survey_path(user, survey_path, count); /* shared_survey_path_util.c */
   snprintf(file, MAX_PATH+1, "%s/%s",survey_path, SURVEY_DELIVER_INFO_FNAME);
   fp = fopen(file, "r");
   if(!fp)
	  cs_critical_error( ERR_FOPEN_READ_FAILED, file);
   if (fread (del_info, sizeof (CENTRAL_SURVEY_RECORD), 1, fp) != 1)
          cs_critical_error (ERR_FREAD_FAILED, file);
      fclose(fp);
}

void
set_deliver_info(CENTRAL_SURVEY_RECORD *del_info)
{
    char timestring[MAX_TIMESTRING + 1];

    cs_set_int_value("display_del", 1);

    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime (&(del_info->record.sent_time)));
    cs_set_value("info.deliver_time", timestring);

    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime (&(del_info->record.release_time)));
    cs_set_value("info.release_time", timestring);

    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime (&(del_info->record.unrelease_time)));
    cs_set_value("info.unrelease_time", timestring);

    strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime (&(del_info->record.results_time)));
    cs_set_value("info.result_time", timestring);

    cs_set_int_value("info.course_count", del_info->record.course_count);
   
    if(del_info->record.save_courseinfo )
       cs_set_int_value("info.save", 1);
    else
       cs_set_int_value("info.save", 0);

    cs_set_int_value("info.taker", del_info->takers);
}




void
set_course_list(COURSE_LIST_NODE *head)
{
 #define MAX_TMP_NAME 50

    COURSE_LIST_NODE *ptr;
    char name[MAX_TMP_NAME];
    int i =0;

    for(ptr = head; ptr; ptr = ptr->next)
    {
      snprintf(name, MAX_TMP_NAME, "info.course.%d", i);
      cs_set_value(name, ptr->crs);      
      i++;
    }
    if(i > 0)
      cs_set_int_value("display_course_list", 1);      
	

  #undef MAX_TMP_NAME 
}

    
    


TEACHER_LIST *
get_list_shared_username(int is_admin, const char *count, SESSION *user)
{
     char survey_path[MAX_PATH +1];
     char shared_file[MAX_PATH +1];
     char line[200];
     TEACHER_LIST *ptr, *head = 0, *current = 0;
     FILE *fp;

     if(is_admin)
     {
	get_survey_path(user, survey_path, count);
        snprintf(shared_file, MAX_PATH +1, "%s/%s", survey_path, SURVEY_SHARED_FNAME);
        fp = fopen(shared_file, "r");
        if(!fp)
	   return head;
        while(fgets(line, 200, fp))
	{
	     ptr = (TEACHER_LIST *)malloc(sizeof(TEACHER_LIST));
             strncpy(ptr->username, line, MAX_USERNAME +1);
             ptr->next = 0;
             if(!head)
	        head = ptr;
             else
	       current->next = ptr;
            current = ptr;
         }
        return head;
      }
      else
	 return head;
}     





void
set_shared_list(TEACHER_LIST *head)
{
    
 #define MAX_TMP_NAME 50
    TEACHER_LIST  *ptr;
    char name[MAX_TMP_NAME];
    int i =0;

    for(ptr = head; ptr; ptr = ptr->next)
    {
      snprintf(name, MAX_TMP_NAME, "info.shared.%d", i);
      cs_set_value(name,ptr->username);
      i++;
    }
    if(i>0)
      cs_set_int_value("display_shared", 1);
	
  #undef MAX_TMP_NAME 
}





void
free_teacher_list(TEACHER_LIST *head)
{
     TEACHER_LIST *ptr;
     while(head)
     {
	ptr = head->next;
	free(head);
	head = ptr;
     }
}





void
set_shared_data(const char *count, int is_admin, SESSION *user)
{
    TEACHER_LIST *head;

    head = get_list_shared_username( is_admin, count, user);
    set_shared_list(head);
    free_teacher_list(head);
}







void
do_action(const char *count, const char *key, int oper, SESSION *user, 
		int is_admin, int domain, const char *username, const char *win, const char *www_count)
{
   
    SURVEY_INFO *info = 0;
    CENTRAL_SURVEY_RECORD del_info;
    COURSE_LIST_NODE *head;
    char survey_path[MAX_PATH +1];
    char survey_base[MAX_PATH +1];
    FILE *fp;
    time_t now;
    now = time(NULL); 
 
    info = get_survey_info(count, user,  domain, is_admin, username);   /* shared_survey_reuse_util.c */

    if(!info)
      cs_critical_error(ERR_SURVEY_NO_INFORMATION , "");
      
    set_survey_info(info, is_admin);
    
    if(strcmp(info->state, SURVEY_STATE_REL) ==0 && domain == -1)
    {
      get_deliver_info( count, &del_info, user);
      set_deliver_info(&del_info);
      get_survey_path(user, survey_base, count);
      snprintf(survey_path, MAX_PATH +1, "%s/%s_txt",survey_base, count);
      fp = fopen(survey_path, "r");
      if(!fp)
	  cs_critical_error( ERR_FOPEN_WRITE_FAILED, "");
      head = build_course_list(fp);
      fclose(fp);
      set_course_list(head);
      super_free_course_list(head);

    }
    
    free_survey_info(info);
    cs_set_current_time();
    set_shared_data(count, is_admin, user);
    if(oper != -1)
	cs_set_int_value("delete_button", 1);
    if(oper ==-1 && strlen(username) ==0 && strlen(win) ==0)
	cs_set_int_value("close_win", 1);
          
}

	

int
main()
{
   char key[MAX_KEY +1];
   char count[MAX_PATH +1];
   char www_count[MAX_PATH +1];
   int survey_domain;
   char course[MAX_PATH +1];
   char win[MAX_PATH +1];
   char username[MAX_USERNAME +1];
   int is_admin;
   SESSION user;
   CONFIG_STRUCT conf;
   int  oper;

   cs_cgi_init();
   *username = '\0';
   read_parameters(key, count, &oper, &is_admin, &survey_domain, username, course, win, www_count);

    if(!*course)
      verify_key_path(&user, key, is_admin);   /* shared_survey_path_util.c */
     else
      {
          read_configuration_file(course, &conf);
          validate_key(key, &user, &conf);
       }

    do_action( count, key, oper, &user, is_admin, survey_domain, username, win, www_count);
    
    cs_cgi_display("survey_info_detail", 1);
    cs_cgi_destroy();
    return 0;
}


