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

#include "ClearSilver.h"
#include "sqlite3.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_util.h"
#include "manhat-lib/shared_grade_book_calculation_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_grade_book_bar_graph_util.h"


void
read_parameters ( char *course, char *key, int *item_id)
{
     char temp[MAX_KEY];

     cs_get_required_parameter("crs", course, MAX_PATH);
     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("item_id", temp, MAX_KEY);

     *item_id = atoi(temp);
}







void get_all_grades(CONFIG_STRUCT *conf)	
{
    if(global_item_list && global_item_list->head)
    {
	for(global_item_list->current = global_item_list->head; global_item_list->current; global_item_list->current = global_item_list->current->next)
	{
	   if(strcmp(global_item_list->current->grade_entered, "yes") ==0)
	   {
	
	       get_one_item_grade_list(conf, global_item_list->current->item_id, GRADE_BOOK_STUDENT_DB); /* shared_grade_book_db.h */
	       if(global_grade_temp_list && global_grade_temp_list->head)
	       {
		  
		   if(strcmp(global_item_list->current->is_letter_grade, "yes") == 0)
			 update_letter_log_list(conf,  global_item_list->current->item_id, GRADE_BOOK_STUDENT_DB, global_grade_temp_list);  /* shared_grade_book_db.h */

	               global_item_list->current->grades = global_grade_temp_list;
	       }

	   }
	}
    }
}



void do_action(CONFIG_STRUCT *conf, int item_id)
{
   INTERVAL_COUNT *head;
   int number_std;
   P_NODE *std_head;
   TOTAL_GRADE *std_grade;

   cs_set_server_name();
   if(item_id > 0)
   {
       get_one_item(conf, item_id, GRADE_BOOK_STUDENT_DB); /* shared_grade_book_db.h */

       if(!global_item_list || !global_item_list->head)
	     cs_critical_error(ERR_GRADES_DB, "no such item in db");
       if(strcmp(global_item_list->head->grade_entered, "no") ==0)
	     cs_critical_error(ERR_GRADES_DB, "no grades for this item");
      
       get_one_item_grade_list(conf, global_item_list->head->item_id, GRADE_BOOK_STUDENT_DB);  /* shared_grade_book_db.h */
       if(!global_grade_temp_list || !global_grade_temp_list->head)
	     cs_critical_error(ERR_GRADES_DB, "no grades for this item");


       if(strcmp(global_item_list->head->is_letter_grade, "yes") ==0)
	     update_letter_log_list(conf,  global_item_list->head->item_id, GRADE_BOOK_STUDENT_DB, global_grade_temp_list);  /* shared_grade_book_db.h */
       global_item_list->head->grades = global_grade_temp_list;

       get_items_min_avg_max(conf, GRADE_BOOK_STUDENT_DB);  /*shared_grade_book_calculation_util.c */
       set_item_list_values(1, 0);   /* shared_grade_book_util.h */

       if(strcmp(global_item_list->head->is_letter_grade, "no") ==0)
       {
          head = get_interval_list(global_item_list->head->points);
          update_interval_count(head); 
          number_std = get_number_student(global_item_list->head->grades->head);
          set_bar_graph_value(head, number_std);
          if(head)
             free_interval_list(head);
       }
       else
       {
    	  update_letter_grade_count(); 
          number_std = get_number_student(global_item_list->head->grades->head);
          set_letter_bar_graph_value(number_std);
       }

       if(global_item_list)
	       free(global_item_list);
   }
   else
   {
         std_head = build_option_person_list(conf); /* shared_person_list.c */
         get_categories(conf, GRADE_BOOK_STUDENT_DB);  /* shared_grade_book_db.c */
         if(!global_cat_list || !global_cat_list->head)
	    cs_critical_error(ERR_GRADES_DB, "bar graph get_cat_list action");
         get_items(conf, GRADE_BOOK_STUDENT_DB, 0);     /* shared_grade_book_db.c */
         if(!global_item_list || !global_item_list->head)
	    cs_critical_error(ERR_GRADES_DB, "bar graph item_list action");
         get_all_grades(conf);
         get_items_min_avg_max(conf, GRADE_BOOK_STUDENT_DB);  /* shared_grade_book_calculation_util.h */	 
	    	
	 std_grade = calculate_total_grades(std_head,conf, GRADE_BOOK_STUDENT_DB); /* shared_grade_book_calculation_db.h */
	 if(std_grade)
	  {
	       number_std = get_student_number(std_grade);
	       if(item_id == -1)
		{
	           update_scale_grade_count(std_grade);
		   set_scale_letter_grades( number_std); 
		}
               else
	       {
                    head = get_interval_list(100);
		    update_total_count(head, std_grade);
                    set_bar_graph_value(head, number_std);
		    if(head)
                       free_interval_list(head);
	       }	       
	      free_total_grade_list(std_grade);
	  }
	 if(std_head)
	     free_option_person_list(std_head);    /* shared_person_list.c */
	 if(global_cat_list)
	           free_category_list(global_cat_list);
	  if(global_item_list)
		   free_item_list(global_item_list);
	  if(global_scale_list)
	           free_scale_list(global_scale_list);


   }
    
}


int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  int item_id =0;
  SESSION user;
  CONFIG_STRUCT conf;

  cs_cgi_init();      /* shared_cs_util.c */
  read_parameters (course, key,&item_id);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	

  cs_set_course_info( &conf);  /* shared_cs_util.c */
  cs_set_current_time();  /* shared_cs_util.c */

  do_action(&conf, item_id);
  cs_cgi_display("grade_book_bar_graph", 1);
  cs_cgi_destroy();                   /** shared_cs_util.c */ 

  return 0;			/* exit successfully */

}





