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

#include "sqlite3.h"
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "calendar.h"		/* for date FORMAT strings */
#include "manhat-lib/shared_calendar_util.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_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.year = atoi(p_fields[2]);
    ptr->event.month = atoi(p_fields[3]);
    ptr->event.day = atoi(p_fields[4]);
    ptr->event.icon = atoi(p_fields[5]);
    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 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, year, month, day, icon from event where "
	     " year=%d and month=%d order by day",
	       year,  month);
    
    ret = sqlite3_open(db_name, &db);
    if(ret)
      return 0; 

    ret = sqlite3_exec(db,
	  select,
          callback,
          &nrecs,
          &errmsg);
    if(errmsg)
    {
       sqlite3_close(db);
       free(errmsg);
       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
set_nav(int year, int month, int selected_day,  const char *course, const char *key)
{
     int pre_year, pre_month, next_year, next_month;
     char url[MAX_PATH +1];

     get_pre_next(year, month, &pre_year, &pre_month, &next_year, &next_month);


     snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;year=%d&amp;month=%d&amp;today=%d", 
		     CALENDAR_SYSTEM, course, key, pre_year, pre_month, 1);
     cs_set_value( "preurl", url);

     snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;year=%d&amp;month=%d&amp;today=%d", 
		     CALENDAR_SYSTEM, course, key, next_year, next_month, 1);
     cs_set_value( "nexturl", url);

     cs_set_int_value( "month_name", month);
     cs_set_int_value( "year", year);
     
}

void
set_body_calendar(int year, int month, int selected_day,  const char *course, const char *key, int today) 
{
      int start, month_days;
      char url[MAX_PATH +1];
      char name[MAX_PATH +1];
      int  day_name =0, g=0;
      char *str;

      start = get_this_month_start_day( year,  month );
      month_days = get_month_days(month, year);

      cs_set_int_value("start", start);
      cs_set_int_value("month_days", month_days);
      cs_set_int_value("selected_day", selected_day);
      if(list && list->head)
      {
            for(list->current = list->head; list->current; list->current = list->current->next)
	    {
		   if(day_name != list->current->event.day)
		   {
			day_name = list->current->event.day;
			g = 0;
		   }

                   snprintf(name, MAX_PATH +1, "event.%d.%d.title", day_name, g);
		   cs_html_escape_string(list->current->event.title,  &str);
		   cs_set_value( name, str);
		   free(str);

                   snprintf(name, MAX_PATH +1, "event.%d.%d.icon", day_name, g);
		   cs_set_int_value( name, list->current->event.icon);

                   snprintf(name, MAX_PATH +1, "event.%d.%d.url", day_name, g);
                   snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;event_id=%d", CALENDAR_SYSTEM_DETAIL,
				   course, key, list->current->event.id);
		   cs_set_value( name, url);
		   g++;
	    }
	    
      }
}

int
set_calendar( const char *course, const char *key, CONFIG_STRUCT *conf)
{
    time_t current;
    struct tm *today;
    int year =0, month =0, selected_day =0;

    current = time(NULL);
    today = localtime(&current);
   
    year = hdf_get_int_value (global_cgi->hdf, "Query.year", 0);
    month = hdf_get_int_value (global_cgi->hdf, "Query.month", 0);
    selected_day = hdf_get_int_value (global_cgi->hdf, "Query.today", 0);
    
    if(!year && !month)
    {
       year = today->tm_year +1900;
       month = today->tm_mon + 1;
       selected_day = today->tm_mday;
    }
    else if(year && month && selected_day)
    {
       if(year == today->tm_year + 1900 && month == today->tm_mon +1)
	    selected_day = today->tm_mday;
    }
    get_event(conf, year, month);
    set_nav(year, month, selected_day,  course, key);

    if(year == today->tm_year + 1900 && month == today->tm_mon +1)
       set_body_calendar(year, month, selected_day, course,key, today->tm_mday); 
    else
       set_body_calendar(year, month, selected_day,  course,key, 0); 
    if(list)
    {
         free_event_list(list->head);
	 free(list);
    }
    return 1;
}

int set_up_current_calendar( const char *course, const char *key, CONFIG_STRUCT *conf)
{
    
    cs_set_course_info(conf);
    set_calendar( course, key, conf);
    return 1;
}	

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

  SESSION user;
  CONFIG_STRUCT conf;
 
  *course = '\0';
  cs_cgi_init();

  read_parameters ( course, key);
  if( !strlen(course))
     validate_super_key(key, &user);
  else
  {
    read_configuration_file (course, &conf);	
    validate_key (key, &user, &conf);	
  }

  mk_conf(&user, &conf);  


  set_up_current_calendar( course, key,&conf);
  cs_load_lang("calendar");
  cs_cgi_display ( "calendar_system", 0);


  cs_cgi_destroy();                   
  return 0;			/* exit successfully */

}


