// ResourceViewerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ResourceViewer.h"
#include "ResourceViewerDlg.h"



#include <Stuff\Stuff.hpp>

#include <Adept\Adept.hpp>

#include <Compost\Compost.hpp>

#include <gosfx\gosfx.hpp>

#include <elementrenderer\elementrenderer.hpp>

#include <GameOS\GameOS.hpp>
#include <DLLPlatform\DLLPlatform.hpp>

#include <Adept\Resource.hpp>
#include <buildnum\buildnum.h>


HGOSHEAP Heap;

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


/////////////////////////////////////////////////////////////////////////////
// CResourceViewerDlg dialog

CResourceViewerDlg::CResourceViewerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CResourceViewerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CResourceViewerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	HINSTANCE	hInstance;
	hInstance = AfxGetResourceHandle();

	InitGameOS( hInstance, NULL, "\0" );


	Stuff::ArmorLevel = 3;

	Stuff::InitializeClasses();
	MidLevelRenderer::InitializeClasses(NULL, 2048*8);
	Compost::InitializeClasses();
	gosFX::InitializeClasses();
	ElementRenderer::InitializeClasses();
	Adept::InitializeClasses();

	Adept::ResourceManager::Instance = new Adept::ResourceManager;
	Check_Object(Adept::ResourceManager::Instance);

	Heap = gos_CreateMemoryHeap("Default");
	Check_Pointer(Heap);
	gos_PushCurrentHeap(Heap);

}

CResourceViewerDlg::~CResourceViewerDlg()
{

	gos_PopCurrentHeap();

	delete Adept::ResourceManager::Instance;
	Adept::ResourceManager::Instance = NULL;
	
	Adept::TerminateClasses();
	ElementRenderer::TerminateClasses();
	gosFX::TerminateClasses();
	Compost::TerminateClasses();
	MidLevelRenderer::TerminateClasses();
	Stuff::TerminateClasses();
	
	ExitGameOS();

}

void CResourceViewerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CResourceViewerDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CResourceViewerDlg, CDialog)
	//{{AFX_MSG_MAP(CResourceViewerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOpenButton)
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_RES_LIST, OnColumnclickResList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceViewerDlg message handlers


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


int ColumnArray[2];

BOOL CResourceViewerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CListCtrl *list = (CListCtrl *)GetDlgItem( IDC_RES_LIST );
	ColumnArray[RESID_COLUMN] = list->InsertColumn( RESID_COLUMN, "ID", LVCFMT_LEFT, 50);
	ColumnArray[NAME_COLUMN] = list->InsertColumn( NAME_COLUMN, "Name", LVCFMT_LEFT, 450);
	ColumnArray[SIZE_COLUMN] = list->InsertColumn( SIZE_COLUMN, "Size", LVCFMT_LEFT, 50);

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CResourceViewerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CResourceViewerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CResourceViewerDlg::OnOpenButton() 
{

	CFileDialog dlg(true, ".mw4", NULL, NULL, "Mech4 Resource (*.mw4)|*.mw4||");


	if (dlg.DoModal() != IDOK)
	{
		return;
	}


	Adept::ResourceFile *res_file;

	res_file = Adept::ResourceManager::Instance->OpenResourceFile(
		dlg.GetPathName(),
		NULL,
		10,
		VER_CONTENTVERSION,
		false,
		false
		);

	if (res_file)
	{

		CListCtrl *list = (CListCtrl *)GetDlgItem( IDC_RES_LIST );
		Check_Pointer(list);
		list->DeleteAllItems();

		int count = 0;
		Adept::Resource res;
		res.First(res_file);

		while (res.ReadAndNext())
		{

			CString data_name = res.GetName();
			CString data_size;
			data_size.Format("%d", res.GetSize());   

			char buffer[255];
			Adept::ResourceID res_id =  res.GetResourceID();
			sprintf(buffer, "%d:%d", res_id.m_fileID, res_id.m_recordID);

			CString res_id_string = buffer;

			int item = list->InsertItem( count, res_id_string );
			list->SetItemText( item, ColumnArray[SIZE_COLUMN], data_size );
			list->SetItemText( item, ColumnArray[NAME_COLUMN], data_name );
			count++;
		}


		Adept::ResourceManager::Instance->CloseAllButCore();

	}

}


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 
	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));
}


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;

}



void CResourceViewerDlg::OnColumnclickResList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	CListCtrl *list = (CListCtrl *)GetDlgItem( IDC_RES_LIST );
	
	
	LPNMLISTVIEW pnmv = (LPNMLISTVIEW) pNMHDR; 

	for(int i=0;i<list->GetItemCount();i++)
		list->SetItemData(i,i);

	
	switch(pnmv->iSubItem)
	{
		case NAME_COLUMN:
			list->SortItems(CompName, (LPARAM) list);
			break;

		case SIZE_COLUMN:
			list->SortItems(CompSize, (LPARAM) list);
			break;

		case RESID_COLUMN:
			list->SortItems(CompResID, (LPARAM) list);
			break;
	}




	*pResult = 0;
}



