#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <wslib.h>
#include <misclib.h>

#include "io.h"
#include "connect.h"
#include "command.h"
#include "message.h"
#include "chat.h"


static int cur_y;

static char lbuf[500][160];

static int lcnt = 0;



void init_scr(void)
{
       _wscroll = 0;

       textbackground(BLUE);
       textcolor(WHITE);
       clrscr();

       textbackground(LIGHTCYAN);
       gotoxy(1, 1);
       clreol();
       gotoxy(1, 24);
       clreol();
       textbackground(BLUE);

       cur_y = 1;
       gotoxy(1, 25);
}


void err_msg(char *s)
{
       int old_x = wherex(), old_y = wherey();

       MessageBeep(MB_ICONEXCLAMATION);
       textbackground(LIGHTCYAN);
       textcolor(BLACK);
       gotoxy(2, 24);
       clreol();
       cprintf("ERROR: %s", s);
       _sleep(2);
       gotoxy(2, 24);
       clreol();
       textbackground(BLUE);
       textcolor(WHITE);
       gotoxy(old_x, old_y);
}


void show_info(char *s)
{
       int old_x = wherex(), old_y = wherey();

       textbackground(LIGHTCYAN);
       textcolor(BLACK);
       gotoxy(2, 1);
       clreol();
       cprintf(s);
       textbackground(BLUE);
       textcolor(WHITE);
       gotoxy(old_x, old_y);
}


static void put_line(char *s)
{
       if (++cur_y > 23) {
               movetext(1, 3, 80, 23, 1, 2);
               gotoxy(1, 23);
               clreol();
               cur_y--;
       }
       gotoxy(1, cur_y);
       cprintf("%s", s);

/*
       gettext(1, cur_y, 80, cur_y, lbuf[lcnt++]);
       if (lcnt == 500) {
               memcpy(buf[100], buf[0], 400 * 160);
               lcnt = 400;
       }
*/
}


void put_rcvd(char *text, int color)
{
       int i, old_x = wherex(), old_y = wherey();
       char *wtext, *tmp, *p1, *p2;

       wtext = new_string(text);
       textcolor(color);

   if (strchr(wtext, '\n') || (strlen(wtext) > 80)) {
               tmp = (char*)malloc(81);
               for (p1 = p2 = wtext; *p2; p2++) {
                       if ((*p2 == '\n') || ((p2 - p1) == 80)) {
                               if (*p2 == '\n') {
                                       *p2 = '\0';
                                       strcpy(tmp, p1);
                               } else {
                                       memset(tmp, 0, 81);
                                       strncpy(tmp, p1, p2 - p1);
                               }
                               put_line(tmp);
                               p1 = (*p2 == '\0') ? p2 + 1 : p2;
                       }
               }
               strcpy(tmp, p1);
               put_line(tmp);
               free(tmp);
   } else
               put_line(wtext);

       textcolor(WHITE);
       gotoxy(old_x, old_y);
       free(wtext);
}


static void get_input(void)
{
       char c;
       static int count = 0;
       static char tmp[256];

       c = getch();

       switch (c) {
               case 8 :
                       if (count) {
                               count--;
                               gotoxy(count + 1, 25);
                               putch(' ');
                               gotoxy(count + 1, 25);
                       }
                       break;

               case 9:
                       if (! count) {
                               if (priv_nick) {
                                       sprintf(tmp, "/msg %s ", priv_nick);
                                       gotoxy(count + 1, 25);
                                       cprintf(tmp);
                                       count += strlen(tmp);
                               }
                       }
                       break;

               case 13:
                       tmp[count] = 0;
                       if (strlen(tmp)) {
                               gotoxy(1, 25);
                               clreol();
                               count = 0;
                               cmd_parse(tmp);
                       }
                       break;

               default:
                       gotoxy(count + 1, 25);
                       if (count < 80) {
                               putch(c);
                               tmp[count++] = c;
                       }
       }
}


void chk_keyboard(void)
{
       char *s;

       if (kbhit())
               get_input();
}


void chk_sockets(void)
{
/*      DOWNLOAD *dl; */
       fd_set fds;
       struct timeval tv;
       int r, empty = 1;

       tv.tv_sec = 0;
       tv.tv_usec = 0;

       FD_ZERO(&fds);

       if (srv_sock != INVALID_SOCKET) {
               FD_SET(srv_sock, &fds);
               empty = 0;
       }

/*
       for (dl = downloads; dl; dl = dl->next) {
               FD_SET(dl->socket, &fds);
               empty = 0;
       }
*/

       if (empty)
               return;

       r = select(0, &fds, NULL, NULL, &tv);
       if (r == -1)
               err_msg(ws_strerror());
       else if (r > 0) {
               if (FD_ISSET(srv_sock, &fds))
                       get_msg();
/*
               for (dl = downloads; dl; dl = dl->next) {
                       if (FD_ISSET(dl->socket))
                               rcv_data(dl->socket);
               }
*/
       }
}