#include "shared_calendar_util.h"
#include "shared_cs_util.h"

static int add_day(int weekday, int inc) 
{
   return( (weekday % 7 + inc % 7) % 7 ) ;
}

static int is_leap_year(int year) 
{
   return ( ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0) );
}

int get_year_days(int year) 
{
    if (is_leap_year(year)) 
         return(366) ;
    else
        return(365) ;
}

int get_month_days(int month, int year) 
{
    switch (month) 
    {
       case 2:
	 if (is_leap_year(year)) return (29);
         return (28);
       case 4: case 6: case 9: case 11:
         return (30);
       default:
          return (31);
    }
}



int
get_this_month_start_day(int year, int month )
{
   int start, i, j;
   int this_month_days;

    /* January 1900 start on monday */
    start = 1;
    for(i = 1900; i< year; i++)
       start = add_day(start, get_year_days(i));
   
   for(j = 1; j<=12;j++)
   {
     this_month_days = get_month_days(j, year);
     if(month == j)
	break;
     start = add_day(start, this_month_days);
   }
   return start;
}

void
get_pre_next(int year, int month, int *pre_year, int *pre_month, int *next_year, int *next_month)
{
    if(month < 1 || month > 12)
        cs_critical_error(ERR_BAD_MONTH, "");
     if(month == 1)
     {
         *pre_month = 12;
	 *pre_year = year -1;
	 *next_month = month +1;
	 *next_year = year;
      }
      else if(month == 12)
      {
         *next_month = 1;
	 *next_year = year +1;
	 *pre_month = month -1;
	 *pre_year = year;

      }
     else
     {
        *pre_month  = month -1;
        *next_month = month +1;
	*pre_year =  year;
	*next_year = year;
     }
}

void
mk_conf(SESSION *user, CONFIG_STRUCT *conf)
{
   strncpy(conf->course_no, "Admin",  MAX_COURSE_NO + 1);
   strncpy(conf->title, "System Calendar", MAX_COURSE_TITLE + 1);
   strncpy(conf->semester, " " ,MAX_SEMESTER + 1);
   strncpy(conf->instructor, "Manhattan administrator", MAX_INSTRUCTOR + 1);
   snprintf(conf->course_path, MAX_PATH +1, "../%s/%s/", USERS_DIR, ADMIN_DIR);
   conf->theme = 0;
}


void
init_tm(struct tm *select, int year, int month, int today)
{
    select->tm_year = year -1900;
    select->tm_mon = month -1;
    select->tm_mday = today;

    select->tm_hour = 10;
    select->tm_min = 0;
    select->tm_sec =0;
    select->tm_isdst = -1;
}

char *get_selected_timestring(struct tm *select)
{
    char *time_string;
    time_t mytime;

    mytime = mktime(select);
    time_string = (char *)malloc(sizeof(char)*(MAX_TIMESTRING +1));
    strftime (time_string, MAX_TIMESTRING, DOW_DATE_FORMAT,
	      localtime (&mytime));
     return time_string;
}

void
free_event_list(EVENT_NODE *head)
{
   EVENT_NODE *ptr;

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

void
set_selected_time(int selected_day, int year, int month)
{
   struct tm select;
   char *timestring;

   init_tm(&select, year, month, selected_day);
   timestring = get_selected_timestring(&select);
   cs_set_value( "selecttimestring", timestring);
   free(timestring);
}



void
set_hour_min(int hour, int min ,  char *hour_min_name,  char *am_name)
{
   char time_str[MAX_PATH +1];
   char am_flag[40];
   char temp[40];
   
   if(min < 10)
     snprintf(temp, 40, "0%d", min);
   else
     snprintf(temp, 40, "%d", min);

   if(hour - 24 >0)
   {
      snprintf(time_str, MAX_PATH +1, "%d:%s", hour-24, temp);
      snprintf(am_flag, 40, "AM");
   }
   else if( hour-12 >0)
   {
       snprintf(time_str, MAX_PATH +1, "%d:%s", hour-12, temp);
       snprintf(am_flag, 40, "PM");
   }
   else
   {
       if(hour-12 ==0)
       {
         snprintf(time_str, MAX_PATH +1, "%d:%s", hour, temp);
         snprintf(am_flag, 40, "PM");
        }
        else
        {
        snprintf(time_str, MAX_PATH +1, "%d:%s", hour, temp);
        snprintf(am_flag, 40, "AM");
        }
     }
     cs_set_value(hour_min_name, time_str);
     cs_set_value(am_name, am_flag);
}


void get_previous_day(struct tm *realtime, int *year, int *month, int *day)
{
    if(realtime->tm_mday -1 == 0 )
    {
        if((realtime->tm_mon +1 )  -1 == 0)
        {
             *year = realtime->tm_year -1 + 1900;
             *month = 12;
             *day = get_month_days( *month, *year);
         }
         else
         {
             *year = realtime->tm_year  + 1900;
             *month = realtime->tm_mon;
             *day = get_month_days( *month, *year);
										         }
      }
      else
      {
             *year = realtime->tm_year  + 1900;
             *month = realtime->tm_mon +1;
	     *day = realtime->tm_mday - 1;
       }
}


