// WebDlg.cpp : 구현 파일입니다.
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "WebDlg.h"
#include ".\webdlg.h"
#include "Mshtml.h"

// CWebDlg 대화 상자입니다.
LPDISPATCH glpDisp = NULL; // global LPDISPATCH, can also
// be of class scope



IMPLEMENT_DYNAMIC(CWebDlg, CDialog)
CWebDlg::CWebDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWebDlg::IDD, pParent)
{
}

CWebDlg::~CWebDlg()
{
}

void CWebDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EXPLORER1, m_web);
}


BEGIN_MESSAGE_MAP(CWebDlg, CDialog)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CWebDlg 메시지 처리기입니다.
BEGIN_EVENTSINK_MAP(CWebDlg, CDialog)
	ON_EVENT(CWebDlg, IDC_EXPLORER1, 259, DocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
	ON_EVENT(CWebDlg, IDC_EXPLORER1, 251, NewWindow2Explorer1, VTS_PDISPATCH VTS_PBOOL)
	ON_EVENT(CWebDlg, IDC_EXPLORER1, 263, WindowClosingExplorer1, VTS_BOOL VTS_PBOOL)
	ON_EVENT(CWebDlg, IDC_EXPLORER1, 252, NavigateComplete2Explorer1, VTS_DISPATCH VTS_PVARIANT)
END_EVENTSINK_MAP()

void CWebDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
	long width;
	long height;

	if (m_nType == 5)
	{
		long width = 627;
		long height = 675;

		int cx = GetSystemMetrics(SM_CXSCREEN);
		int cy = GetSystemMetrics(SM_CYSCREEN);

		m_web.MoveWindow(0, 0, width, height, TRUE);
		MoveWindow(cx/2 - width /2,cy /2 - height/2,width, height);

		return;
	}

	//	if (glpDisp && glpDisp == pDisp)
	{
		// if the LPDISPATCH are same, that means
		// it is the final DocumentComplete. Reset glpDisp
		TRACE("Document is done downloading");
		glpDisp = NULL;

		IDispatch* pDocDispatch = NULL ; 
		pDocDispatch = m_web.get_Document();

		IHTMLDocument2* pHTMLDoc = NULL; 
		IHTMLTextContainer *pText = NULL;

		pDocDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pHTMLDoc);

		IHTMLElement* pHTEMLElement = NULL;
		IHTMLElement *lpParentElm;

		pHTMLDoc->get_body(&pHTEMLElement);

		pHTEMLElement->get_parentElement(&lpParentElm);
		BSTR    bstr;
		lpParentElm->get_outerHTML(&bstr);

		CString sData;
		sData = bstr;

		int curPos= 0;

		CString sTemp = sData.Tokenize("$%^&",curPos);

		if(!sTemp.IsEmpty())
		{
			sTemp= sData.Tokenize("$%^&",curPos);

			if(!sTemp.IsEmpty())
			{
				int iPos=0;
				width = atol(sTemp.Tokenize(",",iPos));
				height = atol(sTemp.Tokenize(",",iPos));

				int cx = GetSystemMetrics(SM_CXSCREEN);
				int cy = GetSystemMetrics(SM_CYSCREEN);

				if(width != 0 && height != 0)
				{
					m_web.MoveWindow(0, 0, width, height, TRUE);
					MoveWindow(cx/2 - width /2,cy /2 - height/2,width, height);
				}

			}
		}

		SysFreeString (bstr);
		//		CComQIPtr<IHTMLElement2> element = pHTEMLElement;

		//		if(element)
		//		{
		//			element->get_clientHeight(&height);
		//			element->get_clientWidth(&width);

		//			int cx = GetSystemMetrics(SM_CXSCREEN);
		//			int cy = GetSystemMetrics(SM_CYSCREEN);

		//			m_web.MoveWindow(0, 0, width, height, TRUE);
		//			MoveWindow(cx/2 - width /2,cy /2 - height/2,width, height);
		//		}
	}

}

void CWebDlg::NewWindow2Explorer1(LPDISPATCH* ppDisp, BOOL* Cancel)
{
	m_pWebDlg = new CWebDlg();
	m_pWebDlg->Create(IDD_WEB_DLG,GetDesktopWindow());
	m_pWebDlg->m_web.put_RegisterAsBrowser(TRUE);
	*ppDisp = m_pWebDlg->m_web.get_Application();

	CString strTitle;
	GetWindowText(strTitle);
	m_pWebDlg->SetWindowText(strTitle);
	m_pWebDlg->ShowWindow(TRUE);
}

