#include "CNewMC.h"
#include "resource.h"
#include "CMainEditor.h"
#include "COSLabel.h"

IMPLEMENT_DYNAMIC(CNewMC, CDialog)

BEGIN_MESSAGE_MAP(CNewMC, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DIR, &CNewMC::OnBnClickedButtonDir)
	ON_BN_CLICKED(IDOK, &CNewMC::OnBnClickedOk)
	ON_COMMAND(ID_ADD_LABEL, &CNewMC::OnAddLabel)
	ON_COMMAND(ID_RENAME_LABEL, &CNewMC::OnRenameLabel)
	ON_COMMAND(ID_DELETE_LABEL, &CNewMC::OnDeleteLabel)
END_MESSAGE_MAP()

CNewMC::CNewMC(CWnd* pParent)
	: CDialog(IDD_CMCEDITOR_NEWMC, pParent)
{
}

CNewMC::~CNewMC()
{
}

void CNewMC::UpdateContext()
{
	auto mainEditor = (CMainEditor*)pMainEditor;
	listbox_set.ResetContent();
	for (auto& p : labelMap)
		listbox_set.AddString(p.second.name.GetString());
}

void CNewMC::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BUTTON_DIR, button_dir);
	static_face = (CStatic*)GetDlgItem(IDC_STATIC_FACE);
	DDX_Control(pDX, IDC_RADIO_CREATURE, radio_creature);
	DDX_Control(pDX, IDC_RADIO_SPRITE, radio_sprite);
	DDX_Control(pDX, IDC_EDIT_IMAGE, edit_image);
	DDX_Control(pDX, IDC_EDIT_NAME, edit_label);
	DDX_Control(pDX, IDC_COMBO_POS, combobox_pos);
	DDX_Control(pDX, IDC_COMBO_CHAR, combobox_char);
	DDX_Control(pDX, IDC_RADIO_FEMALE, radio_female);
	DDX_Control(pDX, IDC_RADIO_MALE, radio_male);
	DDX_Control(pDX, IDC_RADIO_NONE, radio_none);
	DDX_Control(pDX, IDC_EDIT_IDEAL, edit_ideal);
	static_set = (CStatic*)GetDlgItem(IDC_STATIC_SET);
	DDX_Control(pDX, IDC_LIST_SET, listbox_set);
}

BOOL CNewMC::OnInitDialog()
{
	CDialog::OnInitDialog();
	auto mainEditor = (CMainEditor*)pMainEditor;
	//设置子窗口
	picbox.Create(IDD_CMYPICBOX, this);
	CRect rc;
	static_face->GetWindowRect(&rc);
	ScreenToClient(&rc);
	static_face->EnableWindow(false);
	static_face->ShowWindow(SW_HIDE);
	picbox.MoveWindow(&rc);
	picbox.ShowWindow(SW_SHOW);
	//初始化控件
	radio_creature.SetCheck(true);//生命类型默认勾选生物
	radio_none.SetCheck(true);//性别默认勾选无
	//初始化组合框
	std::vector<CString> strList;
	mainEditor->GetDBField_List(&strList, DBTABLENAME_MYCHAR_POSITION, L"mcpos");
	for (auto& p : strList)
		combobox_pos.AddString(p);
	mainEditor->GetDBField_List(&strList, DBTABLENAME_MYCHAR_CHARACTER, L"mcchar");
	for (auto& p : strList)
		combobox_char.AddString(p);
	listbox_set.SetMenu(IDR_CMCPREP_RMENU, IDR_CMCPREP_RMENU_NOSEL);
	//如果是编辑模式
	if (load)
	{
		SetWindowTextW(L"编辑人物");
		auto& mc = outMC;
		picbox.SetPic(mc.facePng);//头像
		if (mc.lifeType == 0)
			radio_creature.SetCheck(true);
		else if (mc.lifeType == 1)
			radio_sprite.SetCheck(true);
		edit_image.SetWindowTextW(mc.image);//印象
		edit_label.SetWindowTextW(mc.name);//名字
		combobox_pos.SetCurSel(combobox_pos.FindString(0, mc.mcpos.GetString()));//角色定位
		combobox_char.SetCurSel(combobox_char.FindString(0, mc.mcChar.GetString()));;//角色性格
		if (mc.gender == 0)
			radio_none.SetCheck(true);
		else if (mc.gender == 1)
			radio_female.SetCheck(true);
		else if (mc.gender == 2)
			radio_male.SetCheck(true);
		edit_ideal.SetWindowTextW(mc.ideal); //理想
		mainEditor->GetXMLLabel_Map(&labelMap, mc.labelXML);//设定实现表
		UpdateContext();
	}
	return 0;
}


