#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <conio.h>
#include <wslib.h>
#include <misclib.h>

#include "connap.h"
#include "io.h"
#include "connect.h"
#include "message.h"
#include "util.h"



void whois(char *nick)
{
   if (send_msg(MSG_CLIENT_WHOIS, nick))
               disconnect(ws_strerror());
}


void show_whois(char *data)
{
   regmatch_t pmatch[30];
   char *values[18], *time_str = (char*)malloc(256);
   char *tmp, *dtxt, *htxt;
   int i, j, r, len, link;
   int time, days, hours, minutes, seconds;
   char time_tmp[80];
       char *titles[] = {"Nick", "Level", "Time", "Channels", "Status",
               "Shared", "Downloads", "Uploads", "Connection", "Client",
               "Tot Downloads", "Tot Uploads", "IP Address", "Conn Port",
               "Data Port", "E-Mail", "Server"};

   enum {NICK, LEVEL, TIME, CHANNELS, STATUS, SHARED, DOWNLOADS,
         UPLOADS, CONNECTION, CLIENT, TOT_DOWNLOADS, TOT_UPLOADS,
         IP_ADDR, CONN_PORT, DATA_PORT, EMAIL, SERVER};

   r = regex(data, "^([^ ]+) \"?([^ \"]+)\"? ([-0-9]+) \"(.*)\" \"([^ ]+)\""
           " ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+) \"(.+)\" *([0-9]*)"
           " *([0-9]*) *([^ ]*) *([0-9]*) *([0-9]*) *([^ ]*) *([^ ]*)$",
           30, pmatch);

   if (r <= 0) {
               err_msg("show_whois: regex error");
               free(time_str);
               return;
   }

/*
       printf("whole match: %d %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);

   for (i = 0, j = 1; i < 17; i++, j++)
               printf("%d: %d %d\n", j, pmatch[j].rm_so, pmatch[j].rm_eo);
*/

   for (i = 0, j = 1; i < 17; i++, j++) {
               if (! (len = pmatch[j].rm_eo - pmatch[j].rm_so))
               break;

               switch (i) {
               case TIME:
                               tmp = (char*)calloc(len + 1, 1);
                               strncpy(tmp, data + pmatch[j].rm_so, len);
                               time = atol(tmp);
                               free(tmp);
                               days = time / 86400;
                               hours = (time / 3600) % 24;
                               minutes = (time / 60) % 60;
                               seconds = time % 60;
                               dtxt = "day";
                               if (days > 1)
                               dtxt = "days";
                               htxt = "hr";
                               if (hours > 1)
                               htxt = "hrs";
                               sprintf(time_str, "%d sec", seconds);
                               if (minutes) {
                               strcpy(time_tmp, time_str);
                               sprintf(time_str, "%d min  %s", minutes, time_tmp);
                               }
                               if (hours) {
                               strcpy(time_tmp, time_str);
                               sprintf(time_str, "%d %s  %s", hours, htxt, time_tmp);
                               }
                               if (days) {
                               strcpy(time_tmp, time_str);
                               sprintf(time_str, "%d %s  %s", days, dtxt, time_tmp);
                               }
                               values[i] = (char*)malloc(strlen(time_str) + 1);
                               strcpy(values[i], time_str);
                               break;
               case CONNECTION:
                               tmp = (char*)calloc(len + 1, 1);
                               strncpy(tmp, data + pmatch[j].rm_so, len);
                               link = atoi(tmp);
                               free(tmp);
                               values[i] = (char*)malloc(strlen(link_str[link]) + 1);
                               strcpy(values[i], link_str[link]);
                               break;
               default:
                               values[i] = (char*)calloc(len + 1, 1);
                               tmp = data + pmatch[j].rm_so;
                               if (i == CHANNELS) {
                               while (*tmp == ' ') {
                                               tmp++;
                                               len--;
                               }
                               }
                               strncpy(values[i], tmp, len);
               }
   }
   values[i] = NULL;

       tmp = (char*)malloc(4096);
       put_rcvd("", NORMAL_COLOR);
       for (j = 0; values[j]; j++) {
               sprintf(tmp, "%-15s%s", titles[j], values[j]);
               put_rcvd(tmp, INFO_COLOR);
       }
       put_rcvd("", NORMAL_COLOR);
       free(tmp);

   for (j = 0; j < i; j++)
       free(values[j]);
   free(time_str);
}


void show_whowas(char *data)
{
   char *values[4];
   char *tmp = (char*)malloc(4096);
   char time_str[80];
       char *titles[] = {"Nick", "Level", "Last Seen"};
   enum {NICK, LEVEL, LAST_SEEN, END};
   struct tm *tm;
   time_t time;
       int i;

       strcpy(tmp, data);

   values[NICK] = strtok(tmp, " ");
   values[LEVEL] = strtok(NULL, " ");
   if (*(values[LEVEL]) == '\"') {
               *(values[LEVEL] + strlen(values[LEVEL]) - 1) = 0;
               (values[LEVEL])++;
   }
   time = (time_t)atol(strtok(NULL, " "));
   tm = gmtime(&time);
   strftime(time_str, 80, "%Y-%m-%d  %H:%M", tm);
   values[LAST_SEEN] = time_str;
   values[END] = NULL;

       put_rcvd("", NORMAL_COLOR);
       for (i = 0; values[i]; i++) {
               sprintf(tmp, "%-15s%s", titles[i], values[i]);
               put_rcvd(tmp, INFO_COLOR);
       }
       put_rcvd("", NORMAL_COLOR);

       free(tmp);
}