// AddIn.cpp

#include "StdAfx.h"

#include "AddIn.h"
#include "Service.h"
#include "FormEx.h"
#include "AttribEx.h"
//#include "QEx.h"
#include "PaintHook.h"
#include "MDIClientWnd.h"
//#include "WrapAboutDlg.h"
#include "v7Windows.h"
#include "InitFormEx.h"
#include "PaintPanel.h"
#include "UserSets.h"

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

class CDataBase7 * pDataBase7; 
class CMetaDataCont * pMetaDataCont;

/*
/////////////////////////////////////////////////////////////////////////////
// CAddIn
STDMETHODIMP CAddIn::QueryInterface(const struct _GUID & giud,void ** ppV)
{
	return FALSE;
}

unsigned long __stdcall CAddIn::AddRef(void)
{
	return FALSE;
}

unsigned long __stdcall CAddIn::Release(void)
{
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// IInitDone

STDMETHODIMP CAddIn::Init(IDispatch *pConnection)
{
	CBLContext* pTestFormEx = CBLContext::CreateInstance("FormEx");
	if(pTestFormEx)
	{
		// компонента уже загружена !
		pTestFormEx->DecrRef();
		//delete pTestFormEx;
		return S_OK;
	}

	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())
    
		CString csProfileName = "FormEx Profile";
		m_iProfile = NULL;
		pConnection->QueryInterface(IID_IPropertyProfile,(void **)&m_iProfile);
		m_iProfile->RegisterProfileAs(csProfileName.AllocSysString());

		if (LoadProperties() == FALSE) return E_FAIL;

		pGlobalConnection = pConnection;
	}

	pApp = AfxGetApp();

	pBkEndUI = GetBkEndUI();
	pDataBase7			= CDataBase7::GetDefault();
	pMetaDataCont		= pDataBase7->GetMD();

	CService::ObjID = CObjID(pMetaDataCont->GetNewID(),CObjID::GetDefDBSign());
	CFormEx::ObjID = CObjID(pMetaDataCont->GetNewID(),CObjID::GetDefDBSign());
	CAttribEx::ObjID = CObjID(pMetaDataCont->GetNewID(),CObjID::GetDefDBSign());

	CBLContext::RegisterContextClass(RUNTIME_CLASS(CService)	,"Service"	, CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CService)	,"Сервис"	, CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CFormEx)		,"FormEx"	, CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CFormEx)		,"РасширениеФормы", CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CAttribEx)	,"AttribEx"	, CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CAttribEx)	,"АтрибутФормы", CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(Cv7Windows)	,"Окна"		,CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(Cv7Windows)	,"Windows"	,CType(100));

	CBLContext::RegisterContextClass(RUNTIME_CLASS(CPaintPanel)	,"ПанельРисования"		,CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CPaintPanel)	,"PaintPanel"	,CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CUserSets)	,"НастройкиПользователя"		,CType(100));
	CBLContext::RegisterContextClass(RUNTIME_CLASS(CUserSets)	,"UserSets"	,CType(100));
//	CRGcontext::UnRegister();
//	CBLContext::RegisterContextClass(RUNTIME_CLASS(CQEx)	,"Запрос", CType(100));

	hLibBasic = GetModuleHandle("Basic.dll");

	if(!InitFormEx.Init()) return E_FAIL;


	return S_OK;
}

STDMETHODIMP CAddIn::Done()
{
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())

		SaveProperties();

		if (m_iProfile) m_iProfile->Release();
	}
    
	pBkEndUI = NULL;

//	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CQEx));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(Cv7Windows));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(Cv7Windows));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CAttribEx));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CAttribEx));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CFormEx));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CFormEx));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CService));
	CBLContext::UnRegisterContextClass(RUNTIME_CLASS(CService));

	if (hLibBasic != NULL)  
		FreeLibrary(hLibBasic);

	InitFormEx.Destroy();


	return S_OK;
}

BOOL CAddIn::LoadProperties()
{
	HRESULT hRes;
	CString csProperty; 
	VARIANT varEnabled;
	VariantInit(&varEnabled);
	V_VT(&varEnabled) = VT_I4;

	csProperty = "EnabledColor:0";
	hRes = m_iProfile->Read(csProperty.AllocSysString(),&varEnabled,NULL);
	if (hRes != S_OK) return FALSE;
	m_bEnabledColor = V_I4(&varEnabled)?TRUE:FALSE;

	csProperty = "LocalColor:0";
	hRes = m_iProfile->Read(csProperty.AllocSysString(),&varEnabled,NULL);
	if (hRes != S_OK) return FALSE;
	m_bLocalColor = V_I4(&varEnabled)?TRUE:FALSE;

	csProperty = "ShowKeyboardLayout:0";
	hRes = m_iProfile->Read(csProperty.AllocSysString(),&varEnabled,NULL);
	if (hRes != S_OK) return FALSE;
	m_bShowKL = V_I4(&varEnabled)?TRUE:FALSE;

	return TRUE;
}

void CAddIn::SaveProperties()
{
	CString csProperty; 
	VARIANT varEnabled;
	VariantInit(&varEnabled);
	V_VT(&varEnabled) = VT_I4;

	csProperty = "EnabledColor";
	V_I4(&varEnabled) = m_bEnabledColor?1:0;
	m_iProfile->Write(csProperty.AllocSysString(),&varEnabled);

	csProperty = "LocalColor";
	V_I4(&varEnabled) = m_bLocalColor?1:0;
	m_iProfile->Write(csProperty.AllocSysString(),&varEnabled);

	csProperty = "ShowKeyboardLayout";
	V_I4(&varEnabled) = m_bShowKL?1:0;
	m_iProfile->Write(csProperty.AllocSysString(),&varEnabled);
}

STDMETHODIMP CAddIn::GetInfo(SAFEARRAY **pInfo)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

      // Component should put supported component technology version 
      // in VARIANT at index 0     
    long lInd = 0;
    VARIANT varVersion;
    V_VT(&varVersion) = VT_I4;

    V_I4(&varVersion) = 2000;
    SafeArrayPutElement(*pInfo,&lInd,&varVersion);
    
	return S_OK;
}

/////////////////////////////////////////////////////////////////////////////
// IPropertyPage

STDMETHODIMP CAddIn::SetPageSite(IPropertyPageSite *pPageSite)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::SetPageSite(pPageSite);
}

STDMETHODIMP CAddIn::Activate(HWND hWndParent,LPCRECT prc,BOOL bModal)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::Activate(hWndParent,prc,bModal);
}

STDMETHODIMP CAddIn::Deactivate()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::Deactivate();
}

STDMETHODIMP CAddIn::GetPageInfo(PROPPAGEINFO *pPageInfo)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::GetPageInfo(pPageInfo);
}

STDMETHODIMP CAddIn::SetObjects(ULONG cObjects,IUnknown **ppUnk)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::SetObjects(cObjects,ppUnk);
}

STDMETHODIMP CAddIn::Show(UINT nCmdShow)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::Show(nCmdShow);
}

STDMETHODIMP CAddIn::Move(LPCRECT prc)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::Move(prc);
}

STDMETHODIMP CAddIn::IsPageDirty()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

    return IPropertyPageImpl<CAddIn>::IsPageDirty();
}

STDMETHODIMP CAddIn::Apply()
{
	InitFormEx.BeforeApply();

	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())

		BOOL boolEnabled1;
		BOOL boolEnabled2;
		BOOL boolEnabled3;
		IPropertyLink *pLink = NULL;

		m_ppUnk[0]->QueryInterface(IID_IPropertyLink,(LPVOID *)&pLink);
		if (pLink != NULL)
		  {
			boolEnabled1 = m_boxEnabledColor.GetCheck()?TRUE:FALSE;
			boolEnabled2 = m_boxLocalColor.GetCheck()?TRUE:FALSE;
			boolEnabled3 = m_boxShowKL.GetCheck()?TRUE:FALSE;
			pLink->put_Enabled(boolEnabled1, boolEnabled2, boolEnabled3);
			pLink->Release();
		  }

		SetDirty(FALSE);
	}

	InitFormEx.Apply();

	return S_OK;
}

STDMETHODIMP CAddIn::Help(LPCOLESTR pszHelpDir)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here

    return IPropertyPageImpl<CAddIn>::Help(pszHelpDir);
}

STDMETHODIMP CAddIn::TranslateAccelerator(LPMSG pMsg)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	// TODO: Add your implementation code here
    
    return IPropertyPageImpl<CAddIn>::TranslateAccelerator(pMsg);
}


LRESULT CAddIn::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    
	IPropertyLink *pLink = NULL;
	m_ppUnk[0]->QueryInterface(IID_IPropertyLink,(LPVOID *)&pLink);

    if (pLink != NULL)
	{
		pLink->get_Enabled(&m_bEnabledColor,&m_bLocalColor,&m_bShowKL);  
		pLink->Release();
	}

    m_boxEnabledColor.Attach(GetDlgItem(IDC_ENCOLOR));
    m_boxEnabledColor.SetCheck(m_bEnabledColor?1:0);

    m_boxLocalColor.Attach(GetDlgItem(IDC_LOCALCOLOR));
    m_boxLocalColor.SetCheck(m_bLocalColor?1:0);

    m_boxShowKL.Attach(GetDlgItem(IDC_SHOWKL));
    m_boxShowKL.SetCheck(m_bShowKL?1:0);

    SetDirty(FALSE);
    m_pPageSite->OnStatusChange(0);
    bHandled = FALSE;

    return 0; 
}

LRESULT CAddIn::OnCommand(WORD wNotifyCode, WORD nID, HWND hWndCtl, BOOL& bHandled)
{
    SetDirty(TRUE);
    bHandled = FALSE;

    return 0;
}

/////////////////////////////////////////////////////////////////////////////
// IPropertyLink

STDMETHODIMP CAddIn::get_Enabled(BOOL *boolEnabled1, BOOL *boolEnabled2, BOOL *boolEnabled3)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	*boolEnabled1 = m_bEnabledColor;
	*boolEnabled2 = m_bLocalColor;
	*boolEnabled3 = m_bShowKL;

	return S_OK;
}

STDMETHODIMP CAddIn::put_Enabled(BOOL boolEnabled1, BOOL boolEnabled2, BOOL boolEnabled3)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState())

	m_bEnabledColor = boolEnabled1;
	m_bLocalColor = boolEnabled2;
	m_bShowKL = boolEnabled3;

	return S_OK;
}
*/
