//
// Programmer: Andrew MacKenzie
//
// Date: 06/25/2001
//
// 06/25/2001
// Added getopt.h
// Added a return value to 'usage'

using namespace::std;

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

#include <ncurses.h>
#include <signal.h>
#include <time.h>
#include <vector>
#include <unistd.h>
#include <string>

#include <sys/time.h>
#include <sys/types.h>

// FreeBSD didn't like this...
#ifdef __linux__
#include <getopt.h>
#endif

#include "Frame.h"
#include "Util.h"

vector <char *> fileList;
vector<Frame> frameList;

#define VERSION "0.4.2"

#ifdef __DEBUG__
FILE * ferr=NULL;
#endif 

long delay;
long bufferSize;

extern char *optarg;
extern int optind, opterr, optopt;

string config;

int init()
{
    for (unsigned long i=0; i<fileList.size(); i++)
    {
        frameList.push_back( Frame(fileList[i],i, fileList.size(), config) );
    }

    delay=3; // Second(s)
    return 1;
}

void display()
{
    int c;
    int scrollUp=0;
    int scrollRight=0;
    int scrollLeft=0;
    int scrollDown=0;
    int reset_bottom=0;
    int reset_left=0;
    bool keyPressed=false;

    //nodelay(stdscr, TRUE);
    halfdelay(delay);
    keypad(stdscr, TRUE);

    while(1)
    {

        for (unsigned long i=0; i<frameList.size(); i++)
        {
            if ( scrollUp )
                frameList[i].incrScrollBack(scrollUp);
            if ( scrollDown )
                frameList[i].decrScrollBack(scrollDown);
            if ( scrollLeft )
                frameList[i].decrScrollRight(scrollLeft);
            if ( scrollRight )
                frameList[i].incrScrollRight(scrollRight);
            if ( reset_bottom )
                frameList[i].setScrollBack(0);
            if ( reset_left )
                frameList[i].setScrollRight(0);

            // Read returns 'true' if any new info is read.
            // Saves some bandwith if we are remote, by 
            // only redrawing if changes have been made.
            if (frameList[i].read() || keyPressed )
                frameList[i].print();
        }

        scrollUp = 0;
        scrollDown = 0;
        reset_bottom = 0;
        reset_left = 0;
        scrollRight = 0;
        scrollLeft = 0;
        keyPressed=false;

        if ( (c=getch()) != ERR )
        {
            // do something.
            // Handle our character.
            //fprintf(ferr,"Got : %d\n",c);
            switch(c)
            {
                case KEY_UP:
                    keyPressed=true;
                    scrollUp=1;
                    break;
                 case KEY_RIGHT:
                    keyPressed=true;
                    scrollRight=1;
                    break;
                case KEY_LEFT:
                    keyPressed=true;
                    scrollLeft=1;
                    break;
                case KEY_DOWN:
                    keyPressed=true;
                    scrollDown=1;
                    break;
                case KEY_NPAGE:
                    keyPressed=true;
                    scrollDown=frameList[0].height();
                    break;
                case KEY_PPAGE:
                    keyPressed=true;
                    scrollUp=frameList[0].height();
                    break;
                case KEY_HOME:
                    keyPressed=true;
                    reset_left=1;
                    break;
                case KEY_END:
                    keyPressed=true;
                    reset_bottom=1;
                    break;
                case ' ':
                    keyPressed=true;
                    reset_bottom=1;
                    reset_left=1;
                    break;
                case 'q':
                case 'Q':
                    clean_exit();
                default:
                    break;
            }
        }
    }

}

int usage(string prog)
{
    printf("Usage:\n\n\t%s [-v] [-b buffer_size] [-f config_file] <file1> <file2> ...\n\n", prog.c_str());
    return 0;
}

extern char *optarg;
extern int optind, opterr, optopt;

int main( int argc, char ** argv )
{
    (void) signal(SIGINT, sig_int);

    char c;

#ifdef __DEBUG__
    ferr = fopen("err.txt","w");
    // Set to non-buffered.
    setbuf(ferr,NULL);
#endif 

    // Set default values for command line args.
    bufferSize = 1000;

    while ( (c=getopt(argc, argv, "d:b:f:v")) != -1)
    {
        switch(c)
        {
            case 'd':
            case 'b':
                bufferSize=atoi(optarg);
                break;
            case 'v':
                printf("%s - %s\n",argv[0],VERSION);
                clean_exit();
            case 'f':
                config=optarg; // Set our config file name.
                break;
            default:
                printf("Unknown option <%c>\n",c);
                usage(argv[0]);
                clean_exit();
        }
    }
    // Get each file...
    if ( optind < argc )
    {

        for (int i=optind; i<argc; i++)
        {
            fileList.push_back(argv[i]);
        }
    }
    else
    {
        // No more 'dipshit', thought I'd be nice. :-)
        printf("No files!\n");
        usage(string(argv[0]));
        clean_exit();
    }

    // Initialize our ncurses window.
    initscr(); 
    cbreak(); noecho(); clear(); refresh();

    init();

    display();

    clean_exit();

    return 0;
}
