#include <stdio.h>
#include "moviedb.h"
#include "dbutils.h"
#include "searchname.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 <X11/Xaw/Dialog.h>

#include "xregal.h"
#include "lookupname.h"
#include "popup.h"
#include "CheckBox.h"
#include "RadioBtn.h"

#define DIALOG_WIDTH 400

static char *searchname;

Widget 	ns_shell,
		ns_form,
		ns_box,
		ns_search,
		ns_cancel,
		nse_label,
		nse_text;

Widget 	nst_label,
		ns_type;

Widget ns_toggles[NO_OF_FILMOGRAPHY_LISTS];

Boolean ns_popped_up=False;

static void Sensify(Widget w, XtPointer client_data, XtPointer call_data) {
	int s = (int) call_data;
	Boolean sensitive;
	int i;

	sensitive = (s==SEARCH_SUBSTRING_SELECTED);
	for (i=0; i<NO_OF_FILMOGRAPHY_LISTS; i++)
		XtVaSetValues(ns_toggles[i], XtNsensitive, sensitive, NULL);
	}

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

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


static void Search(Widget w, XtPointer client_data, XtPointer call_data) {
	int lists[NO_OF_FILMOGRAPHY_LISTS];
	Boolean st, found;
	int i;
	int searchtype;

	XtVaGetValues(ns_type, XtNselected, &searchtype, NULL);
	XtVaGetValues(nse_text, XtNstring, &searchname, NULL);
	AddStringToHistory(nse_text, searchname);

	/* guard against dumb substring searches */
	if ((searchtype!=SEARCH_EXACT) && (strlen(searchname)<2)) {
		Alert("Substring search term must be at least 2 characters long!");
		XBell(XtDisplay(topLevel), 100);
		return;
		}

	if (searchtype==SEARCH_SUBSTRING_SELECTED) 
		for (i=0; i<NO_OF_FILMOGRAPHY_LISTS; i++) {
			XtVaGetValues(ns_toggles[i], XtNstate, &st, NULL);
			lists[i]=st;
			}
	else
		for (i=0; i<NO_OF_FILMOGRAPHY_LISTS; i++) 
			lists[i]=1;

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

	found=LookupName(searchname, lists, False, searchtype);

    XDefineCursor(XtDisplay(topLevel), XtWindow(ns_form), unBusyCursor);
    XFlush(XtDisplay(topLevel));
	
	if (searchtype==SEARCH_SUBSTRING)
		return;

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

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

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

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

	ns_form=XtVaCreateManagedWidget("ns_form",formWidgetClass,ns_shell, 
		NULL);
	ns_box=XtVaCreateManagedWidget("ns_box", boxWidgetClass, ns_form, 
		XtNwidth, DIALOG_WIDTH,
		XtNborderWidth, 0,
		NULL);
	ns_search=XtVaCreateManagedWidget("ns_search",commandWidgetClass,ns_box,
		XtNlabel, "Do Search",
        XtNaccelerators, accel,
		NULL);
	ns_cancel=XtVaCreateManagedWidget("ns_cancel",commandWidgetClass,ns_box,
		XtNlabel, "Cancel",
        XtNaccelerators, escaccel,
		NULL);

	nse_label=XtVaCreateManagedWidget("nse_label",labelWidgetClass,ns_form,
		XtNfromVert, ns_box,
		XtNwidth, 120,
		XtNborderWidth, 0,
		XtNjustify, XtJustifyLeft,
		NULL);
	nse_text = XtVaCreateManagedWidget("nse_text",asciiTextWidgetClass,ns_form,
        XtNeditType, XawtextEdit,
        XtNwidth, DIALOG_WIDTH-120,
        XtNresizable, True,
        XtNfromHoriz, nse_label,
		XtNfromVert, ns_box,
        XtNstring, searchname,
        XtNuseStringInPlace, False,
        NULL);

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

	AddHistoryText(nse_text);

	nst_label=XtVaCreateManagedWidget("nst_label",labelWidgetClass,ns_form,
		XtNfromVert, nse_label,
		XtNlabel, "Search Type:",
		XtNvertDistance, 15,
		XtNborderWidth, 0,
		XtNwidth, 120,
		XtNjustify, XtJustifyLeft,
		NULL);
	ns_type=XtVaCreateManagedWidget("ns_type",radioBtnWidgetClass,ns_form,
		XtNfromHoriz, nst_label,
		XtNfromVert, nse_label,
		XtNvertDistance, 15,
		XtNbuttons, "Exact|Substring - All Lists|Substring - Selected Lists",
		XtNwidth, 120,
		NULL);

	last_widget=ns_type;
	x=25;
	for (i=0; i<NO_OF_FILMOGRAPHY_LISTS; i++) {
		ns_toggles[i]=XtVaCreateManagedWidget(filmographyDefs[i].filmography,checkBoxWidgetClass,ns_form,
			XtNfromVert, last_widget,
			XtNhorizDistance, x,
			XtNlabel, filmographyDefs[i].filmography,
			XtNsensitive, False,
			XtNwidth, 140,
			NULL);
		if (i == (NO_OF_FILMOGRAPHY_LISTS-1)/2) {
			last_widget=ns_type;
			x=DIALOG_WIDTH/2;
			}
		else
			last_widget=ns_toggles[i];
		}

    XtAddCallback(ns_cancel, XtNcallback, Cancel, 0);
    XtAddCallback(ns_search, XtNcallback, Search, 0);
    XtAddCallback(ns_type, XtNradioCallback, Sensify, 0);
    XtSetKeyboardFocus(ns_form, nse_text);
    XtInstallAccelerators(nse_text, ns_search);
    XtInstallAccelerators(nse_text, ns_cancel);
	}

void PopupSearchName(void) {	
	disableSearch();
	XtVaSetValues(nse_text, XtNstring, NULL, NULL);
	SetPopupPosition(ns_shell, topLevel);
	XtPopup(ns_shell, XtGrabNone);
	ns_popped_up=True;
	ResetHistory(nse_text);
	}
