#include "shared_calendar_event.h"



static int one_event_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);
    strncpy(ptr->event.desc, p_fields[2], MAX_CALENDAR_DESC +1);
    strncpy(ptr->event.realname, p_fields[3], MAX_NAME +1);
    ptr->event.year = atoi(p_fields[4]);
    ptr->event.month = atoi(p_fields[5]);
    ptr->event.day = atoi(p_fields[6]);
    ptr->event.s_hour = atoi(p_fields[7]);
    ptr->event.s_min = atoi(p_fields[8]);
    ptr->event.e_hour = atoi(p_fields[9]);
    ptr->event.e_min = atoi(p_fields[10]);
    ptr->event.icon = atoi(p_fields[11]);
    ptr->next =0;

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

   return 0;  
}

int 
get_event(CONFIG_STRUCT *conf, int event_id) 
{
    int ret;
    int nrecs =0;
    char *errmsg =0;
    char select[MAX_PATH +1]; 
    char db_name[MAX_PATH +1]; 
    sqlite3 *db;
   
    event_list = (EVENT_LIST *)malloc(sizeof(EVENT_LIST));
    event_list->head =0;
    event_list->current =0;
    
    snprintf(db_name, MAX_PATH +1, "%s%s/%s", conf->course_path, CALENDAR_DIR, EVENT_DB);

    snprintf(select, MAX_PATH +1, 
	     "select id, title, desc, realname, year, month, day, shour, smin, ehour,emin,icon from event"
	     " where id=%d", event_id);

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

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

void
set_end_ampm( int hour, const char *prefix)
{
   int len = strlen(prefix) + 20;
 
   char name[len];
   snprintf(name,len, "%send_time_am", prefix);
  
   if((hour - 24 >= 0 || hour < 12) )
        cs_set_value( name, "AM");
    else if(hour -12 >= 0 )
        cs_set_value( name, "PM");
    else
        cs_set_value( name, "AM");
}




void set_event(EVENT_NODE *one_event, const char *prefix)
{
   struct tm event_date;

   int len = strlen(prefix) + 20;
   char name[len];
   char name2[len];
   char *str;
   char *date_str;

   snprintf(name, len, "%sevent_id", prefix);
   cs_set_int_value(name, one_event->event.id);

   snprintf(name, len, "%syear", prefix);
   cs_set_int_value(name, one_event->event.year);

   snprintf(name, len, "%smonth", prefix);
   cs_set_int_value(name, one_event->event.month);

   snprintf(name, len, "%sday", prefix);
   cs_set_int_value(name, one_event->event.day);

   snprintf(name, len, "%ss_hour", prefix);
   cs_set_int_value(name, one_event->event.s_hour);

   snprintf(name, len, "%ss_min", prefix);
   cs_set_int_value(name, one_event->event.s_min);

   snprintf(name, len, "%sevent_start", prefix);
   snprintf(name2, len, "%sevent_start_am", prefix);
   set_hour_min(one_event->event.s_hour, one_event->event.s_min, name, name2);

   snprintf(name, len, "%sicon", prefix);
   cs_set_int_value(name, one_event->event.icon);

   snprintf(name, len, "%se_hour", prefix);
   cs_set_int_value(name, one_event->event.e_hour);

   snprintf(name, len, "%se_min", prefix);
   cs_set_int_value(name, one_event->event.e_min);
 
   snprintf(name, len, "%sevent_end", prefix);
   snprintf(name2, len, "%sevent_end_am", prefix);
   set_hour_min(one_event->event.e_hour, one_event->event.e_min, name, name2);


   snprintf(name, len, "%stitle", prefix);
   cs_set_value(name, one_event->event.title);

   snprintf(name, len, "%sescaped_title", prefix);
   cs_html_escape_string(one_event->event.title, &str); 
   cs_set_value(name, str);
   free(str);

   snprintf(name, len, "%sdesc", prefix);
   cs_set_value(name, one_event->event.desc);

   snprintf(name, len, "%sescaped_desc", prefix);
   cs_html_escape_string(one_event->event.desc, &str); 
   cs_set_value(name, str);
   free(str);
    
   init_tm(&event_date,one_event->event.year, one_event->event.month, one_event->event.day);
   date_str = get_selected_timestring(&event_date);
   snprintf(name, len, "%sevent_date", prefix);
   cs_set_value(name, date_str);
   free(date_str); 

}
