/*
 * NYCR UNIX 101 AD - Matt Joyce
 */

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <curses.h>

int current_getch;
int doloop = 1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;
char buffer[30];
struct timeval tv;
int msec, now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;
int rand(void);
int rand_r(unsigned int *seedp);
void srand(unsigned int seed);
int randint;
int jumper[23];
int first = 1;
int timermj = 0;
	
void screen_init(void) {
   mainwnd = initscr();
   if(has_colors() == FALSE)
	{	endwin();
		printf("Your terminal is color blind!\n");
		exit(1);
	}
   start_color();
   assume_default_colors(COLOR_YELLOW,COLOR_BLACK);
   init_pair(1, COLOR_RED, COLOR_BLACK);
   init_pair(2, COLOR_BLACK, COLOR_WHITE);
   noecho();
   cbreak();
   curs_set(0);
   nodelay(mainwnd, TRUE);
   refresh(); 
   wrefresh(mainwnd);
   screen = newwin(0, 0, 0, 0);
   box(screen, ACS_VLINE, ACS_HLINE);
}

static void update_display(void) {
   int acnt;
   if (first == 1){ 
      for (acnt = 0; acnt <= 23; acnt++) {
         randint = 1 + (int) (70.0 * (rand() / (RAND_MAX + 1.0)));
         jumper[acnt] = randint;
      }
      first = 2;
   }

   curs_set(0);
   box(screen, ACS_VLINE, ACS_HLINE);
 //  wattron(screen, COLOR_PAIR(2));
   int secf = msec / 300;
   int bcnt;
   for (acnt = 0; acnt <= 23; acnt++) {
 //      wattroff(screen, COLOR_PAIR(2));
     int ccnt[23];
     for (ccnt[acnt] = 0; ccnt[acnt] < jumper[acnt]; ccnt[acnt]++) {
        int b = ccnt[acnt] + 4;
        bcnt = acnt + 1;
        mvwprintw(screen,bcnt,b," ");
     }
     wattron(screen, COLOR_PAIR(2));

     secf = ( (timermj % 76) % ccnt[acnt] ) + 4 ;
     int scnt;
     for (scnt = 4; scnt < secf; scnt++) {
       bcnt = acnt + 1;
       mvwprintw(screen,bcnt,scnt," ");
     }
     wattroff(screen, COLOR_PAIR(2));
     if ((jumper[acnt] % ccnt[acnt] ) == 0) { 
       randint = 1 + (int) (70.0 * (rand() / (RAND_MAX + 1.0)));
       jumper[acnt] = randint;
     }
     sleep(.3);
     timermj++;
     wattron(screen, COLOR_PAIR(1));
     mvwprintw(screen,22,40, "NYCResistor UNIX/Linux Intro Class ");
     mvwprintw(screen,23,40, "Saturday March 21st 1:00 - 4:00 pm");
     mvwprintw(screen,24,40, "Come grok with us.");  
     wattroff(screen, COLOR_PAIR(1));
   }
   
/*
   mvwprintw(screen,7,6,"PRESS q TO END");
   mvwprintw(screen,10,1," -^v^-   UNIX 101  -^v^-");
*/
   wrefresh(screen);
   refresh();
   getch();
}

void screen_end(void) {
   endwin();
}

void maketime(void) {
	// Get the current date/time
	now = time (NULL);
	now_tm = localtime (&now);
        now_sec = now_tm->tm_sec;
	now_min = now_tm->tm_min;
	now_hour = now_tm->tm_hour;
	now_day = now_tm->tm_mday;
	now_wday = now_tm->tm_wday;
	now_month = now_tm->tm_mon + 1;
	now_year = now_tm->tm_year + 1900;
        time_t curtime;
        gettimeofday(&tv, NULL); 
        curtime=tv.tv_sec;
        strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
        msec = tv.tv_usec;       
}

int main(void) {
   screen_init();

   while (doloop) {
      current_getch = getch();
      if (current_getch == 113) {
         doloop = 0;
      }
      maketime();
      update_display();
 //     sleep(0.5);
   }
   screen_end();
   return 0;
}


