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

#include "ClearSilver.h"
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "grade.h"		/* for date FORMAT strings */
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_grade_book_menu.h"
#include "manhat-lib/shared_grade_book_util.h"
#include "manhat-lib/shared_grade_book_db.h"
#include "manhat-lib/shared_file_util.h"

void set_default_scale_value( int number,  int d_points, char *d_grade);
void set_default_scale();



void set_default_final_grade_value()
{
     cs_set_int_value("default_scoring_method", 0);
     cs_set_int_value("default_rounding_method", 1);
     cs_set_int_value("no_final_scoring_method", 1);
     set_default_scale();
}

void set_db_scales()
{
   #define MAX_TMP 35
    int i = 0;
    char name[MAX_TMP];
    char temp_value[MAX_TMP];

    for(global_scale_list->current = global_scale_list->head; global_scale_list->current; global_scale_list->current = global_scale_list->current->next)
    {
       snprintf(name, MAX_TMP, "scale.%d.start", i);
       snprintf(temp_value, MAX_TMP, "%3.2f", global_scale_list->current->start);
       cs_set_value(name, temp_value);
       snprintf(name, MAX_TMP, "scale.%d.grade", i);
       cs_set_value(name, global_scale_list->current->grade);
       i++;       
    }

    cs_set_int_value( "number_scales", i);
   #undef MAX_TMP 
}


void set_final_grade_value(GRADE_BOOK_CONFIGURE *grade_conf)
{
     cs_set_int_value("default_scoring_method", grade_conf->scoring_method);
     cs_set_int_value("default_rounding_method", grade_conf->rounding_method);
     cs_set_int_value("has_final_method", 1);
     set_db_scales(); 
}


void
read_parameters ( char *course, char *key)
{
     cs_get_required_parameter("crs", course, MAX_PATH);
     cs_get_required_parameter("id", key, MAX_KEY);

}



void do_acton(CONFIG_STRUCT *conf)  
{
   char *default_scales;
   GRADE_BOOK_CONFIGURE grade_conf;

   default_scales = hdf_get_value(global_cgi->hdf, "Query.default", "");
   
   if(strlen(default_scales))
   {
     set_default_final_grade_value();
     cs_set_int_value( "final", 1);
   }
   else if(read_grade_book_conf(conf, &grade_conf))   /* shared_grade_book_util.h */
   {
         get_scales(conf, GRADE_BOOK_DB);   /* sahred_grade_book_db.h */
	 if(global_scale_list && global_scale_list->head)
	 {
            set_final_grade_value(&grade_conf);
	    free_scale_list(global_scale_list);
	 }
	 else
	   cs_critical_error(ERR_GRADES_DB, "grade_book_final do action");

   }
   else
   {
     cs_set_int_value( "final", 0);
     set_default_final_grade_value();
   }
   cs_set_int_value("grade_len", MAX_GRADE_LEN);
}

int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];

  SESSION user;
  CONFIG_STRUCT conf;
  
  cs_cgi_init();   /* shared_cs_util.h */
  read_parameters ( course, key);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	

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

  cs_set_course_info(&conf);  
  set_grade_book_menu("grade_book_final", key, course); /* shared_grade_book_menu.h */
  cs_load_lang("grade_book_menu_util");
  do_acton(&conf);  
  cs_cgi_display ("grade_book_final", 1);
  cs_cgi_destroy();      

  return 0;			

}





void
set_default_scale_value( int number,  int d_points, char *d_grade)
{
#define MAX_TEMP_LEN 35
       char name[MAX_TEMP_LEN +1];
       snprintf(name, MAX_PATH +1, "scale.%d.start", number);
       cs_set_int_value( name, d_points);
       snprintf(name, MAX_PATH +1, "scale.%d.grade", number);
       cs_set_value( name, d_grade);
#undef MAX_TEMP_LEN 
}
void
set_default_scale()
{
     char letter;
     char grade[3]; 
     int i =1;
     int default_points =93;
     for(letter = 'A'; letter <= 'D'; letter++)
     {
	if(letter != 'A')
	{
	 snprintf(grade, 3, "%c+", letter);
         set_default_scale_value( i,  default_points, grade); 
	 default_points -= 4;
	 i++;
	}
	 snprintf(grade, 3, "%c", letter);
         set_default_scale_value( i,  default_points, grade); 
	 default_points -= 3;
	 i++;

	 snprintf(grade, 3, "%c-", letter);
         set_default_scale_value( i,  default_points, grade); 
         default_points -= 3;

	 i++;
     }
     set_default_scale_value( i,  0, "F"); 
    cs_set_int_value( "number_scales", i);
    cs_set_int_value( "come_de", 1);
}

