// SuperVisThread.cpp : implementation file
//

#include "stdafx.h"
#include "supervis.h"
#include "SuperVisThread.h"



#include "SNDinc.h"

#include "snddll.h"

#include "snddbg.h"
#include "sndplay.h"
#include "sndvect.h"
#include "sndres.h"
#include "sndini.h"
#include "sndtheme.h"
#include "sndvirt.h"
#include "sndevent.h"





#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSuperVisThread

IMPLEMENT_DYNCREATE(CSuperVisThread, CWinThread)

CSuperVisThread::CSuperVisThread()
{
}

CSuperVisThread::~CSuperVisThread()
{
}

BOOL CSuperVisThread::InitInstance()
{
	
	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();
	


	//
	//Initialisation de l'ACP
	//
	
	//Initialiser le module Erm
	Erm_M_InitErrMsg(Erm);
	//Initialiser le module Mmg
	Erm_M_InitErrMsg(Mmg);
	//Initialiser le nombre de channel pour le module Mmg. 10 est un chiffre magique, vaut mieux plus que moins
	Mmg_fn_vFirstInitMmgModule(10);
	//Initialiser le module script
	SCR_fn_v_RdL0_Init();



	//
	//initialisation du module SCR de l'ACP pour permettre d'aller
	//chercher des informations dans le fichier script
	//
	SCR_fn_v_RdL0_RegisterPath((char*)(LPCTSTR)(pSuperVisApp->m_cLCBPath));


	//Init Script callback
	SCR_fn_v_RdL0_RegisterCallback("SndEventGroupList",fn_AnalyseLCB, SCR_CRC_c_RdL0_ForSection);	
	SCR_fn_v_RdL0_RegisterCallback("SndEventE",fn_FindEventNameInCSB, SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback("SndResourceE",fn_FindResNameInCSB, SCR_CRC_c_RdL0_ForSection);

	

	//Error calback for script
	SCR_fnp_fn_RdL0_RegisterErrorCallback(erreur, SCR_CM_Err_Normal);



	//Create the modeless PropertySheet. This is the main dialog
	SuperVisSheet = new CSuperVisSheet;
	SuperVisSheet->Create(NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE   , 0 );

	//Activate the CPropertyPage.
	SuperVisSheet->SetActivePage(1);
	SuperVisSheet->SetActivePage(2);
	SuperVisSheet->SetActivePage(3);
	SuperVisSheet->SetActivePage(4);
	SuperVisSheet->SetActivePage(5);
	SuperVisSheet->SetActivePage(6);

	//Focus on page 0 on startup
	SuperVisSheet->SetActivePage(0);


	SuperVisSheet->ShowWindow(SW_SHOW);


	//Test automation
	if(!InitAutomation())
		return FALSE;


	return TRUE;
}

int CSuperVisThread::ExitInstance()
{
	
	//UnInstall Automation objects
	ExitAutomation();
	
	
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CSuperVisThread, CWinThread)
	//{{AFX_MSG_MAP(CSuperVisThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSuperVisThread message handlers

BOOL CSuperVisThread::InitAutomation()
{

	CSupervisApp* pSuperVisApp = (CSupervisApp*)AfxGetApp();

	m_IEditor = new ISoundEditor;


	if(OleInitialize(NULL) == OLE_E_WRONGCOMPOBJ )
	{
		AfxMessageBox("The versions of COMPOBJ.DLL and OLE2.DLL on your machine are incompatible with each other.");
		return FALSE;
	}

	COleObjectFactory::RegisterAll();


	// Sound editor int:
	if (!m_IEditor->CreateDispatch(_T("SoundEditor.Document")))
	{
		AfxMessageBox("Could not load the sound editor");
		return FALSE;
	}

retry:
	
	if (!m_IEditor->OpenDocument(SuperVisSheet->m_EventsHistoryPage.m_LCBPath ))
	{
		AfxMessageBox("Could not open the SoundEditor file project:" + SuperVisSheet->m_EventsHistoryPage.m_LCBPath +"\nBrowse for a valid on");
		if(SuperVisSheet->m_EventsHistoryPage.BrowseForLcb())
			goto retry;
		return FALSE;
	}



	//Get all groups
	m_IGroups =  new IEventGroups;
	m_IGroups->AttachDispatch(m_IEditor->GetEventGroups());

	return TRUE;

}

void CSuperVisThread::ExitAutomation()
{

	//Remove Ole object
	OleUninitialize();

	//delete soundeditor object
	delete m_IEditor;
	delete m_IGroups;




}
