#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "manhat-lib/shared_util.h" 
//#include "manhat-lib/shared_cgi_util.h" 
#include "manhat-lib/shared_cs_util.h" 

#define MAX_ACCOUNT_TYPE 15 
#define MAX_NUM_PARAM_ID 3

void
read_parameters(int *count, char* id[MAX_NUM_PARAM_ID], char *type[MAX_NUM_PARAM_ID], char *expire) 
{
    char temp[MAX_ACCOUNT_TYPE +1];
    int i;
    char name[20];

    
    cs_get_required_parameter("count", temp, MAX_ACCOUNT_TYPE);
    *count = atoi(temp);

    if(*count >0 && *count <= 3)
    {	  
       for(i =0; i<*count; i++)
       {  
	  snprintf(name, 20, "id%d", i+1);
          cs_get_required_parameter(name, id[i], MAX_KEY);
	  snprintf(name, 20, "acc_type%d", i+1);
          cs_get_required_parameter(name, type[i], MAX_ACCOUNT_TYPE);
       }
    }

    cs_get_optional_parameter("expire", expire, MAX_PATH);

}


void
init(char *data[MAX_NUM_PARAM_ID], int len, int max)
{
    int i;
    for(i =0;i<len; i++)
    {
        data[i] = (char *)malloc(sizeof(char) * max);
        data[i][0] = '\0';
     }

}
void
free_char(char *data[MAX_NUM_PARAM_ID])
{
    int i;
    for(i =0; i<MAX_NUM_PARAM_ID; i++)
    {
          if(data[i])
          free(data[i]);
    }
}
void
print_select_page(char *id[MAX_NUM_PARAM_ID], char *type[MAX_NUM_PARAM_ID], int count, char *expire)
{
     int i;
     send_content_type();
     puts("<html><head><title>Manhattan select page</title></head>\n");
     puts("<body><br /><br /><br />");
     for(i =0; i<count; i++)
     {
	if(!strcmp(type[i], "faculty"))
	  {
	      printf("<a href=\"%s?id=%s&log=1&expire=%s\" title=\"Enter your teacher account\">", 
			   "mymanhattan", id[i], expire);
	      printf("<h4 align=\"center\">Enter your teacher account</h4></a>");
	  }
	else if(!strcmp(type[i], "student"))
	  {
	      printf("<a href=\"%s?id=%s&log=1&expire=%s\" title=\"Enter your student account\">", 
			   "mymanhattan", id[i], expire);
	      printf("<h4 align=\"center\">Enter your student account</h4></a>");
          }
	else
	{
	      printf("<a href=\"%s?id=%s&log=1&expire=%s\" title=\"Enter your staff account\">", 
			   "mymanhattan", id[i], expire);
	      printf("<h4 align=\"center\">Enter your staff account</h4></a>");
	}
     }
     puts("</body></html>");
}

int
main()
{
   char *id[MAX_NUM_PARAM_ID];
   char *type[MAX_NUM_PARAM_ID];
   int count;
   char expire[MAX_PATH +1];

   cs_cgi_init();

   init(id, MAX_NUM_PARAM_ID, MAX_KEY +1);
   init(type, MAX_NUM_PARAM_ID, MAX_ACCOUNT_TYPE +1);
   read_parameters(&count,  id, type, expire); 

   if(count ==0)
   {
     cs_critical_error(ERR_NO_COURSE_ON_MANHATTAN, "");
     free_char(id);
     free_char(type);
   }
   else if(count == -1)
   {
     cs_critical_error(ERR_SERVER_LOCK_MESSAGE, "");
     free_char(id);
     free_char(type);
   }

   else if(count==1)
   {
     printf("Location: %s?id=%s&log=1&expire=%s\n\n", "mymanhattan", id[0], expire);
     free_char(id);
     free_char(type);
   }
   else if(count >=2 && count <= 3)
   {
     print_select_page(id, type, count, expire);
     free_char(id);
     free_char(type);
   }
   else
   {
     cs_critical_error(ERR_REQUEST_DENIED, "The page can not be displayed"); 
     free_char(id);
     free_char(type);
   }
   cs_cgi_destroy();
   return 0;
}
