/* ********************************************
 *  file: calendar_slot.c 
 * ********************************************
 */

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

#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "sqlite3.h"
#include "calendar.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_calendar_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_file_util.h"


EVENT_LIST *list;


int callback(void *p_data, int num_fields, char **p_fields, char **p_col_name)
{
    EVENT_NODE *ptr;

    ptr = (EVENT_NODE *)malloc(sizeof(EVENT_NODE));

    ptr->event.id = atoi(p_fields[0]);
    strncpy(ptr->event.title, p_fields[1], MAX_CALENDAR_TITLE +1);
    ptr->event.s_hour = atoi(p_fields[2]);
    ptr->event.s_min = atoi(p_fields[3]);
    ptr->next =0;

    if(!list->head)
	 list->head = ptr;
    else
        list->current->next = ptr;
    list->current = ptr; 

   return 0;  
}

int 
get_event(CONFIG_STRUCT *conf, int year, int month, int day )
{
    int ret;
    int nrecs =0;
    char *errmsg =0;
    char select[MAX_PATH +1]; 
    char db_name[MAX_PATH +1]; 
    sqlite3 *db;
   
    snprintf(db_name, MAX_PATH +1, "%s%s/%s", conf->course_path, CALENDAR_DIR, EVENT_DB);
    if(!file_exists(db_name))
	 return 0;

    list = (EVENT_LIST *)malloc(sizeof(EVENT_LIST));
    list->head =0;
    list->current =0;
    

    snprintf(select, MAX_PATH +1, 
	     "select id, title, shour, smin from event where "
	     " year=%d and month=%d and day=%d ",
	       year,  month, day);

    ret = sqlite3_open(db_name,&db);
    if(ret)
      return 0; 

    ret = sqlite3_exec(db,
	  select,
          callback,
          &nrecs,
          &errmsg);
    if(errmsg)
    {
      sqlite3_close(db);
      return 0; 
    }
    sqlite3_close(db);
    return 1;
}

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


void make_window_name(const char *course_real, char *mkName)
{
    char *ptr;
    strncpy(mkName, course_real, MAX_PATH +1);

    for(ptr = mkName; *ptr; ptr++)
         if(isspace(*ptr) || ispunct(*ptr))
               *ptr = 'x';
}

void
set_one_hour( int is_link, const char *course, const char *key, int year, int month, int today, int hour)
{
#define MAX_TEM_STR 30
   static int i =0;
   char url[MAX_PATH +1];
   char name[MAX_TEM_STR +1];
   int is_am =0;
   char *new_str;
   

   if(hour -24 >=0)
   {
      snprintf(name, MAX_TEM_STR +1, "slot.%d.hour", i);
      cs_set_int_value(name, hour - 24);
      snprintf(name, MAX_TEM_STR +1, "slot.%d.am", i);
      cs_set_value(name, "AM");
      is_am =1;
   }
   else if(hour-12 >0)
   {
      snprintf(name, MAX_TEM_STR +1, "slot.%d.hour", i);
      cs_set_int_value(name, hour - 12);
      snprintf(name, MAX_TEM_STR +1, "slot.%d.am", i);
      cs_set_value(name, "PM");
      is_am = 2;
   }
   else
   {
      snprintf(name, MAX_TEM_STR +1, "slot.%d.hour", i);
      cs_set_int_value(name, hour );
      snprintf(name, MAX_TEM_STR +1, "slot.%d.am", i);
      if(hour -12 ==0)
      {
             cs_set_value(name, "PM");
             is_am = 2;
      }
      else
      {
            cs_set_value(name, "AM");
            is_am =1;
      }
   }
   if(is_link)
   {
      snprintf(name, MAX_TEM_STR +1, "slot.%d.url", i);
      snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;year=%d&amp;month=%d&amp;today=%d&amp;hour=%d&amp;min=",
	   CALENDAR_EVENT_EDIT, course, key, year, month, today, hour);
      cs_set_value( name, url);
   }
   i = i +1;
   if(list && list->head)
     {
	 for(list->current = list->head; list->current; list->current = list->current->next)
	 {
	       if(list->current->event.s_hour == hour )
	       {
		   if(list->current->event.s_min < 10 )
                      snprintf(name, MAX_TEM_STR +1, "event.%d:0%d%s.title", hour,list->current->event.s_min, is_am == 1?"am": is_am == 2 ?  "pm":"");
		   else
                      snprintf(name, MAX_TEM_STR +1, "event.%d:%d%s.title", hour,list->current->event.s_min, is_am == 1?"am": is_am == 2 ?  "pm":"");
                   cs_html_escape_string(list->current->event.title, &new_str);                   
                   cs_set_value( name, new_str);
		   free(new_str);

		   if(list->current->event.s_min < 10 )
                      snprintf(name, MAX_TEM_STR +1, "event.%d:0%d%s.titleurl", hour,list->current->event.s_min, is_am == 1?"am": is_am == 2 ? "pm":"");
		   else
                      snprintf(name, MAX_TEM_STR +1, "event.%d:%d%s.titleurl", hour,list->current->event.s_min, is_am == 1?"am": is_am == 2 ? "pm":"");
                   snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;year=%d&amp;month=%d&amp;today=%d&amp;event_id=%d",
			CALENDAR_EVENT_DETAIL, course, key, year, month, today, list->current->event.id);
                   cs_set_value( name, url);
	       }
	 }
      }
   
#undef MAX_TEM_STR

}

int
set_value( const char *course, const char *key, SESSION *user, CONFIG_STRUCT *conf)
{
   int i, start =7, is_link;
   char winname[MAX_PATH +1];
   int year, month,  today;
    
    if(user->group == STUDENT)
       is_link =0;
    else
       is_link = 1;

    year = hdf_get_int_value (global_cgi->hdf, "Query.year", 0);
    month = hdf_get_int_value (global_cgi->hdf, "Query.month", 0);
    today = hdf_get_int_value (global_cgi->hdf, "Query.today", 0);

   get_event(conf, year, month, today );

   for(i = 0; i<24; i++)
      set_one_hour(is_link, course, key, year, month, today, i+start);
   if(list)
   {
     free_event_list(list->head);
     free(list);
   } 
    make_window_name(course, winname);
    cs_set_value( "winname", winname);
    return 1;
}


int
main ()
{
  char course[MAX_PATH +1];	/* from crs=? command line */
  char key[MAX_KEY +1];	/* from id=? command line */

  SESSION user;
  CONFIG_STRUCT conf;
  *course = '\0';

  cs_cgi_init();
  read_parameters ( course, key);

  if(!strlen(course))
  {
     validate_super_key(key, &user);  /* shared_super_util.c */
     mk_conf(&user, &conf); /* shared_calendar.c */ 
  }
  else 
  {
    read_configuration_file (course, &conf);	/* shared_util.c */
    validate_key (key, &user, &conf);	/* shared_util.c */
  }
  set_value( course, key, &user, &conf);

  cs_cgi_display ("calendar_slot", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}




