#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../custom.h"
#include "../global.h"
#include "shared_time.h"
#include "shared_util.h"
#include "shared_cs_util.h"


static void
check_date_string(const char *date_string)
{
   int len;
   
   len = strlen(date_string);
   if(len > 10 || len < 10)
	cs_critical_error(ERR_SURVEY_TIME_FORMAT, "");
   if(date_string[2] != '/')
	cs_critical_error(ERR_SURVEY_TIME_FORMAT, "");
   if(date_string[5] != '/')
	cs_critical_error(ERR_SURVEY_TIME_FORMAT, "");
}
     




void
get_month_date_year(const char *date_string, int *month, int *date, int *year)
{
   char tmp[25];
   char *ptr, *ptr1;

   check_date_string(date_string);
   strncpy(tmp, date_string, 25);
   ptr = strchr(tmp, '/');
   if(ptr)
      *ptr = '\0';
   ptr++;
   *month = atoi(tmp)-1;
   ptr1 = strchr(ptr, '/');
   if(ptr1)
      *ptr1 = '\0';
   ptr1++;
   *date = atoi(ptr);
   *year = atoi(ptr1)-1900;
}






void set_hour_minute_ampm(struct tm *target_tm, const char *prefix)
{
#define MAX_TMP_NAME 50
 int selected_hour;
 char tmp[20];
 char name[MAX_TMP_NAME];

/* handle hour */
 strftime(tmp, 10, "%I" ,target_tm);
 selected_hour = atoi(tmp);
 snprintf(name, MAX_TMP_NAME, "%s_hour", prefix);
 cs_set_int_value(name,selected_hour);
  
/* handle minute */
 snprintf(name, MAX_TMP_NAME, "%s_minute", prefix);
 cs_set_int_value(name,target_tm->tm_min);

/* handle AM/PM */
 snprintf(name, MAX_TMP_NAME, "%s_is_am", prefix);
 if(target_tm->tm_hour < 12)
     cs_set_int_value(name, 1);
 else
   cs_set_int_value(name, 0);


/* set MM/DD/YYYY text field length */
   cs_set_int_value("mm_dd_yyyy_len", 10);


#undef MAX_TMP_NAME 

}


/* following function returns 1 if year is leap year, else 0 */
int
leap_year (int year)
{
  return (!(year % 4));
}



/* returns number of days in month */
int
days_in_month (int month, int year)
{
  switch (month)
    {
    case 0:
      return (31);
    case 1:
      if (leap_year (year))
	return (29);
      else
	return (28);
    case 2:
      return (31);
    case 3:
      return (30);
    case 4:
      return (31);
    case 5:
      return (30);
    case 6:
      return (31);
    case 7:
      return (31);
    case 8:
      return (30);
    case 9:
      return (31);
    case 10:
      return (30);
    case 11:
      return (31);
    default:
      return (0);


    }
}




int
valid_date (int month, int day, int year)
{

  if ((month < 0) || (month > 11))
    return (0);

  if ((day < 1) || (day > days_in_month (month, year)))
    return (0);

  return (1);
}



/* converts month, day, year to unix calendar time at 
   ** 1 hr 1 min 1 sec of that day
 */

time_t
to_time_t (int month, int day, int year, int hour, int minute, int am)
{
  struct tm tm_time;
  char msg[80];

  if (!valid_date (month, day, year + 1900))
    {
      snprintf (msg, 80, "%d %d %d", month + 1, day, year);
      cs_critical_error (ERR_BAD_DATE,  msg);
    }


  tm_time.tm_mon = month;
  tm_time.tm_mday = day;
  tm_time.tm_year = year;

  if(!am)
   {
      if(hour == 12) 
           hour = 0;
    }
   else
    {
      if(hour != 12)
           hour += 12;
    }
    


  /* now 'normalize' it according to our convention */
  tm_time.tm_hour = hour;
  tm_time.tm_min = minute;
  tm_time.tm_sec = 0;

  tm_time.tm_isdst = -1;   /* don't know if this is Daylight savings time */
  
  return mktime (&tm_time);
}



char *
get_time_string()
{
   char *time_string;
   time_t now;

   now = time (NULL);
   time_string = (char *)malloc(sizeof(char)*(MAX_TIMESTRING +1));
   strftime (time_string, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,
                          localtime (&now));
  return time_string;
}



time_t
get_time( char *prefix)
{
    #define MAX_TMP_NAME 50
    int month, day, year, hour, minute, am;
    char name[MAX_TMP_NAME];
    char tmp[25];
    
    
    cs_get_required_parameter (prefix, tmp, 10);
    get_month_date_year(tmp, &month, &day, &year);
    
    snprintf(name, MAX_TMP_NAME, "%s_hour", prefix);
    cs_get_required_parameter (name, tmp, 24);
    hour = atoi(tmp);
    if(hour == -1)
	cs_critical_error(ERR_PARAM_MISSING, name);

    snprintf(name, MAX_TMP_NAME, "%s_min", prefix);
    cs_get_required_parameter (name, tmp, 24);
    minute = atoi(tmp); 
    if(minute == -1)
	cs_critical_error(ERR_PARAM_MISSING, name);

    snprintf(name, MAX_TMP_NAME, "%s_am", prefix);
    cs_get_required_parameter (name, tmp, 24);
    am = atoi(tmp);
    if(am == -1)
	cs_critical_error(ERR_PARAM_MISSING, name);
	
	
    return to_time_t (month, day, year, hour, minute, am);
    #undef MAX_TMP_NAME 
}

