#include <stdio.h>
#include <time.h>
#include <fstream.h>
#include "frontend.h"
#include "Position.h"
#include "resource.h"
#include "Backend.h"

Controls Ctrs;

void Controls::StatPrintf(const char * szFmt,...) {
		va_list lst;
		va_start(lst,szFmt);
		vsprintf(buf,szFmt,lst);
		va_end(lst);
		PostStatMsg(buf);
}

void Controls::PostStatMsg(char* szStatStr) {
	PostMessage(GetStatText(),SB_SETTEXT,
		0 ,(LPARAM) (LPSTR) szStatStr );
}

void Controls::PostDefStatMsg( void ) {
	PostStatMsg(
	"Train : Introduce a new face (person) to program \t Recognize : Recognize a known person \t Advanced : Feature Extraction.   \
	");
}

void Controls::Init(HWND _hWnd,HINSTANCE _hInst) {
	hWnd = _hWnd;
	hInst = _hInst;

	int x , y , xSz , ySz ;

	Pos.GetCtrPos(x,y,xSz,ySz,Postion::Train);

	hWndTrain = CreateWindow(/* L */ "BUTTON",/* L */ "Train",
	WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP|WS_GROUP,
	x,y,xSz,ySz,hWnd,(HMENU)(TRAIN_BTN), hInst,NULL);

	Pos.GetCtrPos(x,y,xSz,ySz,Postion::Recog);

	hWndRecogn = CreateWindow(/* L */ "BUTTON",/* L */ "Recognize",
	WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP,
	x,y,xSz,ySz,hWnd,(HMENU)(RECOGN_BTN), hInst,NULL);
	
	Pos.GetCtrPos(x,y,xSz,ySz,Postion::Advanced);

	hWndFExt = CreateWindow(/* L */ "BUTTON",/* L */ "Advanced",
	WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP,
	x,y,xSz,ySz,hWnd,(HMENU)(FEXT_BTN), hInst,NULL);
		
	Pos.GetCtrPos(x,y,xSz,ySz,Postion::Exit);

	hWndExit = CreateWindow(/* L */ "BUTTON",/* L */ "Exit",
	WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE|WS_TABSTOP,
	x,y,xSz,ySz,hWnd,(HMENU)(EXIT_BTN), hInst,NULL);

	 hWndStat = CreateWindowEx( 
       0,                       // no extended styles 
       STATUSCLASSNAME,         // name of status bar class 
       (LPCTSTR) NULL,          // no text when first created 
       WS_CHILD & ~SBARS_SIZEGRIP,                // creates a child window 
       0, 0, 0, 0,              // ignores size and position 
       hWnd,              // handle to parent window 
       (HMENU) (STATUS_TEXT),       // child window identifier 
       hInst,                   // handle to application instance 
       NULL);            

	SendMessage(hWndStat,SB_SETMINHEIGHT ,40,0);

	ShowWindow(hWndStat,SW_SHOWMAXIMIZED );

	PostDefStatMsg();

}

Controls::~Controls() {
}



void Controls::DisableControls () {
	ShowWindow(GetTrainBtn(),SW_HIDE);
	ShowWindow(GetRecognBtn(),SW_HIDE);
	ShowWindow(GetFextBtn(),SW_HIDE);
}

void Controls::EnableControls () {
	ShowWindow(GetTrainBtn(),SW_RESTORE);
	ShowWindow(GetRecognBtn(),SW_RESTORE);
	ShowWindow(GetFextBtn(),SW_RESTORE);
}

char szName[200]; // receives name 
char szPath[200];
BOOL CALLBACK EditProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
        case WM_COMMAND: 
            switch (LOWORD(wParam)) 
            { 
                case IDOK: 
                    if (!GetDlgItemText(hwndDlg, IDC_EditName, 
                             szName, 80)) 
                         *szName=0; 
					if (!GetDlgItemText(hwndDlg, IDC_EditPath, 
                             szPath, 80)) 
                         *szPath=0; 
 
                    // Fall through. 
 
                case IDCANCEL: 
                    EndDialog(hwndDlg, wParam); 
                    return TRUE; 
            } 
    } 
    return FALSE; 
} 

DWORD WINAPI TrnThreadProc(LPVOID lpParameter) {
	// BackEndTrainStuff();
	Ctrs.DisableControls();

	char Name[80];
	sprintf(Name,"Train\\%s",szName);
	BTrain(Name);

	Ctrs.EnableControls();

	return 0;
}

void OnTrain () {

	// Ctrs.PostStatMsg("\tTraining ...");

	if(DialogBox(Ctrs.GethInst(),MAKEINTRESOURCE(IDD_Train),
		Ctrs.GetMainWin(),(DLGPROC)EditProc) == IDOK) {
		char buf[128];
		sprintf(buf,".\\Train\\%s",szName);
		CreateDirectory(buf,NULL);

		char Temp[200];
		sprintf(Temp,"Train\\%s\\Train.jpg",szName);
		/*system(Temp);*/
		CopyFile(szPath,Temp,FALSE);
		
		fstream Txt;
		Txt.open("Train\\Trained.txt",ios::app);
		Txt << szName << "\n";
		Txt.close();

		DWORD ThreadId;
		//TrnThreadProc(NULL);
		CreateThread(NULL,0,TrnThreadProc,NULL,0,& ThreadId);
	}
}

DWORD WINAPI RecThreadProc(LPVOID lpParameter) {
	// BackEndTrainStuff();
	Ctrs.DisableControls();

	char Msg[200];
	char Name[80];
	BRecog(Name);

	sprintf(Msg,"%s is the closest match ",Name);
	MessageBox(Ctrs.GetMainWin(),Msg,"Match",MB_OK);

	Ctrs.EnableControls();

	return 0;
}

void OnRecog () {
	DWORD ThreadId;
	CreateThread(NULL,0,RecThreadProc,NULL,0,&ThreadId);
	// Ctrs.PostStatMsg("\tRecognizing ...");
}

void OnFext  () {
	// Ctrs.PostStatMsg("\tFexting ...");
}

void OnSplash() {
}

void OnCreate() { 
	// Any Startup code ...
	WIN32_FIND_DATA FndDt;
	if(FindFirstFile("Nw\\PixEye.dat",&FndDt) == INVALID_HANDLE_VALUE ) {
		TrainNetwork("eye","Eyes\\",22,"jpg",80,50,0.001,0.01,8);
	}
}
