// ResourceBrowserView.cpp : implementation of the CResourceBrowserView class
//

#include "stdafx.h"
#include "ResourceBrowser.h"

#include "ResourceBrowserDoc.h"
#include "ResourceBrowserView.h"
#include "DepDlg.h"
#include <Adept\Adept.hpp>
#include <Stuff\Stuff.hpp>

using namespace Stuff;

#define NAME_COLUMN 0
#define BUILD_COLUMN 1
#define RESID_COLUMN 2
#define SIZE_COLUMN  3


int ColumnArray[4];



/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView

IMPLEMENT_DYNCREATE(CResourceBrowserView, CListView)

BEGIN_MESSAGE_MAP(CResourceBrowserView, CListView)
//{{AFX_MSG_MAP(CResourceBrowserView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView construction/destruction

CResourceBrowserView::CResourceBrowserView()
{
	m_dwDefaultStyle |= ( LVS_REPORT|LVS_SHOWSELALWAYS);//|LVS_SHAREIMAGELISTS );
}

CResourceBrowserView::~CResourceBrowserView()
{
}

BOOL CResourceBrowserView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView drawing

void CResourceBrowserView::OnDraw(CDC* pDC)
{
	CResourceBrowserDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView printing

BOOL CResourceBrowserView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CResourceBrowserView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

void CResourceBrowserView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView diagnostics

#ifdef _DEBUG
void CResourceBrowserView::AssertValid() const
{
	CListView::AssertValid();
}

void CResourceBrowserView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CResourceBrowserDoc* CResourceBrowserView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CResourceBrowserDoc)));
	return (CResourceBrowserDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserView message handlers

void CResourceBrowserView::OnInitialUpdate() 
{
	CListView::OnInitialUpdate();
	int col=0;
	ColumnArray[NAME_COLUMN] =GetListCtrl().InsertColumn( col++, "FileName", LVCFMT_LEFT, 200);
	ColumnArray[BUILD_COLUMN] =GetListCtrl().InsertColumn( col++, "InBuild", LVCFMT_LEFT, 100);
	ColumnArray[RESID_COLUMN] =GetListCtrl().InsertColumn( col++, "ResourceID", LVCFMT_LEFT, 100);
	ColumnArray[SIZE_COLUMN] =GetListCtrl().InsertColumn( col++, "Size", LVCFMT_LEFT, 100);
	
}

int CResourceBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	//	GetListCtrl().Set.Create(WS_CHILD|LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SHAREIMAGELISTS,CRect(0,0,200,200),this,NULL);
	//  GetListCtrl().ShowWindow(SW_SHOW);	
	
	return 0;
}

void CResourceBrowserView::OnSize(UINT nType, int cx, int cy) 
{
	CListView::OnSize(nType, cx, cy);
	
}

static int CALLBACK 
CompName(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// lParamSort contains a pointer to the list view control.
	// The lParam of an item is just its index.
	CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
	CString    strItem1 = pListCtrl->GetItemText(lParam1, NAME_COLUMN);
	CString    strItem2 = pListCtrl->GetItemText(lParam2, NAME_COLUMN);
	
	return strItem1.CompareNoCase(strItem2);
}

static int CALLBACK 
CompBuild(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// lParamSort contains a pointer to the list view control.
	// The lParam of an item is just its index.
	CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
	CString    strItem1 = pListCtrl->GetItemText(lParam1, BUILD_COLUMN);
	CString    strItem2 = pListCtrl->GetItemText(lParam2, BUILD_COLUMN);
	
	return strItem1.CompareNoCase(strItem2);
}


static int CALLBACK 
CompResID(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// lParamSort contains a pointer to the list view control.
	// The lParam of an item is just its index.
	CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
	CString    strItem1 = pListCtrl->GetItemText(lParam1, RESID_COLUMN);
	CString    strItem2 = pListCtrl->GetItemText(lParam2, RESID_COLUMN);
	
	int val1a,val2a;
	int val1b,val2b;
	val1a=atoi(strItem1.Left(strItem1.Find(':')));
	val1b=atoi(strItem1.Mid(strItem1.Find(':')+1));
	val2a=atoi(strItem2.Left(strItem1.Find(':')));
	val2b=atoi(strItem2.Mid(strItem1.Find(':')+1));
	
	if(val1a==val2a)
		return val1b>val2b;
	else
		return val1a>val2a;
	
}

static int CALLBACK 
	CompSize(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
   // lParamSort contains a pointer to the list view control.
   // The lParam of an item is just its index.
   CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
   return atoi(pListCtrl->GetItemText(lParam1, SIZE_COLUMN))>atoi(pListCtrl->GetItemText(lParam2, SIZE_COLUMN));
}

