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

#include "../global.h"
#include "../survey.h"
#include "shared_authenticate.h"
#include "shared_file_util.h"
#include "shared_survey_deliver_list.h"
#include "shared_util.h"
#include "shared_lock.h"
#include "shared_cs_util.h"

SURVEY_DELIVER_INFO_LIST *
build_survey_info_list(const char *file)
{
    FILE *fp;
    SURVEY_DELIVER_INFO_LIST *head = 0, *ptr, *current;
    INDIV_SURVEY_RECORD record;

    fp = fopen(file, "r");
    if(!fp)
        cs_critical_error( ERR_FOPEN_READ_FAILED, "");
    get_shared_lock(fileno(fp), 1);
     while(fread(&record, sizeof(INDIV_SURVEY_RECORD), 1, fp) == 1)
     {
	 ptr = (SURVEY_DELIVER_INFO_LIST*)malloc(sizeof(SURVEY_DELIVER_INFO_LIST));
	 if(!ptr)
	 {
		fclose(fp);
        	cs_critical_error (ERR_MALLOC_FAILED, "");
	 }
         ptr->record = record;
	 ptr->next =0;
	 if(!head)
	    head = ptr;
	 else
	    current->next = ptr;
	 current = ptr;
     }
     release_lock(fileno(fp));
     fclose(fp);
     return head;
}






void 
free_survey_deliver_info_list(SURVEY_DELIVER_INFO_LIST *head)
{
	SURVEY_DELIVER_INFO_LIST *ptr;
	while(head)
	{
	   ptr = head->next;
	   free(head);
	   head = ptr;
	}

}




int
valid_survey_taker(const char *file, const char *count, int *mode, int *save_course_info)
{
    
    SURVEY_DELIVER_INFO_LIST *list, *ptr;
    int valid = 0;


    list = build_survey_info_list(file);
    for(ptr = list; ptr; ptr = ptr->next)
    {
	if(!strcmp(ptr->record.record.id, count))
	{
          valid = 1;
	  *mode = ptr->record.store_result;
	  *save_course_info = ptr->record.record.save_courseinfo;
	  break;
	}
    }
    
    free_survey_deliver_info_list(list);
    
    if(!valid)
    {
	  *mode = -1;
          *save_course_info = -1;
    }
    return valid;
}





void
remove_this_survey_from_deliver_info(const char *username, const char* count)
{
     SURVEY_DELIVER_INFO_LIST *head, *ptr;
     char file_base[MAX_PATH +1];
     char file[MAX_PATH +1];
     FILE *fp;
     int record_count =0;
     time_t now;
     now = time(NULL);

     snprintf(file_base, MAX_PATH +1, "../%s/%c/%s/%s",
		     USERS_DIR, sub_dir_name(username), username, SURVEY_DIR);
     snprintf(file, MAX_PATH +1, "%s/%s", file_base, SURVEY_DELIVER_INFO_FNAME);
     head =build_survey_info_list(file);
     fp = fopen(file, "w");
     if(!fp)
	 cs_critical_error( ERR_FOPEN_APPEND_FAILED, "");

     get_exclusive_lock(fileno(fp), 1);
     for(ptr = head; ptr; ptr = ptr->next)
     {
	if(strcmp(ptr->record.record.id, count))
	{

          if(now < ptr->record.record.unrelease_time)
	  {
	    if (fwrite (&(ptr->record), sizeof (INDIV_SURVEY_RECORD), 1, fp) != 1)
	       cs_critical_error (ERR_FWRITE_FAILED, "");
	     record_count++;
	  }
	}

     }
     release_lock(fileno(fp));
     fclose(fp);
     if(record_count ==0)
	  kill_directory(file_base);

     free_survey_deliver_info_list(head);
     snprintf(file, MAX_PATH +1, "%s/%s_txt", file_base, count);
     unlink(file);
}
    
