#include "shared_grade_book_bar_graph_util.h"



INTERVAL_COUNT *get_interval_list(int points)
{
    INTERVAL_COUNT *head =0, *current=0, *ptr;
    double interval_value =0;
    int i, mod, max_interval;

     if(points < MAX_INTERVALS)
     {
         max_interval = points;
	 mod =0;
     }
     else
     {
       mod = points % MAX_INTERVALS;
       max_interval = MAX_INTERVALS;
     }

   interval_value = points / max_interval;
    cs_set_int_value("max_intervals", max_interval);
     for(i =0; i<max_interval; i++)
     {
          ptr = (INTERVAL_COUNT *)malloc(sizeof(INTERVAL_COUNT));
          if(!ptr)
               cs_critical_error(ERR_MALLOC_FAILED, "");
           ptr->next =0;
           if(i ==0)
              ptr->start = i * interval_value;
           else
              ptr->start = i * interval_value + (mod ? 0.1 : 1);
           ptr->end = (i+1) * interval_value;
           ptr->count =0;
           if(!head)
                head = ptr;
           else
                current->next = ptr;
           current = ptr;
    }
    return head;
}

void
free_interval_list(INTERVAL_COUNT *head)
{
    INTERVAL_COUNT *ptr;

     if(head)
    {
          while(head)
          {
             ptr = head->next;
            free(head);
             head = ptr;
        }
   }
}






void update_interval_count(INTERVAL_COUNT *head) 
{
  INTERVAL_COUNT *int_ptr;
  GRADE *grades;
  float temp1 =0;

  if(!global_item_list || !global_item_list->head)
     cs_critical_error(ERR_GRADES_DB, "no grades");
  if(!global_item_list->head->grades)
     cs_critical_error(ERR_GRADES_DB, "no grades");

   for(int_ptr = head; int_ptr; int_ptr = int_ptr->next)
   {
       for(grades = global_item_list->head->grades->head; grades; grades = grades->next)
       {
	  if(strcmp(grades->pending, "no") ==0 && strcmp(grades->excuse, "no") ==0)
	  {
	     if(grades->score >= int_ptr->start && grades->score <= int_ptr->end)
		   (int_ptr->count)++;
	     else if(grades->score >= int_ptr->start)
	     {
		     temp1 = grades->score - (int)int_ptr->end;
		     if(temp1 >0 && temp1 < 1)
			   (int_ptr->count)++;
	     }

	  }
       }	       
   }
}

void  update_letter_grade_count() 
{
    LETTER_GRADE *ptr;
    GRADE *grades;

  if(!global_item_list || !global_item_list->head)
     cs_critical_error(ERR_GRADES_DB, "no grades");
  if(!global_item_list->head->grades)
     cs_critical_error(ERR_GRADES_DB, "no grades");

    for(ptr = global_letter_grades->head; ptr; ptr = ptr->next)
    {
       for(grades = global_item_list->head->grades->head; grades; grades = grades->next)
       {
	  if(strcmp(grades->pending, "no") ==0 && strcmp(grades->excuse, "no") ==0)
	  {
	    if(strcmp(ptr->letter, grades->letter) ==0)
		  (ptr->count)++;
	  }
       } 
    }
}

int get_number_student(GRADE *grades)
{
    GRADE *ptr;
    int count =0;

    for(ptr = grades; ptr; ptr = ptr->next)
    {
	  if(strcmp(ptr->pending, "no") ==0 && strcmp(ptr->excuse, "no") ==0)
             count++;
    }
    return count;

}

float
compute_percent(int total, int count)
{
    if(total >= count)
        return (float) count / (float) total * 100;
   return 0;
}

void  set_bar_graph_value(INTERVAL_COUNT *head, int number_std)
{
#define MAX_TMP 50

   INTERVAL_COUNT *ptr;
   char name[MAX_TMP];
   char temp[MAX_TMP];
   int i =0;
   float percent;
  
   for(ptr = head; ptr; ptr = ptr->next)
   {
      percent = compute_percent(number_std, ptr->count);

      snprintf(name, MAX_TMP, "percent.%d", i);
      snprintf(temp, MAX_TMP, "%4.*f", test_precision(percent), percent);
      strtrm(temp); 
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "xscale.%d", i);
      snprintf(temp, MAX_TMP, "%4.*f-%4.*f", test_precision(ptr->start), ptr->start, test_precision(ptr->end),ptr->end);
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "count.%d", i);
      snprintf(temp, MAX_TMP, "%d", ptr->count);
      cs_set_value(name, temp);

      i++;

   }  
   cs_set_int_value("number_scales", i);

#undef MAX_TMP 
}

void set_letter_bar_graph_value(int number_std)
{
#define MAX_TMP 50
   LETTER_GRADE *ptr;
   char name[MAX_TMP];
   char temp[MAX_TMP];
   int i =0;
   float percent;

   for(ptr = global_letter_grades->head; ptr; ptr = ptr->next)
   {
      snprintf(name, MAX_TMP, "percent.%d", i);
      percent = compute_percent(number_std, ptr->count);
      snprintf(temp, MAX_TMP, "%4.*f", test_precision(percent), percent);
      strtrm(temp); 
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "xscale.%d", i);
      snprintf(temp, MAX_TMP, "%s", ptr->letter);
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "count.%d", i);
      snprintf(temp, MAX_TMP, "%d", ptr->count);
      cs_set_value(name, temp);
      i++;
   }
   cs_set_int_value("number_scales", i);
#undef MAX_TMP 
}




void update_scale_grade_count(TOTAL_GRADE *head)
{
    SCALE *ptr;
    TOTAL_GRADE *st_ptr;
    for(ptr = global_scale_list->head; ptr; ptr = ptr->next)
    {
	for(st_ptr = head; st_ptr; st_ptr = st_ptr->next)
	{
             if(strcmp(st_ptr->grade, ptr->grade) == 0)
		     (ptr->count)++;
	}
    }
}

int get_student_number(TOTAL_GRADE *head)
{
     int count =0;
     TOTAL_GRADE *st_ptr;

     for(st_ptr = head; st_ptr; st_ptr = st_ptr->next)
          count++; 
     return count;
}

void set_scale_letter_grades( int number_std)
{
#define MAX_TMP 50
   SCALE *ptr;
   char name[MAX_TMP];
   char temp[MAX_TMP];
   int i =0;

   for(ptr = global_scale_list->head; ptr; ptr = ptr->next)
   {
      snprintf(name, MAX_TMP, "percent.%d", i);
      snprintf(temp, MAX_TMP, "%4.2f", compute_percent(number_std, ptr->count));
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "xscale.%d", i);
      snprintf(temp, MAX_TMP, "%s", ptr->grade);
      cs_set_value(name, temp);

      snprintf(name, MAX_TMP, "count.%d", i);
      snprintf(temp, MAX_TMP, "%d", ptr->count);
      cs_set_value(name, temp);
      i++;
   }
   cs_set_int_value("number_scales", i);
#undef MAX_TMP 
}

void  update_total_count(INTERVAL_COUNT *head, TOTAL_GRADE *std_grade)
{
   INTERVAL_COUNT *ptr;
   TOTAL_GRADE *st_ptr;

   for(ptr = head; ptr; ptr = ptr->next)
   {
       for(st_ptr = std_grade; st_ptr; st_ptr=st_ptr->next)
       {
	    if(st_ptr->total >= ptr->start && st_ptr->total <= ptr->end)
		    (ptr->count)++;
       }

   }
     
}







