//*****************************************************************************
//*  TutInter.cpp                                                             *
//*****************************************************************************
//*                                                                           *
//* This file contains the definition of the TUT_Interface class,             *
//* the interface of the TUT dll.                                             *
//*                                                                           *
//*****************************************************************************
//* Author : Alexis Vaisse                                                    *
//*****************************************************************************


#include "stdafx.h"

#include "ACP_Base.h"
#include "ITF.h"

#include "TUT.h"
#include "TutInter.hpp"
#include "DlgInput.hpp"
#include "..\TutLib\Inc\Tut_Mngr.hpp"


tdstDLLIdentity	g_stTUT_Identity;

#define C_uiTUTPopupRun			10
#define C_uiTUTPopupAutoRun		11
#define C_uiTUTPopupBackGround	12
#define C_uiTUTPopupSet			14
#define C_uiTUTPopupRecord		15

//-----------------------------------------------------------------------------
// Class TUT_Interface - Constructor
//-----------------------------------------------------------------------------
TUT_Interface::TUT_Interface ()
{
    m_p_stDLLIdentity = & g_stTUT_Identity;

    m_stBaseDLLDefinition . bCanOutputIn3DView      = FALSE;
    m_stBaseDLLDefinition . bCanBeRefreshedByEngine = FALSE;

    SetEditorInfo ("Tutorial", "Alexis Vaisse", "V 2.0.0   (not updated)", "", "");

	m_bAutoRun			= FALSE;
	m_bBackGroundActive	= FALSE;
	m_csScriptFileName	= "Test.tut";
	m_csScriptPathName	= "x:\\Cpa\\Exe\\Main";
	m_bScriptRecursive	= FALSE;
	m_bRecording		= FALSE;
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - fn_vConstruct
//-----------------------------------------------------------------------------
void TUT_Interface::fn_vConstruct (void)
{
	fn_vSetReceivingWindowMessages (TRUE);
	m_fn_vLoadPreferences ();

	TUT_g_oTutorialManager . m_fn_vSetBackGroundActive (m_bBackGroundActive) ;
	if (m_bAutoRun && ! m_csScriptFileName . IsEmpty() )	TUT_g_oTutorialManager . m_fn_vLaunchScript (m_csScriptFileName);
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - OnQueryAction
//-----------------------------------------------------------------------------
long TUT_Interface::OnQueryAction (CPA_EditorBase * _pCallingEditor , WPARAM _wParam , LPARAM _lParam)
{
    switch (_wParam)
    {
        case TUT_C_lQueryRegisterControl :
            {
             
                TUT_tdstQueryWindowStructure * p_stQueryStructure = (TUT_tdstQueryWindowStructure *) _lParam;
                TUT_g_oTutorialManager . m_fn_vRegisterWnd (p_stQueryStructure -> hHandleOfControl ,
                                                            p_stQueryStructure -> csNameOfControl  ,
                                                            (TUT_tdeWndType) p_stQueryStructure -> eTypeOfControl);
                break;
            }
        case TUT_C_lQueryUnregisterControl :
            {
                TUT_g_oTutorialManager . m_fn_bUnregisterWnd ((HWND) _lParam);
                break;
            }

        case TUT_C_lQueryRegisterMenu :
            {
                TUT_tdstQueryMenuStructure * p_stQueryStructure = (TUT_tdstQueryMenuStructure *) _lParam;
                TUT_g_oTutorialManager . m_fn_vRegisterMenu (p_stQueryStructure -> hHandleOfWindow ,
                                                             p_stQueryStructure -> hHandleOfMenu ,
                                                             p_stQueryStructure -> iX ,
                                                             p_stQueryStructure -> iY);
                break;
            }
    }

    return 0;
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - _OnKeyDown
//-----------------------------------------------------------------------------
BOOL TUT_Interface::_OnKeyDown (UINT nChar , UINT nRepCnt , UINT nFlags)
{
/*
    // Alt+T -> lauch the script
    if (nChar == 'T' && (nFlags & 0x2000) != 0)
        TUT_g_oTutorialManager . m_fn_vLaunchScript ("Test.tut");
*/
    return FALSE;
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - fn_bDefineSubMenu
// Add an entry in the editor menu
//-----------------------------------------------------------------------------
BOOL TUT_Interface::fn_bDefineSubMenu (EDT_SubMenu * _p_oEDTSubMenu)
{
    if (_p_oEDTSubMenu -> GetSubMenuType () == C_SubMenuTool)
    {
		EDT_SubMenu *p_oSubMenu = _p_oEDTSubMenu -> fn_p_oGetNewSubMenu("Tutorial");
		CString csEntry = "Run File";

		if (! m_csScriptFileName . IsEmpty())		csEntry += " : \"" + m_csScriptFileName + "\"";

		p_oSubMenu -> AddAnEntry (this , (char*)(LPCTSTR)csEntry , C_uiTUTPopupRun , FALSE , ! TUT_g_oTutorialManager . m_fn_bIsRunning());
		p_oSubMenu -> AddASeparator ();
		p_oSubMenu -> AddAnEntry (this , "AutoRun" , C_uiTUTPopupAutoRun , m_bAutoRun);
		p_oSubMenu -> AddAnEntry (this , "Choose Main File" , C_uiTUTPopupSet);
		p_oSubMenu -> AddAnEntry (this , "Active On BackGround" , C_uiTUTPopupBackGround , m_bBackGroundActive);
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Script recording
		p_oSubMenu -> AddAnEntry (this , m_bRecording ? "Stop recording" : "Start recording" , C_uiTUTPopupRecord);
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		_p_oEDTSubMenu -> AddASubMenu (p_oSubMenu);
        return TRUE;
    }
    return FALSE;
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - _OnSubMenuCommand
// Called when the user selects an entry in the menu
//-----------------------------------------------------------------------------
void TUT_Interface::_OnSubMenuCommand (EDT_SubMenu * _p_oEDTSubMenu , UINT uiMsgID)
{
    if (_p_oEDTSubMenu -> GetSubMenuType () == C_SubMenuTool)
    {
		if (uiMsgID == C_uiTUTPopupRun)
		{
			CDialogInput *p_oDlg = new CDialogInput ("File Name" , m_csScriptPathName, m_csScriptFileName, m_bScriptRecursive);

			if (p_oDlg && p_oDlg -> DoModal () == IDOK)
			{
				m_csScriptFileName = p_oDlg -> m_fn_csGetEditText ();
				m_csScriptPathName = p_oDlg -> m_fn_csGetPath();
				m_bScriptRecursive = p_oDlg -> m_fn_bGetRecurse();
				m_fn_vWritePreferences ();
				if (! m_csScriptFileName . IsEmpty())	TUT_g_oTutorialManager . m_fn_vLaunchScript (m_csScriptFileName);
			}
			if (p_oDlg)		delete p_oDlg;
		}
		else if (uiMsgID == C_uiTUTPopupAutoRun)
		{
			m_bAutoRun = ! m_bAutoRun;
			m_fn_vWritePreferences ();
		}
		else if (uiMsgID == C_uiTUTPopupBackGround)
		{
			m_bBackGroundActive = ! m_bBackGroundActive;
			TUT_g_oTutorialManager . m_fn_vSetBackGroundActive (m_bBackGroundActive) ;
			m_fn_vWritePreferences ();
		}
		else if (uiMsgID == C_uiTUTPopupSet)
		{
			CDialogInput *p_oDlg = new CDialogInput ("File Name" , m_csScriptFileName);

			if (p_oDlg && p_oDlg -> DoModal () == IDOK)
			{
				m_csScriptFileName = p_oDlg -> m_fn_csGetEditText ();
				m_fn_vWritePreferences ();
			}
			if (p_oDlg)		delete p_oDlg;
		}    
		else if (uiMsgID == C_uiTUTPopupRecord)
		{
			m_fn_vStartStopRecord ();
		}
    }
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - m_fn_vLoadPreferences
// Load preferences from a .INI file
//-----------------------------------------------------------------------------
void TUT_Interface::m_fn_vLoadPreferences (void)
{
	CString	csFileName		= M_GetMainApp()->m_csEditorDataPath + "Tools\\Tut\\Tut.ini";
    char	*p_szFileName	= (char*)(LPCSTR)csFileName;
	char    szDimension[256];

    // AutoRun Mode
	GetPrivateProfileString ("Preference" , "AutoRun"			, "FALSE" , szDimension , 256 , (char*)(LPCSTR)p_szFileName);
    m_bAutoRun = !strcmpi( szDimension, "TRUE" );

	// Main Script File
	GetPrivateProfileString ("Preference" , "Script File"		,  ""	  , szDimension , 256 , (char*)(LPCSTR)p_szFileName);
    m_csScriptFileName = szDimension;

	// Main Path Name
	GetPrivateProfileString ("Preference" , "Path Name"			,  ""	  , szDimension , 256 , (char*)(LPCSTR)p_szFileName);
    m_csScriptPathName = szDimension;

	// Recursive Read
	GetPrivateProfileString ("Preference" , "Recursive Read"	, "FALSE" , szDimension , 256 , (char*)(LPCSTR)p_szFileName);
    m_bScriptRecursive = !strcmpi( szDimension, "TRUE" );

	// Main BackGround Active Mode
	GetPrivateProfileString ("Preference" , "BackGround Active" , "FALSE" , szDimension , 256 , (char*)(LPCSTR)p_szFileName);
    m_bBackGroundActive = !strcmpi( szDimension, "TRUE" );
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - m_fn_vWritePreferences
// Save preferences on a .INI file
//-----------------------------------------------------------------------------
void TUT_Interface::m_fn_vWritePreferences (void)
{
	CString	csFileName;

	csFileName = M_GetMainApp()->m_csEditorDataPath + "Tools\\Tut\\Tut.ini";

    char *p_szFileName = (char*)(LPCSTR)csFileName;

    // AutoRun Mode
    WritePrivateProfileString ("Preference" , "AutoRun"				, m_bAutoRun ? "TRUE" : "FALSE"				, p_szFileName);

	// Main Script File
    WritePrivateProfileString ("Preference" , "Script File"			, m_csScriptFileName						, p_szFileName);

	// Main Path Name
    WritePrivateProfileString ("Preference" , "Path Name"			, m_csScriptPathName						, p_szFileName);

	// Recursive Read
    WritePrivateProfileString ("Preference" , "Recursive Read"  	, m_bScriptRecursive ? "TRUE" : "FALSE"		, p_szFileName);

	// Main BackGround Active Mode
    WritePrivateProfileString ("Preference" , "BackGround Active"	, m_bBackGroundActive ? "TRUE" : "FALSE"	, p_szFileName);

    // write on file
	WritePrivateProfileString (NULL			, NULL					, NULL										, p_szFileName);
}

//-----------------------------------------------------------------------------
// Class TUT_Interface - m_fn_vStartStopRecord
// 
//-----------------------------------------------------------------------------
void TUT_Interface::m_fn_vStartStopRecord (void)
{
	m_bRecording = !m_bRecording;

	if (m_bRecording)
	{
		// Start recording
		TUT_g_oTutorialManager . m_fn_vStartRecord (GetDLLIdentity () -> hModule , M_GetMainApp() -> m_nThreadID);
	}
	else
	{
		CFileDialog oDlg (FALSE, "*.tut" , NULL , OFN_EXPLORER | OFN_HIDEREADONLY | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT | OFN_SHOWHELP , NULL , NULL );

		if (oDlg . DoModal() == IDOK)
		{
			// stop and save recording
			TUT_g_oTutorialManager . m_fn_vStopRecord (GetDLLIdentity () -> hModule , M_GetMainApp() -> m_nThreadID , oDlg . GetPathName ());
		}
		else
		{
			// stop and not save recording
			TUT_g_oTutorialManager . m_fn_vStopRecord (GetDLLIdentity () -> hModule , M_GetMainApp() -> m_nThreadID , NULL);
		}
	}
}