// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "ImageGrinder.h"

#include "MainFrm.h"

#include "TextureLibFrame.h"
#include "FeatureLibFrame.h"
#include <Compost\FeatureGrid.hpp>


/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_TOOLS_CONVERTFGDTOIGDATA, OnToolsConvertfgdtoigdata)
	ON_COMMAND(ID_TOOLS_EXPORTFGDIGDATA, OnToolsExportfgdigdata)
	ON_COMMAND(ID_TOOLS_EXPORTALLIGDATAFILES, OnToolsExportalligdatafiles)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
TextLibWin=NULL;	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);


	return 0;
}

void CMainFrame::Init()
{
	gosASSERT(!TextLibWin);
TextLibWin=new CTextureLibFrame;
TextLibWin->Create(this);
TextLibWin->ShowWindow(SW_SHOW);
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

BOOL CMainFrame::OnIdle()
{
	if (TextLibWin) {
		return TextLibWin->OnIdle();
	}
	return FALSE;
}


void CMainFrame::OnToolsConvertfgdtoigdata() 
{

	CFileDialog dlg(TRUE,NULL,NULL,NULL,"Game Compost Files (*.fgd)|*.fgd||");	
	
	if(dlg.DoModal()==IDOK)
	{
	Compost::FeatureGrid *FGrid;
	Compost::FeaturePool *FPool;
	CString iname=dlg.GetPathName();

	FPool = new Compost::FeaturePool();
	Register_Pointer(FPool);
	
	Check_Object(Stuff::FileStreamManager::Instance);
	Stuff::FileStream *fstr=new Stuff::FileStream;
	Register_Object(fstr);
	fstr->Open(iname,Stuff::FileStream::ReadOnly);
	FPool->LoadIndex(fstr);
	FGrid = new Compost::FeatureGrid(fstr, FPool);
	Register_Pointer(FGrid);
	Unregister_Object(fstr);
	delete fstr;

	CString oname=dlg.GetFileName();
	oname=oname.Left(oname.GetLength()-dlg.GetFileExt().GetLength());
	oname+="IGData";
	CFileDialog dlg2(FALSE,"IGData",oname,NULL,"Image Gringer files (*.IGData)|*.IGData||");	
	if(dlg2.DoModal()==IDOK)
	{
		oname=dlg2.GetPathName();
		Stuff::NotationFile note_file(oname);
		note_file.DeleteAllPages();
		Stuff::Page *page=note_file.SetPage(NULL);
		Check_Object(page);
		page->SetEntry("!concatenate","no");

		FPool->SaveIndex(&note_file);
		FGrid->SaveGrid(&note_file);	
		note_file.Save();
	}
	Unregister_Pointer(FPool);
	delete FPool;
	Unregister_Pointer(FGrid);
	delete FGrid;
	

	}
	
}

void CMainFrame::OnToolsExportfgdigdata() 
{
	CFileDialog dlg(TRUE,NULL,NULL,NULL,"Image Gringer files (*.IGData)|*.IGData||");	
	
	if(dlg.DoModal()==IDOK)
	{
	ExportFGD(dlg.GetPathName());
	}

	
}

void CMainFrame::OnToolsExportalligdatafiles() 
{

	WIN32_FIND_DATA file_data;
	HANDLE file_find;
	file_find=FindFirstFile("Content\\Maps\\*.*",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		do
		{
			CString dir_name=file_data.cFileName;
			if(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY && 
				dir_name!="."  && dir_name!=".." )
			{
				ExportFGD("Content\\Maps\\"+dir_name+"\\"+dir_name+".IgData");
			}
		}
		while (FindNextFile(file_find,&file_data));
	}




}


void CMainFrame::ExportFGD(CString &path)
{
	Compost::FeatureGrid *FGrid;
	Compost::FeaturePool *FPool;
	CString iname=path;

	FPool = new Compost::FeaturePool();
	Register_Pointer(FPool);
	
	Check_Object(Stuff::FileStreamManager::Instance);
	Stuff::NotationFile note_file(iname);
	FPool->LoadIndex(&note_file);
	FGrid = new Compost::FeatureGrid(&note_file, FPool);
	Register_Pointer(FGrid);

	CString oname=path.Left(path.ReverseFind('.'))+".fgd";
		Check_Object(Stuff::FileStreamManager::Instance);
		Stuff::FileStream *fstr=new Stuff::FileStream;
		Register_Object(fstr);
		fstr->Open(oname,Stuff::FileStream::WriteOnly);
		FPool->SaveIndex(fstr);
		FGrid->SaveGrid(fstr,0,0);	
		Unregister_Object(fstr);
		delete fstr;
	Unregister_Pointer(FPool);
	delete FPool;
	Unregister_Pointer(FGrid);
	delete FGrid;
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	extern void DestroyTNTThread();

	DestroyTNTThread();

	if (TextLibWin) {
		TextLibWin->DestroyWindow();
		TextLibWin = NULL;
	}
	CMDIFrameWnd::OnClose();
}
