#include "CNewMap.h"
#include "resource.h"

IMPLEMENT_DYNAMIC(CNewMap, CDialog)

BEGIN_MESSAGE_MAP(CNewMap, CDialog)
	ON_BN_CLICKED(IDOK, &CNewMap::OnBnClickedOk)
END_MESSAGE_MAP()

CNewMap::CNewMap(CWnd* pParent)
	: CDialog(IDD_CMAPEDITOR_NEWMAP, pParent)
{
}

CNewMap::~CNewMap()
{
}

void CNewMap::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_MAPFILE, edit_mapFile);
	DDX_Control(pDX, IDC_EDIT_MAPNAME, edit_mapName);
	DDX_Control(pDX, IDC_EDIT_MAP_W, edit_mapW);
	DDX_Control(pDX, IDC_EDIT_MAP_H, edit_mapH);
}

BOOL CNewMap::OnInitDialog()
{
	CDialog::OnInitDialog();
	//设置控件初始值
	if (load)
	{
		SetWindowText(L"编辑地图属性");
		edit_mapFile.SetWindowTextW(outMapFile.GetString());
		edit_mapName.SetWindowTextW(outMapName.GetString());
	}
	SetDlgItemInt(IDC_EDIT_MAP_W, outMapW);
	SetDlgItemInt(IDC_EDIT_MAP_H, outMapH);
	return 0;
}


void CNewMap::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CString mapFile;
	edit_mapFile.GetWindowTextW(mapFile);
	if (mapFile.GetLength() == 0)
	{
		MessageBoxW(L"必须填写地图文件名", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	if (!load || 
		(load && outMapFile != mapFile))
	{
		//是否有修改文件名
		//检查本地map目录下是否存在同名地图文件
		CString pxmfile;
		pxmfile.Format(L"./data/map/%s.pxm", mapFile.GetString());
		FILE* fp;
		_wfopen_s(&fp, pxmfile.GetString(), L"rb");
		if (fp)
		{
			fclose(fp);
			//pxm文件存在
			CString err;
			err.Format(L"存在同名地图文件%s", pxmfile.GetString());
			MessageBoxW(err.GetString(), L"填写信息无效", MB_ICONINFORMATION);
			return;
		}
		//检查脚本文件重名
		CString luafile;
		luafile.Format(L"./data/map/%s.lua", mapFile.GetString());
		if (fp)
		{
			fclose(fp);
			//lua文件存在
			CString err;
			err.Format(L"存在同名地图文件%s", luafile.GetString());
			MessageBoxW(err.GetString(), L"填写信息无效", MB_ICONINFORMATION);
			return;
		}
	}
	CString mapName;
	edit_mapName.GetWindowTextW(mapName);
	CString mapW;
	edit_mapW.GetWindowTextW(mapW);
	if (mapW.GetLength() == 0)
	{
		MessageBoxW(L"必须填写地图宽度", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	if (GetDlgItemInt(IDC_EDIT_MAP_W) > 0xffff)
	{
		MessageBoxW(L"地图宽度超出65535", L"填写信息无效", MB_ICONINFORMATION);
		return;
	}
	CString mapH;
	edit_mapH.GetWindowTextW(mapH);
	if (mapH.GetLength() == 0)
	{
		MessageBoxW(L"必须填写地图高度", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	if (GetDlgItemInt(IDC_EDIT_MAP_H) > 0xffff)
	{
		MessageBoxW(L"地图高度超出65535", L"填写信息无效", MB_ICONINFORMATION);
		return;
	}
	//输出
	outMapFile = mapFile;
	outMapName = mapName;
	outMapW = GetDlgItemInt(IDC_EDIT_MAP_W);
	outMapH = GetDlgItemInt(IDC_EDIT_MAP_H);
	CDialog::OnOK();
}
