// RemoteComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "RemoteComboBox.h"
#include "MainFrm.h"
#include "RemoteView.h"
#include "RemoteTreeCtrl.h"
#include ".\remotecombobox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRemoteComboBox

CRemoteComboBox::CRemoteComboBox()
{
	m_iSPos = m_iEPos = -1;
}

CRemoteComboBox::~CRemoteComboBox()
{
}


BEGIN_MESSAGE_MAP(CRemoteComboBox, CComboBoxEx)
	//{{AFX_MSG_MAP(CRemoteComboBox)
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
//	ON_NOTIFY_REFLECT(CBEN_ENDEDIT, OnCbenEndedit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteComboBox message handlers
int CRemoteComboBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//	if (CComboBox::OnCreate(lpCreateStruct) == -1)
//		return -1;

	SHFILEINFO shfi;
	HIMAGELIST hil = (HIMAGELIST)SHGetFileInfo("C:\\", 0, &shfi, sizeof(shfi), SHGFI_ICON | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

	SetImageList(CImageList::FromHandle(hil));

	return 0;
}

void CRemoteComboBox::OnSelchange()
{
	CRemoteTreeCtrl *ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

	ptree->SelectItem((HTREEITEM)GetItemData(GetCurSel()));
}

void CRemoteComboBox::Init()
{
	Clear();

	CRemoteTreeCtrl *ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();

	COMBOBOXEXITEM cbei;
	cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_INDENT;

	int     nImage, nSelectedImage;
	CString sText;

	// 루트 삽입
	HTREEITEM hti = ptree->GetRootItem();
	ptree->GetItemImage(hti, nImage, nSelectedImage);
	sText = ptree->GetItemText(hti);

	cbei.iItem          = GetCount();
	cbei.iIndent        = 0;
	cbei.iImage         = nImage;
	cbei.iSelectedImage = nSelectedImage;
	cbei.pszText        = (LPSTR)(LPCTSTR)sText;
	InsertItem(&cbei);
	SetItemData((int)cbei.iItem, (DWORD_PTR)hti);

	// 볼륨 삽입
	hti = ptree->GetChildItem(hti);
	while (hti) {
		ptree->GetItemImage(hti, nImage, nSelectedImage);
		sText = ptree->GetItemText(hti);

		cbei.iItem          = GetCount();
		cbei.iIndent        = 1;
		cbei.iImage         = nImage;
		cbei.iSelectedImage = nSelectedImage;
		cbei.pszText        = (LPSTR)(LPCTSTR)sText;
		InsertItem(&cbei);
		SetItemData((int)cbei.iItem, (DWORD_PTR)hti);

		hti = ptree->GetNextSiblingItem(hti);
	}

	SetCurSel(0);
}

void CRemoteComboBox::Add(HTREEITEM hti)
{
	COMBOBOXEXITEM cbei;
	if (m_iSPos >= 0 && m_iEPos >= 0) {
		for (int i = m_iEPos; i >= m_iSPos; i--)
			DeleteItem(i);
	}

	m_iSPos = m_iEPos = -1;

	CRemoteTreeCtrl   *ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();
	PRTREEITEM_STRUCT prti   = (PRTREEITEM_STRUCT)ptree->GetItemData(hti);
	if (prti->nClass != TNS_FOLDER) {
		for (int i = 0; i < GetCount(); i++) {
			if ((HTREEITEM)GetItemData(i) == hti) {
				SetCurSel(i);
				break;
			}
		}
		return;
	}

	HTREEITEM htiParent = ptree->GetVolumeItem(hti);

	int     nImage, nSelectedImage;
	CString sText;

	cbei.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE | CBEIF_INDENT;

	for (int i = 1; i < GetCount(); i++) {
		if ((HTREEITEM)GetItemData(i) != htiParent)
			continue;

		m_iSPos = m_iEPos = i + 1;
		int iIndent = 2;
		while (htiParent != hti) {
			HTREEITEM htiChild, htiTemp = hti;
			while (htiParent != htiTemp) {
				htiChild = htiTemp;
				htiTemp  = ptree->GetParentItem(htiChild);
			}

			ptree->GetItemImage(htiChild, nImage, nSelectedImage);
			sText = ptree->GetItemText(htiChild);

			cbei.iItem          = m_iEPos++;
			cbei.iIndent        = iIndent++;
			cbei.iImage         = nImage;
			cbei.iSelectedImage = nSelectedImage;
			cbei.pszText        = (LPSTR)(LPCTSTR)sText;
			InsertItem(&cbei);
			SetItemData((int)cbei.iItem, (DWORD_PTR)htiChild);

			htiParent = htiChild;
		}

		m_iEPos--;
		SetCurSel(m_iEPos);
		break;
	}
}

BOOL CRemoteComboBox::PreTranslateMessage(MSG* pMsg)
{
	if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
	{
		if (SetDirectPath() == FALSE)
			return FALSE;
	}

	return CComboBoxEx::PreTranslateMessage(pMsg);
}

BOOL CRemoteComboBox::SetDirectPath(void)
{
	CString strTemp;
	CString strPath;
	CString token;
	int		ipos=0;
	UINT	ncount;
	HTREEITEM item=NULL;
	HCOMMLIB	hcl;


	CRemoteTreeCtrl *ptree = ((CRemoteView*)GetParent())->GetTreeCtrl();
	CMainFrame*		pmf = (CMainFrame*)AfxGetMainWnd();

	CRemoteView		*pView = (CRemoteView*)GetParent();
	CRemoteListCtrl *pList = pView->GetListCtrl();
	

	GetWindowText(strPath);
	
	strTemp = strPath;

	if (ptree->GetSelectedItem() == ptree->GetRootItem())
	{
		if (pmf->m_mapHcl.GetCount() == 2)
			item = ptree->GetChildItem(ptree->GetRootItem());
		else
		{
			MessageBox("서비스 볼륨을 먼저 선택해 주세요.");
			return FALSE;
		}
	}
	else
	{
		item = ptree->GetVolumeItem(ptree->GetSelectedItem()); // 볼륨노드
	}

	if (item == NULL)
	{
		MessageBox("존재하지 않는 볼륨입니다.");
		return FALSE;
	}

	pmf->m_mapHcl.Lookup(item,(void*&)hcl);

	PCLFILE_STRUCT pclf = cl_get_file_list(hcl,strTemp.Mid(ipos),0,&ncount);

	if( pclf == NULL)
	{
		MessageBox("존재하지 않는 경로 입니다.");
		return FALSE;
	}
	if(pclf->attr != CLFILEATTR_FOLDER)
	{// 파일이다.
		strTemp = strTemp.Left(strTemp.ReverseFind('/'));
	}

	token = strTemp.Tokenize("/",ipos); // 첫번째 경로

	while (token != "")
	{
		HTREEITEM titem = ptree->GetItem(item,token);
		if( titem == NULL)
			pView->AppendObject(item,true,token,0,"");
		item = ptree->GetItem(item,token);
		token= strTemp.Tokenize("/",ipos);
	};

	ptree->SelectItem(item);

/*
		if(pclf->attr != CLFILEATTR_FOLDER)
		{
			CString strName;
			strName = strPath.Mid(strPath.ReverseFind('/')+1);

			for (;;)
			{
				MSG					msg;
				
				while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}

				ncount = pList->GetItemCount();
				if(ncount > 1)
					break;
			}

			for (int i = 0; i < ncount; i++)
			{
				if (pList->GetItemText(i,0) == strName)
				{
					pList->SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
				}
			}
		}
*/

	return TRUE;
}