BOOL CWebDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	return TRUE;  // return TRUE unless you set the focus to a control
	// 예외: OCX 속성 페이지는 FALSE를 반환해야 합니다.
}

int CWebDlg::GetPage(WEBDLG_TYPE nFlag,CString sCP,CString sUser,CString strPath)
{
	CString strURL;
	CString strTitle;
	CString sData;
	CString strHeader = _T("Content-Type: application/x-www-form-urlencoded\r\n");

	m_nType = nFlag;

	switch(nFlag)
	{
	case MAN_TRAFFIC:
		strURL = URL_MAN_TRAFFIC;
		strTitle = "컨텐츠 통계";
		sData.Format("p=%s!%s",sCP,sUser);
		break;
	case MAN_FOLDER:
		strURL = URL_MAN_FOLDER;
		strTitle = "권한 관리";
		sData.Format("p=%s!%s",sCP,sUser);
		break;
	case MAN_USER:
		strURL = URL_MAN_USER;
		strTitle = "사용자 관리";
		sData.Format("p=%s!%s",sCP,sUser);
		break;
	case MAN_TOP10:
		strURL = URL_MAN_TOP10;
		strTitle = "이미지 호스팅 Top10";
		sData.Format("p=%s!%s",sCP,sUser);
		break;
	case MAN_FILE_STAT:
		strURL = URL_MAN_FILE_STAT;
		strTitle = "파일 통계";
		sData.Format("p=%s!%s",sCP,strPath);
		break;
	case MAN_FILE_PRO:
		strURL = URL_MAN_FILE_PRO;
		strTitle = "파일 속성";
		sData.Format("p=%s!%s",sCP,strPath);
		break;
	case MAN_NOTICE:
		strURL = URL_MAN_NOTICE;
		strTitle = "공지 사항";
		sData.Format("p=%s!%s",sCP,sUser);
		break;
	case MAN_COUPON:
		strURL = URL_MAN_COUPON;
		strTitle = "쿠폰정보";
		sData.Format("p=%s",sCP);
		break;
	case MAN_TRAFIC_STAT:
		strURL = URL_MAN_TRAFIC_STAT;
		strTitle = "트래픽 이용내역";
		sData.Format("p=%s",sCP);
		break;
	case MAN_FINDID:
		strURL = URL_MAN_FINDID;
		strTitle = "아이디/비밀번호찾기";
		break;
	case MAN_DISTINFO:
		strURL = URL_MAN_DISTINFO;
		strTitle = "배포현황조회";
		sData.Format("p=%s!%s!%s",sCP,sUser,strPath);
		break;
	}

	SetWindowText(strTitle);

	VARIANT vHeader;
	vHeader.vt = VT_BSTR;
	vHeader.bstrVal = strHeader.AllocSysString();

	SAFEARRAY FAR *sfPost = NULL;
	SAFEARRAYBOUND bound;
	bound.cElements = sData.GetLength();
	bound.lLbound = 0;
	sfPost = SafeArrayCreate(VT_UI1,1,&bound);

	for (long lIndex = 0; lIndex < (signed)bound.cElements;lIndex++)
	{
		SafeArrayPutElement(sfPost,&lIndex, sData.GetBuffer(bound.cElements) + lIndex);
	}

	sData.ReleaseBuffer();

	VARIANT vaPost;
	vaPost.vt = VT_ARRAY;
	vaPost.parray = sfPost;

	m_web.Navigate(strURL, 0 , NULL, &vaPost,&vHeader);
//	m_web.Navigate(strURL, 0 , NULL, NULL,&vHeader);

	SafeArrayDestroy(sfPost);
	SysFreeString(vHeader.bstrVal);
	return 0;
}

void CWebDlg::PostNcDestroy()
{
	CDialog::PostNcDestroy();
	delete this;
}

void CWebDlg::OnClose()
{
	CDialog::OnClose();
}

void CWebDlg::OnCancel()
{
	DestroyWindow();
}

void CWebDlg::WindowClosingExplorer1(BOOL IsChildWindow, BOOL* Cancel)
{
	PostMessage(WM_CLOSE,0,0);
	// TODO: 여기에 메시지 처리기 코드를 추가합니다.
}

void CWebDlg::NavigateComplete2Explorer1(LPDISPATCH pDisp, VARIANT* URL)
{
	// Check if glpDisp is NULL. If NULL, that means it is
	// the top level NavigateComplete2. Save the LPDISPATCH
	if (!glpDisp)
		glpDisp = pDisp;

}
