#include "screen.h"
#include <fstream.h>
#include <iostream.h>

#define move(y, x)      wmove(stdscr, y, x)
#define clear()         wclear(stdscr)

extern char *logfilename;


Screen::Screen( bool quiet ) {
    do_output = true;
        
    if( quiet ) {
	do_output = false;
	return;
    }

    initscr();
    cbreak();
    noecho();
    nonl();
    intrflush(stdscr, FALSE);
    keypad(stdscr, TRUE);

    attron(A_REVERSE);
    for (int i = 0; i<80; i++) {
	mvaddch(0,i,' '); mvaddch(2,i,' ');
	mvaddch(5,i,' '); mvaddch(8,i,' ');
	mvaddch(12,i,' ');
    }
    mvaddch(9,30,' ');
    mvaddch(9,31,' ');
    for (int i = 0; i<=11; i++) {
	mvaddch(i,0,' '); mvaddch(i,79,' ');
    }
    attroff(A_REVERSE);
    mvaddstr(1,2,VERSION);
    mvaddstr(3,6,"News Server:");
    mvaddstr(4,8,"Newsgroup:");
    mvaddstr(6,6,"Last decode:");
    mvaddstr(7,7,"Encoded as:");
    mvaddstr(9,2,"Reading article:");
    mvaddstr(9,33,"Last article:");
    mvaddstr(10,10,"Subject:");
    mvaddstr(11,13,"Line:");

    scrollwindow = subwin( stdscr, 0,80,13,0);
    scrollok(scrollwindow, TRUE);
    wrefresh(scrollwindow);
    wscrl(scrollwindow, 1);

    wrefresh(scrollwindow);
    refresh();
}

Screen::~Screen() {
    if (!do_output) return;

    clear();
    refresh();
    move(0,0);
    endwin();
}

void Screen::fix_string(char *p) {

    char *j = p;

    while (*j) {
	if ((*j == '\r')||(*j == '\n')) *j = ' ';
	j++;
    }
}

void Screen::clearline(int which) {
    if (!do_output) return;

    for (int i = 19; i < 79; i++) 
	if (mvaddch(which,i,' ') == ERR) exit(-1);
}

void Screen::clearline(int which, int to) {
    if (!do_output) return;
    
    for (int i = 19; i < to; i++) 
	if (mvaddch(which,i,' ') == ERR) exit(-1);
}

void Screen::newsServer(char *name) {
    if (!do_output) return;

    clearline(3);
    mvaddstr(3,19,name); refresh();
}

void Screen::newsgroup(char *name) {
    if (!do_output) return;

    clearline(4);
    mvaddstr(4,19,name); refresh();
}

void Screen::msgID(char *id) {
    if (!do_output) return;

    clearline(9,30);
    mvaddstr(9,19,id); refresh();
}

void Screen::msgIDlast(char *id) {
    if (!do_output) return;

    //clearline(6,30);
    mvaddstr(9,47,id); refresh();
}

void Screen::subject(char *sub) {
    if (!do_output) return;

    fix_string(sub);

    clearline(10);

    if (strlen(sub) > 60) sub[59] = 0; 

    clearline(10);
    mvaddstr(10,19,sub); refresh();
}

void Screen::line(char *sub) {
    if (!do_output) return;

    fix_string(sub);

    clearline(11);

    if (strlen(sub) > 60) sub[59] = 0; 

    clearline(11);
    mvaddstr(11,19,sub); refresh();
}

void Screen::lastDecode(char *name) {
    if (!do_output) return;

    clearline(6);
    mvaddstr(6,19,name); refresh();
}

void Screen::encodedAs(char *name) {
    if (!do_output) return;

    clearline(7);
    mvaddstr(7,19,name); refresh();
}

void Screen::addTextToScrollWindow( char *text) {
    if (!do_output) return;

    if( text == NULL ) return;

    waddstr(scrollwindow, text);
    wrefresh(scrollwindow);

    ofstream tfile;
    if(logfilename != NULL) {
      tfile.open(logfilename,ios::app);
      tfile << text;
    }
    tfile.close();

}

void Screen::percentLeft( float percent ) {
    if (!do_output) return;

    char temp[20];
    sprintf(temp,"%f",percent);
    mvaddstr(0,0,temp); refresh();
}

void Screen::messageProgress( int iCurLine, int iLines ) {
    if (!do_output) return;

    char temp[40];
    clearline(11);
    
    if (iLines > 0) {
	sprintf(temp, "%i/%i (%2.2f\%)", iCurLine, iLines, 100*(float)iCurLine/(float)iLines);
	//while (strlen(temp) < sizeof(temp)) {
	//  strcat(temp," ");
	//}
	
	mvaddstr(11,19,temp);
    }
    refresh();
}



syntax highlighted by Code2HTML, v. 0.9.1