void CResourceBrowserView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	
	
	
	for(int i=0;i<GetListCtrl().GetItemCount();i++)
		GetListCtrl().SetItemData(i,i);
	
	
	switch(pNMListView->iSubItem)
	{
	case NAME_COLUMN:
		GetListCtrl().SortItems(CompName, (LPARAM) &GetListCtrl());
		break;
		
	case BUILD_COLUMN:
		GetListCtrl().SortItems(CompBuild, (LPARAM) &GetListCtrl());
		break;
	case RESID_COLUMN:
		GetListCtrl().SortItems(CompResID, (LPARAM) &GetListCtrl());
		break;
	case SIZE_COLUMN:
		GetListCtrl().SortItems(CompSize, (LPARAM) &GetListCtrl());
		break;
		
	}
	
	
	
	*pResult = 0;
}

void CResourceBrowserView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if(pSender!=this)
	{
		CResourceBrowserDoc *doc=(CResourceBrowserDoc *)GetDocument();
		GetListCtrl().DeleteAllItems();
		
		WIN32_FIND_DATA file_data;
		HANDLE file_find;
		file_find=FindFirstFile(doc->m_CurPath+"*.*",&file_data);
		
		CString file_name;
		
		if(file_find != INVALID_HANDLE_VALUE)
		{
			do
			{
				file_name=file_data.cFileName;
				int count = 0;
				if(!(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && file_name.CompareNoCase("vssver.scc") )  
				{
					int item = GetListCtrl().InsertItem( count++,file_name );
					GetListCtrl().SetItemText(item,ColumnArray[BUILD_COLUMN],GetResultString(doc->m_CurPath+file_name));
					GetListCtrl().SetItemText(item,ColumnArray[RESID_COLUMN],GetResID(doc->m_CurPath+file_name));
					GetListCtrl().SetItemText(item,ColumnArray[SIZE_COLUMN],GetResSize(doc->m_CurPath+file_name));
				}
			}
			while (FindNextFile(file_find,&file_data));
		}
		
		
	}
	
}


#define EXTMAX 6
char *UnknownExts[EXTMAX]={".obb",".armature",".table",".data",".raw",".defines"};


const char *CResourceBrowserView::GetResultString(CString str)
{
	
	if(!str.CompareNoCase("vssver.scc")) return "N\\A";

	

	bool inres=InResources(str);

	if(inres) return  "Yes";

	if(!str.Right(strlen(".build")).CompareNoCase(".build")) return "N\\A";
	
	for(int i=0;i<EXTMAX;i++)
	if(!str.Right(strlen(UnknownExts[i])).CompareNoCase(UnknownExts[i]))
	{
	return "Unknown";
	}






	return "No";

}

bool CResourceBrowserView::InResources(CString &str)
{
	Adept::Resource res(str);
	return res.DoesResourceExist();
	
}

CString CResourceBrowserView::GetResID(CString str)
{
	Adept::Resource res(str);

	if(res.DoesResourceExist())
	{
		CString fstr;
		Adept::ResourceID res_id =  res.GetResourceID();
		fstr.Format("%d:%d", res_id.m_fileID, res_id.m_recordID);
		return fstr;
	}
	else return ("N/A");
}

CString CResourceBrowserView::GetResSize(CString str)
{
	Adept::Resource res(str);
	if(res.DoesResourceExist())
	{
			CString data_size;
			data_size.Format("%d", res.GetSize());   

	return data_size;
	}
	else return ("N/A");
}


bool CResourceBrowserView::ReferencedInFile(CString str2,CString str)
{
NotationFile note_file(str);
NotationFile::PageIterator *pages=note_file.MakePageIterator();
Page *page;

while(page=pages->ReadAndNext())
{
	Page::NoteIterator *notes=page->MakeNoteIterator();
	Note *note;
	const char *ccs;
	while(note=notes->ReadAndNext())
	{
	note->GetEntry(&ccs);
	if(!str2.CompareNoCase(ccs)) return true;
	}
}

return false;
}

void CResourceBrowserView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NMITEMACTIVATE *dat=(NMITEMACTIVATE *)pNMHDR;

	if(dat->iItem>=0)
	{
	ShowDependancies(GetListCtrl().GetItemText((dat->iItem),ColumnArray[NAME_COLUMN]));
	}
	
	*pResult = 0;
}

void CResourceBrowserView::ShowDependancies(CString str)
{
		CResourceBrowserDoc *doc=(CResourceBrowserDoc *)GetDocument();
		CDepDlg dlg(doc->m_CurPath+str,this);
		dlg.DoModal();
}


