#include <stdio.h>
#include "moviedb.h"
#include "searchtitle.h"

#ifdef NAME_COMPLETION
#include "completion.h"
#endif

#include "searchHistory.h"

#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/AsciiText.h>

#include "lookuptitle.h"
#include "xregal.h"
#include "popup.h"
#include "RadioBtn.h"

#define DIALOG_WIDTH 400

char *searchtitle;

Widget 	ts_shell,
		ts_form,
		ts_box,
		ts_search,
		ts_cancel,
		tse_label,
		tse_text;

Widget 	tst_label,
		ts_type,
		tst_prefix,
		tst_substring;

Boolean ts_popped_up=False;

static void Cancel(Widget w, XtPointer client_data, XtPointer call_data) {
	enableSearch();
	XtPopdown(ts_shell);
	ts_popped_up=False;

	/* Cancel any substring search in progress */
	if (movie_substring_stage>0)
		movie_substring_stage=-1;
	search_in_progress=False; /* redundant? reset in work procedure? */
	}

static void Search(Widget w, XtPointer client_data, XtPointer call_data) {
	Boolean found;
	int searchtype;

    XDefineCursor(XtDisplay(topLevel), XtWindow(ts_form), busyCursor);
    XFlush(XtDisplay(topLevel));

	XtVaGetValues(ts_type, XtNselected, &searchtype, NULL);
	XtVaGetValues(tse_text, XtNstring, &searchtitle, NULL);
	AddStringToHistory(tse_text, searchtitle);

	/* guard against dumb substring searches */
	if ((searchtype==SEARCH_SUBSTRING) && (strlen(searchtitle)<2)) {
		Alert("Substring search term must be at least 2 characters long!");
		XBell(XtDisplay(topLevel), 100);
	    XDefineCursor(XtDisplay(topLevel), XtWindow(ts_form), unBusyCursor);
		return;
		}
	found=LookupTitle(searchtitle, searchtype, False);

    XDefineCursor(XtDisplay(topLevel), XtWindow(ts_form), unBusyCursor);
   	XFlush(XtDisplay(topLevel));

	if (searchtype==SEARCH_SUBSTRING)
		return;
	if (!found) {
		Alert("No matches found!");
		XBell(XtDisplay(topLevel), 100);
		}
	}

void CreateTitleSearchDialog(void) {
    static String myreturn = "#override <Key>Return:   set() notify() unset()\n";
    static String myescape = "#override <Key>Escape:   set() notify() unset()\n";
    XtAccelerators accel, escaccel;

    accel = XtParseAcceleratorTable(myreturn);
    escaccel = XtParseAcceleratorTable(myescape);


	ts_shell=XtVaCreatePopupShell(
                "ts_shell",
                transientShellWidgetClass,
                topLevel,
                XtNallowShellResize, False,
                NULL);

	ts_form=XtVaCreateManagedWidget("ts_form",formWidgetClass,ts_shell, NULL);
	ts_box=XtVaCreateManagedWidget("ts_box", boxWidgetClass, ts_form, 
		XtNwidth, 400,
		XtNborderWidth, 0,
		NULL);
	ts_search=XtVaCreateManagedWidget("ts_search",commandWidgetClass,ts_box,
		XtNlabel, "Do Search",
        XtNaccelerators, accel,
		NULL);
	ts_cancel=XtVaCreateManagedWidget("ts_cancel",commandWidgetClass,ts_box,
		XtNlabel, "Cancel",
        XtNaccelerators, escaccel,
		NULL);

	tse_label=XtVaCreateManagedWidget("tse_label",labelWidgetClass,ts_form,
		XtNfromVert, ts_box,
        XtNwidth, 100,
		XtNborderWidth, 0,
		XtNjustify, XtJustifyLeft,
		NULL);
	tse_text = XtVaCreateManagedWidget("tse_text",asciiTextWidgetClass,ts_form,
        XtNeditType, XawtextEdit,
        XtNwidth, DIALOG_WIDTH-100,
        XtNresizable, True,
		XtNfromVert, ts_box,
		XtNfromHoriz, tse_label,
        XtNstring, searchtitle,
        XtNuseStringInPlace, False,
        NULL);

#ifdef NAME_COMPLETION
	if (strcmp(app_data.nameCompletion, "off") != 0)
		addTitleCompletion();
#endif

	AddHistoryText(tse_text);

	tst_label=XtVaCreateManagedWidget("tst_label",labelWidgetClass,ts_form,
		XtNfromVert, tse_text,
		XtNlabel, "Search Type:",
		XtNvertDistance, 15,
		XtNborderWidth, 0,
		XtNwidth, 100,
		XtNjustify, XtJustifyLeft,
		NULL);
	ts_type=XtVaCreateManagedWidget("ts_type",radioBtnWidgetClass,ts_form,
		XtNfromVert, tse_text,
		XtNfromHoriz, tst_label,
		XtNvertDistance, 15,
		XtNbuttons, "Exact|Substring",
		XtNwidth, 120,
		NULL);

    XtAddCallback(ts_cancel, XtNcallback, Cancel, 0);
    XtAddCallback(ts_search, XtNcallback, Search, 0);
    XtSetKeyboardFocus(ts_form, tse_text);
    XtInstallAccelerators(tse_text, ts_search);
    XtInstallAccelerators(tse_text, ts_cancel);
	}

void PopupSearchTitle(void) {
	disableSearch();
	XtVaSetValues(tse_text, XtNstring, NULL, NULL);
	
	SetPopupPosition(ts_shell, topLevel);
	XtPopup(ts_shell, XtGrabNone);
	ts_popped_up=True;
	ResetHistory(tse_text);
	}
