#include <windows.h>
#include <gdiplus.h>
#include "frontend.h"
#include "OnPaint.h"
#include "Position.h"
#include "Image.h"

#pragma warning( disable : 4244 ) 

using namespace Gdiplus;

OnPnt cOnPaint;

OnPnt::OnPnt() {
	State = none;
}

void OnPnt::OnPaint () {
	int x1 , y1 , x2 , y2;

	Graphics graphics(hDc);

	// Draw the 4 rectangles
	for ( int i = 0 ; i < 4 ; i ++ ) {

		Pos.GetRectPos(x1,y1,x2,y2,i);
		Rectangle(hDc , x1 , y1 , x2 , y2);

	}
}

void OnPnt::OnRecogPaint() {
	// Training Paint stuff
	strcpy(szPath,"Recog.jpg");
	OnTrainInit();
	OnTrainPaint();
	if( szMatching[0] != 0 ) {
		
		ImageManip Im;
		WCHAR * wzMatching = Im.ToUnicodeFR(szMatching);

		Bitmap Bmp ( wzMatching );
		delete [] wzMatching ;

			GetThumbnailImageAbort ImgDs = 0;
	
		int iWidth , iHeight;
		Pos.GetThumbSz(iWidth,iHeight);

		int x1 , y1 , x2 , y2 , offx , offy;
		Pos.GetRectPos(x1,y1,x2,y2,1);
		Pos.GetThumbOffset(offx,offy);

		Graphics graphics(hDc);

		Image *	InpImg = Bmp.GetThumbnailImage(iWidth,iHeight,ImgDs,NULL);
		graphics.DrawImage(InpImg,x1+offx,y1+offy,iWidth,iHeight);

	}
}

void OnPnt::OnTrainInit() {
	Graphics graphics(hDc);
	
	ImageManip im;

	WCHAR * wzName = im.ToUnicodeFR(szPath);
	Bitmap b( wzName );
	delete [] wzName;

	GetThumbnailImageAbort ImgDs = 0;
	
	int iWidth , iHeight;
	Pos.GetThumbSz(iWidth,iHeight);

	int x1 , y1 , x2 , y2 , offx , offy;
	Pos.GetRectPos(x1,y1,x2,y2,0);
	Pos.GetThumbOffset(offx,offy);

	Image *	InpImg = b.GetThumbnailImage(iWidth,iHeight,ImgDs,NULL);
	graphics.DrawImage(InpImg,x1+offx,y1+offy,iWidth,iHeight);
}

void OnPnt::OnTrainPaint() {

	// This code is just for testing ... will go

	if ( sx == -1 )
		return;

	int x1 = sx ;
	int y1 = sy ;
		
	int x3 = x1 + ( 80 / Pos.GetDivFac());
	int y3 = y1 + ( 50 / Pos.GetDivFac());

	int x2 = x3;
	int y2 = y1;

	int x4 = x1;
	int y4 = y3;

	POINT ourrect[] = {
		{x1,y1},
		{x2,y2},
		{x3,y3},
		{x4,y4},
		{x1,y1},
	};

	HPEN hpen, hpenOld;

	// Create a RED pen.
    hpen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));

	// Bring it into action
	hpenOld = (HPEN)SelectObject(hDc, hpen);

	Polyline(hDc,ourrect,5);

	SelectObject(hDc, hpenOld);
    DeleteObject(hpen);

}

void OnPnt::Paint(HDC _hDc) {
	
	static int PrevState = State;

	hDc = _hDc;
	
	OnPaint();
	if(State == Train) {
		OnTrainInit();
		OnTrainPaint();
	}
	if(State == Recog)
		OnRecogPaint();

	PrevState = State;
}

void OnPnt::SetState( int _State ) {
	State = _State;

	if(State == Train) {
		int x1,y1,x2,y2,ox,oy;
		
		Pos.GetRectPos(x1,y1,x2,y2,0);
		Pos.GetThumbOffset(ox,oy);

		RECT rec;

		rec.top = y1;
		rec.left = x1;
		rec.right = x2;
		rec.bottom = y2;

		InvalidateRect(Ctrs.GetMainWin(),&rec,FALSE);
	}
}

void OnPnt::SetBlock( int _sx , int _sy ) {

	static RECT prevrec = {0,0,0,0};

	if( _sx == -1 ) {
		sx = -1;
		InvalidateRect(Ctrs.GetMainWin(),&prevrec,FALSE);
		return;
	}

	int x1,y1,x2,y2,ox,oy;
	
	Pos.GetRectPos(x1,y1,x2,y2,0);
	Pos.GetThumbOffset(ox,oy);

	sx = ( _sx / Pos.GetDivFac() + ox ) + x1;
	sy = ( _sy / Pos.GetDivFac() + oy ) + y1;
	
	
	RECT rec;
	rec.left = sx - 6;
	rec.top =  sy - 6;
	rec.right = rec.left + ( 80 / Pos.GetDivFac())+12;
	rec.bottom = rec.top + ( 50 / Pos.GetDivFac())+12;

	InvalidateRect(Ctrs.GetMainWin(),&rec,FALSE);
	InvalidateRect(Ctrs.GetMainWin(),&prevrec,FALSE);

	prevrec = rec;
}

void OnPnt::OnRecogInit() {
	szMatching[0] = 0;
}

void OnPnt::Matching( char nam[] ) {
	
	strcpy(szMatching,nam);
	int x1,y1,x2,y2;
	Pos.GetRectPos(x1,y1,x2,y2,1);

	RECT Rect;
	
	Rect.left = x1;
	Rect.top  = y1;

	Rect.right  = x2;
	Rect.bottom = y2;

	InvalidateRect(Ctrs.GetMainWin(),&Rect,FALSE);
}


VOID OnPaint(HDC hDc)
{	
	cOnPaint.Paint(hDc);
}