#pragma once

/**
Auto generer :D
**/
#include <xtl.h>
#include <xui.h>
#include <xuiapp.h>
#include <string>
#include <vector>
#include <algorithm> 
#include "Cemulator.h"
#include "Running.h"

extern "C" {
	#include "PlugCD.h"
};

typedef struct s_iso_item{
	std::string path;
	std::string filename;
	std::wstring affichage;
} iso_item;

static std::vector<iso_item> g_iso_list;

bool alphatri (iso_item i, iso_item j) 
{ 
	return j.affichage > i.affichage;
}

class XuiIsoList:public CXuiListImpl
{
	std::wstring strtowstr(std::string str)
	{
		wchar_t buffer[1000];
		MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, 1000);
		std::wstring s = buffer;
		return s;
	}
public:
	XUI_IMPLEMENT_CLASS( XuiIsoList, L"IsoList", XUI_CLASS_LIST )

	XUI_BEGIN_MSG_MAP()
		XUI_ON_XM_GET_SOURCE_IMAGE( OnGetSourceImage )
	    XUI_ON_XM_GET_ITEMCOUNT_ALL( OnGetItemCountAll )
        XUI_ON_XM_GET_SOURCE_TEXT( OnGetSourceText )
		XUI_ON_XM_INIT( OnInit )
		//XUI_ON_XM_TIMER( OnTimer )
		XUI_ON_XM_DESTROY ( OnDestroy )
		XUI_ON_XM_NOTIFY_SELCHANGED( OnNotifySelChanged )
	XUI_END_MSG_MAP()

	HRESULT ScanDir()
	{

		HANDLE				hFind;                   // Handle to file
		WIN32_FIND_DATA	FileInformation;         // File information

		g_iso_list.clear();

		hFind = FindFirstFile( "game:\\psxisos\\*", &FileInformation );
		if( hFind != INVALID_HANDLE_VALUE )
		{
			do
			{
				{
					if(!(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
					{
						iso_item s;
						s.path="game:\\psxisos\\";
						s.filename= FileInformation.cFileName;
						s.affichage = strtowstr(FileInformation.cFileName);
						g_iso_list.push_back(s);
					}
				}
			}
			while( FindNextFile( hFind, &FileInformation ) == TRUE );
			FindClose( hFind );
		}
		//Tri alphabetic
		std::sort (g_iso_list.begin(), g_iso_list.end(), alphatri);
		return S_OK;
	}

	HRESULT OnNotifySelChanged( HXUIOBJ hObjSource, XUINotifySelChanged* pNotifySelChangedData, BOOL& bHandled )
	{
		return S_OK;
	}

	HRESULT OnTimer( XUIMessageTimer *pTimer, BOOL& bHandled )
	{
		return S_OK;
	}
	HRESULT OnDestroy()
	{
		return S_OK;
	}
	HRESULT OnInit( XUIMessageInit* pInitData, BOOL& bHandled )
	{
		ScanDir();
		return S_OK;
	}
	HRESULT OnGetItemCountAll( XUIMessageGetItemCount* pGetItemCountData, BOOL& bHandled )
	{
		pGetItemCountData->cItems=g_iso_list.size();
		return S_OK;
	}
	HRESULT OnGetSourceText( XUIMessageGetSourceText* pGetSourceText, BOOL& bHandled )
	{
		pGetSourceText->szText= g_iso_list.at(pGetSourceText->iItem).affichage.c_str();

		bHandled = TRUE;
		return S_OK;
	}
	HRESULT OnGetSourceImage( XUIMessageGetSourceImage* pGetSourceImage, BOOL& bHandled )
	{
		return S_OK;
	}
};

/**
	SceneIsoSelection
**/
class SceneIsoSelection: CXuiSceneImpl
{
	private:

	XUI_BEGIN_MSG_MAP()
		XUI_ON_XM_INIT( OnInit )
		XUI_ON_XM_NOTIFY_PRESS( OnNotifyPress  )
		XUI_ON_XM_RENDER( OnRender )
		XUI_ON_XM_KEYDOWN ( OnKeyDown )
		//XUI_ON_XM_TIMER( OnTimer )
	XUI_END_MSG_MAP()

	CXuiList XuiIsoList;
	CXuiControl XuiChoose;
	CXuiCheckbox XuiDynarec;
	CXuiRadioGroup XuiFilter;

	CXuiControl Hq3x;
	CXuiControl Hq2x;
	CXuiControl Scale2x;

	HRESULT OnInit( XUIMessageInit* pInitData, BOOL& bHandled ){
		if(GetChildById( L"XuiIsoList", &XuiIsoList)!=S_OK){
			printf("XUIWarning !! XuiIsoList error");
		}
		if(GetChildById( L"XuiDynarec", &XuiDynarec)!=S_OK){
			printf("XUIWarning !! XuiDynarec error");
		}
		if(GetChildById( L"XuiChoose", &XuiChoose)!=S_OK){
			printf("XUIWarning !! XuiChoose error");
		}
		if(GetChildById( L"XuiFilter", &XuiFilter)!=S_OK){
			printf("XUIWarning !! XuiFilter error");
		}
		if(GetChildById( L"Hq3x", &Hq3x)!=S_OK){
			printf("XUIWarning !! Hq3x error");
		}
		if(GetChildById( L"Hq2x", &Hq2x)!=S_OK){
			printf("XUIWarning !! Hq2x error");
		}
		if(GetChildById( L"Scale2x", &Scale2x)!=S_OK){
			printf("XUIWarning !! Scale2x error");
		}
		//SetTimer(0,20000);//20 seconde pour selectionné un jeux
		XuiFilter.SetCurSel(0);
		XuiDynarec.SetCheck(true);
		return S_OK;
	}

	HRESULT OnNotifyPress( HXUIOBJ hObjPressed, BOOL& bHandled ){
		if(hObjPressed==XuiIsoList){
			//CODE 
			XuiDynarec.IsChecked()?SetDynarec(0):SetDynarec(1);
			Cemulator & c = Cemulator::GetInstance();
			std::string s = g_iso_list.at(XuiIsoList.GetCurSel()).path;
			s.append(g_iso_list.at(XuiIsoList.GetCurSel()).filename);
			c.SetIsoFilename(s);
			c.SetFilter(XuiFilter.GetCurSel());
			c.StartEmulation();//prochain boucle emule la psx
		}
		bHandled = TRUE;
		return S_OK;
	}

	HRESULT OnRender(XUIMessageRender *pRenderData, BOOL& bHandled){
		return S_OK;
	}

	HRESULT OnKeyDown(XUIMessageInput  *pInputData, BOOL& bHandled){
		return S_OK;
	}

	HRESULT OnTimer( XUIMessageTimer *pTimer, BOOL& bHandled ){
		KillTimer(pTimer->nId);

		Cemulator & c = Cemulator::GetInstance();
		std::string s = g_iso_list.at(XuiIsoList.GetCurSel()).path;
		s.append(g_iso_list.at(XuiIsoList.GetCurSel()).filename);
		c.SetIsoFilename(s);
		c.StartEmulation();//prochain boucle emule la psx
		return S_OK;
	}

	public:
	XUI_IMPLEMENT_CLASS( SceneIsoSelection, L"SceneIsoSelection", XUI_CLASS_SCENE )
};

