// 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();

	
	bAutomationActivated = FALSE;
	
	//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(0);
	SuperVisSheet->SetActivePage(1);
	SuperVisSheet->SetActivePage(2);
	SuperVisSheet->SetActivePage(3);
	SuperVisSheet->SetActivePage(4);
	SuperVisSheet->SetActivePage(5);
	SuperVisSheet->SetActivePage(6);

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


	SuperVisSheet->ShowWindow(SW_SHOW);


	//Start the Timer...
	SuperVisSheet->m_SuperVisSettingPage.m_uiRefreshRate = pSuperVisApp->m_iRefreshRate;

	UINT ret = SuperVisSheet->SetTimer( WM_USER_TIMER, SuperVisSheet->m_SuperVisSettingPage.m_uiRefreshRate, NULL );
	if (ret != WM_USER_TIMER)
		MessageBox(NULL, "SetTimer Failure", "Error!", MB_OK);


	//Signal the Other Thread that this Thread have finished loading
	pSuperVisApp->LaunchSuperVisEvent->SetEvent();


	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();

	if(pSuperVisApp->m_cLCBPath == "NoProjectTitle")
	{
		//The app didnt return ProjectTitle. We are in binary mode...
		if( pSuperVisApp->m_cBinaryLCBPath != "NoLCBPath")
			return Automation(pSuperVisApp->m_cBinaryLCBPath);

	}
	else
	{
		//We use the projectitle returned by the app.
		return Automation(pSuperVisApp->m_cLCBPath);
	}

	return FALSE;
}
	
BOOL CSuperVisThread::Automation(CString _cProfectFile)
{

	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 init
	if (!m_IEditor->CreateDispatch(_T("SoundEditor.Document")))
	{
		AfxMessageBox("Could Not Load SoundEditor. Check if it is installed on your computer and be sure to have it started once before using Supervisater.\n This will let know Windows where to find it");
		return FALSE;
	}



	if (!m_IEditor->OpenDocument(_cProfectFile))
	{
		AfxMessageBox("Could not open the SoundEditor file project:" + _cProfectFile + "\nBrowse for a valid on");
		return FALSE;
	}


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


	//Enable and disable controls
	InitAutomationControls();
	
	bAutomationActivated = TRUE;

	return TRUE;

}

void CSuperVisThread::ExitAutomation()
{

	if(bAutomationActivated)
	{
		
		//Close The SoundEditor document
		m_IEditor->ReleaseDispatch();
		
		
		//delete soundeditor object
		/*delete m_IEditor;
		delete m_IGroups;
		*/
		
		//Remove Ole object
		OleUninitialize();
	}


}


BOOL CSuperVisThread::PreTranslateMessage(MSG* pMsg) 
{

	//The thread just received a message to Init OLE automation with soundeditor
	//Wparam : not used
	//LParam : Event to synchronise the two thread
	if (pMsg->message == WM_INIT_AUTOMATION){

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

		
		SetEvent((HANDLE)(pMsg->lParam));
		
	}

	//The thread just received a message to Exit OLE automation with soundeditor
	//Wparam : not used
	//LParam : Event to synchronise the two thread
	if (pMsg->message == WM_EXIT_AUTOMATION){

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

		SetEvent((HANDLE)(pMsg->lParam));
		
	}

	return CWinThread::PreTranslateMessage(pMsg);
}


void CSuperVisThread::InitAutomationControls()
{

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

	//
	//This function init all controls related to automation on all property page
	//

	//Enable the save button on the Mixer Page
	((CButton *)(pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_MixerPage.GetDlgItem(IDC_SAVE)))->EnableWindow( TRUE );

	//Disable the browse for LCB button in the SuperVisetting page
	((CButton *)(pSuperVisApp->m_SuperVisThread->SuperVisSheet->m_SuperVisSettingPage.GetDlgItem(IDC_BROWSE_FOR_LCB)))->EnableWindow( FALSE );

}



