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

#include "stdafx.h"
#include "ConsoleApp.h"
#include "RTDropTarget.h"
#include "MainFrm.h"
#include "RemoteView.h"
#include "RemoteTreeCtrl.h"


bool IsPathItself(CString sSrcPath, CString sDstPath);

// CRTDropTarget

IMPLEMENT_DYNAMIC(CRTDropTarget, COleDropTarget)
CRTDropTarget::CRTDropTarget()
{
}

CRTDropTarget::~CRTDropTarget()
{
}


BEGIN_MESSAGE_MAP(CRTDropTarget, COleDropTarget)
END_MESSAGE_MAP()


// CRTDropTarget 메시지 처리기입니다.
DROPEFFECT CRTDropTarget::OnDragEnter(CWnd* pwnd, COleDataObject* podo, DWORD dwKeyState, CPoint point)
{
	m_hwndFocused = GetFocus();

	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

	if (!pmf->m_bDragFromRemote) {
		if (!podo->IsDataAvailable(CF_HDROP))
			return DROPEFFECT_NONE;

		((CMainFrame*)AfxGetMainWnd())->m_saDragSource.RemoveAll();
		if (!GetDragFileInfo(podo))
			return DROPEFFECT_NONE;
	}

	CRemoteTreeCtrl* ptree = (CRemoteTreeCtrl*)pwnd;

	ptree->SetFocus();

	m_htiSelected = m_htiPrev = ptree->GetSelectedItem();

	m_dePrev = DROPEFFECT_NONE;

	return OnDragOver(pwnd, podo, dwKeyState, point);
}

DROPEFFECT CRTDropTarget::OnDragOver(CWnd* pwnd, COleDataObject* podo, DWORD dwKeyState, CPoint point)
{
	DROPEFFECT de = DROPEFFECT_NONE;

	if (((CMainFrame*)AfxGetMainWnd())->m_saDragSource.GetCount() == 0)
		return de;

	CRemoteTreeCtrl* ptree = (CRemoteTreeCtrl*)pwnd;

	HTREEITEM hti = ptree->HitTest(point);
	if (!hti)
		return de;

	ptree->SetItemState(m_htiPrev, NULL, TVIS_SELECTED);
	ptree->SetItemState(m_htiPrev = hti, TVIS_SELECTED, TVIS_SELECTED);

	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

	if (hti != ptree->GetRootItem()) {
		if (!pmf->m_bDragFromRemote)
			de = DROPEFFECT_COPY;
		else {
			CString sTargetPath = ptree->GetPath(hti);

			BOOL bAvail = TRUE;
			if (ptree->GetVolumeItem(hti) != pmf->m_htiDragTarget)
				bAvail = FALSE;
			else {
				for (int i = 0; i < pmf->m_saDragSource.GetCount(); i++) {
					if (IsPathItself(pmf->m_saDragSource[i], sTargetPath)) {
						bAvail = FALSE;
						break;
					}
				}
			}

			if (bAvail)
				de = dwKeyState & MK_CONTROL ? DROPEFFECT_COPY : DROPEFFECT_MOVE;
		}
	}

	m_dePrev = de;

	return de;
}

void CRTDropTarget::OnDragLeave(CWnd* pwnd)
{
	CRemoteTreeCtrl* ptree = (CRemoteTreeCtrl*)pwnd;

	ptree->SetItemState(m_htiPrev, NULL, TVIS_SELECTED);
	ptree->SetItemState(m_htiSelected, TVIS_SELECTED, TVIS_SELECTED);

	if (m_hwndFocused)
		CWnd::FromHandle(m_hwndFocused)->SetFocus();
}

BOOL CRTDropTarget::OnDrop(CWnd* pwnd, COleDataObject* podo, DROPEFFECT de, CPoint point)
{
	HTREEITEM htiTarget = m_htiPrev;

	if (m_dePrev != DROPEFFECT_NONE) {
		CRemoteTreeCtrl* ptree = (CRemoteTreeCtrl*)pwnd;
		CMainFrame*      pmf   = (CMainFrame*)AfxGetMainWnd();

		if (!pmf->m_bDragFromRemote) {
			pmf->m_sDragTarget   = ptree->GetPath(htiTarget);
			pmf->m_htiDragTarget = ptree->GetVolumeItem(htiTarget);

			pmf->Upload(false);
		}
		else {
			pmf->m_sDragTarget = ptree->GetPath(htiTarget);

			pmf->BeginWaitMessage();

			CStringArray saSucceed;
			if (de == DROPEFFECT_COPY)
				pmf->CopyRemoteObject(pmf->m_htiDragTarget, pmf->m_saDragSource, pmf->m_sDragTarget, saSucceed );
			else
				pmf->MoveRemoteObject(pmf->m_htiDragTarget, pmf->m_saDragSource, pmf->m_sDragTarget, saSucceed );

			CRemoteView* pview = (CRemoteView*)ptree->GetParent();
			for (int i = 0; i < saSucceed.GetCount(); i++) {
				HTREEITEM hti = ptree->GetItem(pmf->m_htiDragTarget, saSucceed[i]);
				if (!hti)
					continue;

				CString sName = saSucceed[i].Mid(saSucceed[i].ReverseFind('/') + 1);
				if (!ptree->IsExistsChild(htiTarget, sName))
					pview->AppendObject(htiTarget, true, sName, 0, "");
				if (de == DROPEFFECT_MOVE)
					ptree->DeleteItem(hti);
			}

			pmf->EndWaitMessage();

			if (saSucceed.GetCount() > 0)
				pmf->m_pRemoteView->RefreshItem();
		}
	}

	OnDragLeave(pwnd);

	return TRUE;
}

BOOL CRTDropTarget::GetDragFileInfo(COleDataObject* podo)
{
	STGMEDIUM sm;
	FORMATETC fe = {CF_HDROP, (DVTARGETDEVICE FAR*)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};

	if (!podo->GetData(CF_HDROP, &sm, &fe))
		return FALSE;

	HDROP hdrop = (HDROP)sm.hGlobal;

	int nDragCount = ::DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0);
	if (nDragCount == 0)
		return FALSE;

	CMainFrame* pmf = (CMainFrame*)AfxGetMainWnd();

	char szFile[MAX_PATH];
	for (int i = 0; i < nDragCount; i++) {
		::DragQueryFile(hdrop, i, szFile, sizeof(szFile));

		CString sFile(szFile);
		if (sFile.Find('\\') < 0) {
			pmf->m_saDragSource.RemoveAll();
			break;
		}

		pmf->m_saDragSource.Add(sFile);
	}

	if (pmf->m_saDragSource.GetCount() == 0)
		return FALSE;

	return TRUE;
}
