/*
  
Title:	MsgEd
  
File:	Showmail.c
  
Author:	Jim Nutt
	
Copr:	1987 by Jim Nutt
  
Description:
	
	routines to display messages
	
Revision History:
  
	0.00	14 December 1987
	
Support Files:
  
	msged.h
  
*/


#include "msged.h"

LINE   *top = NULL, *bottom = NULL;

extern LINE *anchorpt;

static void up(void);
static void down(void);
static void pgdown(void);
static void pgup(void);

void    refresh(LINE * c, int y);

int     showmsg(m)
	MSG     m;

{
	int     command;

	showheader(m);

	refresh(msgbuf.first, 1);

	for (;;) {
                gotoxy(1,maxy);
                video_update();
		switch (command = getkey()) {

                case PGUP:
                        if (msgbuf.first != NULL)
                                pgup();
			break;

                case UP:
                        if (msgbuf.first != NULL)
                                up();
			break;

                case SPACE:
                case PGDN:
                        if (msgbuf.first != NULL)
                                pgdown();
			break;

                case DOWN:
                        if (msgbuf.first != NULL)
                                down();
			break;

		default:
			return (command);
		}
	}
}

void    showheader(MSG m)
{
        clrwnd(1, 1, maxx, 6);

	gotoxy(1, 1);

	bprintf("%03d/%03d %s", m.msgnum, arealist[area].last, m.header.date);

	if (m.header.reply)
		bprintf("  Reply to #%d", m.header.reply);

	if (m.header.up)
		bprintf("  See #%d", m.header.up);

	gotoxy(1, 2);
	bprintf("From:   %s", m.header.from);

	if (arealist[area].netmail) {
		bputs(" of ");

		if (m.from.zone != thisnode.zone)
			bprintf("%d:", m.from.zone);

		bprintf("%d/%d.%d", m.from.net, m.from.node, m.from.point);
                if ((m.from.domain != NULL) && (m.to.domain != NULL))
			bprintf("@%s",m.from.domain);
	}

	gotoxy(1, 3);

	bprintf("To:     %s", m.header.to);

	if (arealist[area].netmail) {
		bputs(" of ");

		if (m.to.zone != thisnode.zone)
			bprintf("%d:", m.to.zone);

		bprintf("%d/%d.%d", m.to.net, m.to.node, m.to.point);
                if ((m.to.domain != NULL) && (m.from.domain != NULL))
			bprintf("@%s",m.to.domain);
	}

	gotoxy(1, 4);

	if (m.header.attached)
		bprintf("Files:  %s", m.header.subj);
	else
		bprintf("Subj:   %s", m.header.subj);

	gotoxy(1, 5);

	bputs("Attr:   ");

	if (m.header.private)
                bputs("privileged ");
	if (m.header.crash)
		bputs("crash ");
	if (m.header.recvd)
		bputs("recvd ");
	if (m.header.sent)
		bputs("sent ");
	if (m.header.attached)
		bputs("f/a ");
	if (m.header.killsent)
		bputs("kill/sent ");
	if (m.header.freq)
		bputs("freq ");
	if (m.header.ureq)
		bputs("ureq ");
        if (m.header.hold)
                bputs("hold ");
	if (m.header.orphan)
		bputs("orphan ");
	if (m.header.forward)
		bputs("in transit ");
	if (m.header.local)
		bputs("local ");
	if (m.header.xx2)
		bputs("xx2 ");
	if (m.header.rreq)
		bputs("rreq ");
	if (m.header.rcpt)
		bputs("rcpt ");
	if (m.header.areq)
		bputs("areq ");

	gotoxy(1, 6);
	bputs(
	      "______________________________________________________________________________"
		);
	gotoxy(1, 6);
	foreground(normback);
	background(normfore);
	bputs(arealist[area].description);
	foreground(normfore);
	background(normback);
}

void    up()
{
        int     i = 1;

	while (top->prev) {
		top = top->prev;
		if (shownotes || (*(top->text) != '\01')) {
			scrolldown(1, 7, maxx, maxy, 1);
			gotoxy(1, 7);
			bputs(top->text);
			break;
		}
	}

	for (bottom = top;
	     (bottom->next != NULL) && (i < (maxy - 6));
	     bottom = bottom->next)
		if ((*(bottom->text) != '\01') || shownotes)
			i++;
}

void    down()
{
        int     i = 1;

	while (bottom->next) {
		bottom = bottom->next;
		if (shownotes || (*(bottom->text) != '\01')) {
			scrollup(1, 7, maxx, maxy, 1);
			gotoxy(1, maxy);
			bputs(bottom->text);
			break;
		}
	}

	for (top = bottom; (top->prev != NULL) && (i < (maxy - 6)); top = top->prev)
		if ((*(top->text) != '\01') || shownotes)
			i++;
}

void    pgup()
{
	int i = 0;
	
	if ((top->prev == NULL) || ((*(top->prev->text) == 1) && !shownotes))
		return;

	for (bottom = top; (top->prev != NULL) && (i < (maxy - 6)); top = top->prev)
		if ((*(top->text) != '\01') || shownotes)
			i++;

	refresh(top, 1);
}

void    pgdown()
{
	int     i = 1;

	if ((bottom->next == NULL) || ((*(bottom->next->text) == 1) && !shownotes))
		return;

	clrwnd(1, 7, maxx, maxy);

	for (top = bottom; (bottom->next != NULL) && (i < (maxy - 6)); bottom = bottom->next) {
		if ((*(bottom->text) != '\01') || shownotes) {
			gotoxy(1, 6 + i++);
			bputs(bottom->text);
		}
	}
	if ((*(bottom->text) != '\01') || shownotes) {
		gotoxy(1, 6 + i);
		bputs(bottom->text);
	}
}

void    refresh(LINE * c, int y)
{
	int     i = y;

	top = bottom = c;
	clrwnd(1, (i + 6), maxx, maxy);

	if ((top == NULL) || (top->text == NULL))
		return;

	while ((top != NULL) && ((*top->text == '\01') && !shownotes))
		top = top->next;

	if (top == NULL)
		return;

        for (bottom = top; (bottom->next != NULL) && (i < (maxy - 6)); bottom = bottom->next) {
		if (bottom->block)
			intense(ON);
		else
			intense(OFF);
		if ((*(bottom->text) != '\01') || shownotes) {
			gotoxy(1, 6 + i++);
			bputs(bottom->text);
		}
	}
	
	if (bottom->block)
		intense(ON);
	else
		intense(OFF);

	if ((*(bottom->text) != '\01') || shownotes) {
		gotoxy(1, 6 + i);
		bputs(bottom->text);
	}
}