void CNewMC::OnBnClickedButtonDir()
{
	
	CFileDialog fd(true, TEXT(""), TEXT(""), OFN_FILEMUSTEXIST, TEXT("PNG图像文件(*.png)|*.png|BMP图像文件(*.bmp)|*.bmp|JPEG图像文件(*.jpg)|*.jpg|所有文件(*.*)|*.*||"), this);
	if (fd.DoModal() != IDOK)
		return;
	auto mainEditor = (CMainEditor*)pMainEditor;
	outMC.facePng = fd.GetPathName();
	picbox.SetPic(outMC.facePng);
}

void CNewMC::OnBnClickedOk()
{
	
	//确认各个表单是否填写
	auto mainEditor = (CMainEditor*)pMainEditor;
	//是否选择生命类型
	if (radio_creature.GetCheck() == false && 
		radio_sprite.GetCheck() == false)
	{
		MessageBoxW(L"必须选择一个生命类型", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//名字是否填写
	CString name;
	edit_label.GetWindowTextW(name);
	if (name.GetLength() == 0)
	{
		MessageBoxW(L"必须填写名字", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//角色定位
	CString mcpos;
	combobox_pos.GetWindowTextW(mcpos);
	if (mcpos.GetLength() == 0)
	{
		MessageBoxW(L"必须选择一个角色定位", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//角色性格
	CString mcchar;
	combobox_char.GetWindowTextW(mcchar);
	if (mcchar.GetLength() == 0)
	{
		MessageBoxW(L"必须选择一个角色性格", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//是否选择性别
	if (radio_female.GetCheck() == false && 
		radio_male.GetCheck() == false &&
		radio_none.GetCheck() == false)
	{
		MessageBoxW(L"必须选择一个性别", L"填写信息未完成", MB_ICONINFORMATION);
		return;
	}
	//填写返回值
	if (radio_creature.GetCheck())
		outMC.lifeType = 0;
	else if (radio_sprite.GetCheck())
		outMC.lifeType = 1;
	edit_image.GetWindowTextW(outMC.image);
	outMC.name = name;
	outMC.mcpos = mcpos;
	outMC.mcChar = mcchar;
	if (radio_none.GetCheck())
		outMC.gender = 0;
	else if (radio_female.GetCheck())
		outMC.gender = 1;
	else if (radio_male.GetCheck())
		outMC.gender = 2;
	edit_ideal.GetWindowTextW(outMC.ideal);
	outMC.labelXML = mainEditor->GetXMLString(labelMap);
	CDialog::OnOK();
}


void CNewMC::OnAddLabel()
{
	
	COSLabel dlg;
	dlg.SetWindowTitleAndLabel(L"添加设定实现标签", L"设定名：");
	if (dlg.DoModal() != IDOK)
		return;
	MyLabelXML label;
	label.name = dlg.outLabel.GetString();
	if (labelMap.find(label.name.GetString()) != labelMap.end())
	{
		CString str;
		str.Format(L"添加失败\n已有同名设定：%s", label.name.GetString());
		MessageBoxW(str.GetString(), L"添加失败", MB_ICONINFORMATION);
		return;
	}
	labelMap.insert(std::make_pair(label.name.GetString(), label));
	UpdateContext();
}


void CNewMC::OnRenameLabel()
{
	
	int sel = listbox_set.GetCurSel();
	CString name_prep;
	listbox_set.GetText(sel, name_prep);
	COSLabel dlg;
	dlg.load = true;
	dlg.outLabel = name_prep;
	dlg.SetWindowTitleAndLabel(L"重命名标签", L"设定名：");
	if (dlg.DoModal() != IDOK)
		return;
	if (dlg.outLabel == name_prep)
		return;
	MyLabelXML label = labelMap.at(name_prep.GetString());
	label.name = dlg.outLabel;
	labelMap.erase(name_prep.GetString());
	if (labelMap.find(label.name.GetString()) != labelMap.end())
	{
		CString str;
		str.Format(L"重命名失败\n已有同名设定：%s", label.name.GetString());
		MessageBoxW(str.GetString(), L"重命名失败", MB_ICONINFORMATION);
		return;
	}
	labelMap.insert(std::make_pair(label.name.GetString(), label));
	UpdateContext();
}


void CNewMC::OnDeleteLabel()
{
	
	int sel = listbox_set.GetCurSel();
	CString name;
	listbox_set.GetText(sel, name);
	labelMap.erase(name.GetString());
	UpdateContext();
}
