#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <stdio.h>
#include <stdlib.h>

/* moviedb includes */
#include "moviedb.h"

/* xregal includes */
#include "xregal.h"
#include "history.h"
#include "printmovies.h"
#include "filmbuf.h"
#include "titlelist.h"
#include "LinkText.h"
#include "LinkView.h"

/* Displays a list of movies from which the user must choose. */

void displayListOfMovies(TitleRec *tchain, char *searchtext, int final_message) {
    TitleRec *trec ;
	struct akaRec *aka;
	char buf[128];
	char hangindent[16];

	sprintf(hangindent, "<h %d>", app_data.hangingIndent);

	ClearFilmbuf();
  	trec=tchain; 
	while (trec!=NULL) {
		AddToFilmbuf2(hangindent, "<w1>");
		AddToFilmbuf3("<l MOV:", trec->title, ">");
		AddToFilmbuf2(trec->title, "<e><w0>\n");

		/* display a.k.a.'s */
		for (aka=trec->aka; aka!=NULL; aka=aka->next) {
			AddToFilmbuf2("            ... aka <f2>", aka->akaTitle);
			if (aka->akaAttr != NULL)
				AddToFilmbuf2("  ", aka->akaAttr);
			AddToFilmbuf("<f1>\n");
			}

		if (trec!=NULL)
			trec = trec->next;
		}

	switch (final_message) {
		case SEARCH_OVERFLOW:
			sprintf(buf, "\n<f2>Maximum of %d movies reached.\n", app_data.substringMax);
			AddToFilmbuf(buf);
			break;
		case SEARCH_ABORTED:
			AddToFilmbuf("\n<f2>Search aborted.\n");
			break;
		default:
			/* do nothing */
			;
		}

	AddToFilmbuf3("\n<m1><f2>", COPYRIGHT, "<f1><m0>");

	historySavePosition();
	SetTitle(searchtext);
	XtVaSetValues(viewer, XtNhypertext, Filmbuf(), NULL);
	LinkViewReload(vport, 0, 0);
	XtVaSetValues(url_text, XtNstring, "", NULL);
	historyAdd(Filmbuf(), searchtext, NULL);
	}

