// InsertTileDlg2.cpp : implementation file
//

#include "stdafx.h"
#include "mapeditor.h"
#include "InsertTileDlg2.h"

#include "MainFrm.h"
#include "MapEditorDoc.h"
#include "MapEditorView.h"
#include <direct.h>


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

/////////////////////////////////////////////////////////////////////////////
// CInsertTileDlg2 dialog


CInsertTileDlg2::CInsertTileDlg2(CWnd* pParent /*=NULL*/)
	: CDialog(CInsertTileDlg2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInsertTileDlg2)
	m_strTilename = _T("");
	m_strTileFilename = _T("");
	//}}AFX_DATA_INIT
}


void CInsertTileDlg2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInsertTileDlg2)
	DDX_Text(pDX, IDC_EDIT_TILENAME, m_strTilename);
	DDX_Text(pDX, IDC_EDIT_TILEFILENAME, m_strTileFilename);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CInsertTileDlg2, CDialog)
	//{{AFX_MSG_MAP(CInsertTileDlg2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInsertTileDlg2 message handlers

void CInsertTileDlg2::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
	CMapEditorView* pView = (CMapEditorView*)pFrame->GetActiveView();
	if(!m_strTileFilename.IsEmpty() && !m_strTilename.IsEmpty())
	{
		UINT Result = pView->m_ctrlTileList.FindStringExact( -1, m_strTilename ); 
		
		if( Result == LB_ERR )
		{
			int usetile, i, j;//,k;
//			char strpathtile[256];
			char buf[20];
			CFile WriteTileFile;

			CString strFilename;
			memset(buf,0x00,sizeof(buf));
			wsprintf(buf,m_strTilename);
			pView->m_ctrlTileList.AddString(m_strTilename);
			pView->m_ctrlBaseTileList.AddString(m_strTilename);

			char strpath[256];
			_chdir(pView->m_strCurrentDir);
			wsprintf(strpath,".\\Tile\\%d\\Mapdata.trd",pView->m_pBackground->m_projectInfo.m_useTileSetIndex);
			if(WriteTileFile.Open(strpath, CFile::modeReadWrite))
			{
				WriteTileFile.Seek(20,CFile::begin);
				WriteTileFile.Read(&usetile,sizeof(int));
				usetile++;
				WriteTileFile.Seek(20,CFile::begin);
				WriteTileFile.Write(&usetile,sizeof(int));
				WriteTileFile.SeekToEnd();
				WriteTileFile.Write(buf,sizeof(char)*20);
				for(i=0;i<BASETILE_CENTER_NUM;i++)
				{
					memset(buf,0x00,sizeof(buf));
					wsprintf(buf,"%s00%d.tga",m_strTileFilename,i);
					_chdir(pView->m_strCurrentDir);
					wsprintf(strpath,".\\Res-Tile\\%s",buf);
					pView->m_pBackground->InsertTile(strpath);
					WriteTileFile.Write(buf,sizeof(char)*20);
				}
				for(i=1;i<4;i++)
				{
					for(j=1;j<5;j++)
					{
						memset(buf,0x00,sizeof(buf));
						wsprintf(buf,"%s%d%d0.tga",m_strTileFilename,i,j);
						_chdir(pView->m_strCurrentDir);
						wsprintf(strpath,".\\Res-Tile\\%s",buf);
						pView->m_pBackground->InsertTile(strpath);
						WriteTileFile.Write(buf,sizeof(char)*20);
					}
					
/*					j = 1;
					memset(buf,0x00,sizeof(buf));
					wsprintf(buf,"%s%d%d0.tga",m_strTileFilename,i,j);
					_chdir(pView->m_strCurrentDir);
					wsprintf(strpath,".\\Res-Tile\\%s",buf);
					pView->m_pBackground->InsertTile(strpath);
					WriteTileFile.Write(buf,sizeof(char)*20);
*/
				}
				WriteTileFile.Close();
			}
			else
			{
				if(WriteTileFile.Open(strpath,CFile::modeCreate | CFile::modeReadWrite))
				{

					usetile=1;
					WriteTileFile.Seek(20,CFile::begin);
					WriteTileFile.Write(&usetile,sizeof(int));
					WriteTileFile.Write(buf,sizeof(char)*20);
					for(i=0;i<BASETILE_CENTER_NUM;i++)
					{
						memset(buf,0x00,sizeof(buf));
						wsprintf(buf,"%s00%d.tga",m_strTileFilename,i);
						_chdir(pView->m_strCurrentDir);
						wsprintf(strpath,".\\Res-Tile\\%s",buf);
						pView->m_pBackground->InsertTile(strpath);
						WriteTileFile.Write(buf,sizeof(char)*20);
					}
					for(i=1;i<4;i++)
					{
						for(j=1;j<5;j++)
						{
							memset(buf,0x00,sizeof(buf));
							wsprintf(buf,"%s%d%d0.tga",m_strTileFilename,i,j);
							_chdir(pView->m_strCurrentDir);
							wsprintf(strpath,".\\Res-Tile\\%s",buf);
							pView->m_pBackground->InsertTile(strpath);
							WriteTileFile.Write(buf,sizeof(char)*20);
						}
					}
					WriteTileFile.Close();
				}
			}	
		}
	}
	
	CDialog::OnOK();
}
