#include "CMCEditor.h"
#include "resource.h"
#include "CMainEditor.h"
#include "CMCPosTable.h"
#include "CNewMC.h"

IMPLEMENT_DYNAMIC(CMCEditor, CDialog)

BEGIN_MESSAGE_MAP(CMCEditor, CDialog)
	ON_COMMAND(IDOK, &CMCEditor::OnEnterKey)
	ON_WM_SHOWWINDOW()
	ON_COMMAND(ID_MCPOS_TABLE, &CMCEditor::OnMcposTable)
	ON_COMMAND(ID_MCCHAR_TABLE, &CMCEditor::OnMccharTable)
	ON_COMMAND(ID_ADD_MC, &CMCEditor::OnAddMc)
	ON_COMMAND(ID_EDIT_MC, &CMCEditor::OnEditMc)
	ON_COMMAND(ID_DELETE_MC, &CMCEditor::OnDeleteMc)
	ON_COMMAND(ID_COPY_MC, &CMCEditor::OnCopyMc)
	ON_MESSAGE(MYDXLIST_SELCHANGED,&CMCEditor::OnDXListSelChange)
END_MESSAGE_MAP()

CMCEditor::CMCEditor(CWnd* pParent)
	: CDialog(IDD_CMCEDITOR, pParent)
{
}

CMCEditor::~CMCEditor()
{
}

void CMCEditor::UpdateContext()
{
	//更新子窗口的内容
	pMCPosTable.UpdateContext();
	pMCCharTable.UpdateContext();

	//更新自身窗口的内容
	auto mainEditor = (CMainEditor*)pMainEditor;
	//更新角色列表
	auto index = pMCList.GetCurSel();
	pMCList.Clear();
	mainEditor->GetDBMyCharacter_List(&mcList);
	for (auto& p : mcList)
		pMCList.AddItem(p.name, p.facePng);
	pMCList.SetCurSel(index);
}

void CMCEditor::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	static_id = (CStatic*)GetDlgItem(IDC_STATIC_ID);
	static_mclist = (CStatic*)GetDlgItem(IDC_STATIC_MCDXLIST);
	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);
	DDX_Control(pDX, IDC_LIST_SET, listbox_set);
	DDX_Control(pDX, IDC_LIST_EVENT, listbox_event);
	DDX_Control(pDX, IDC_LIST_MAP, listbox_map);
}

BOOL CMCEditor::OnInitDialog()
{
	CDialog::OnInitDialog();
	//设置应用程序图标
	auto hIcon = AfxGetApp()->LoadIcon(IDI_APPICON_TABLE);
	SetIcon(hIcon, true);
	//初始化控件
	DisableCtrl();//警用控件
	SetWindowTextW(L"角色编辑器");
	//初始化子窗口
	pMCList.Create(IDD_CMYDXLIST, this);
	pMCList.Init(48, 48, 48);
	pMCList.SetMenu(IDR_CMCEDITOR_MCLIST_RMENU, IDR_CMCEDITOR_MCLIST_RMENU_NOSEL);
	pMCPosTable.pMainEditor = pMainEditor;
	pMCPosTable.pParentWindow = this;
	pMCPosTable.Create(IDD_CMCPOS_TABLE, this);
	pMCCharTable.pMainEditor = pMainEditor;
	pMCCharTable.pParentWindow = this;
	pMCCharTable.Create(IDD_CMCCHAR_TABLE, this);
	picbox.Create(IDD_CMYPICBOX, this);
	//设置控件pMCList
	CRect rc;
	static_mclist->GetWindowRect(&rc);
	ScreenToClient(&rc);
	static_mclist->EnableWindow(false);
	static_mclist->ShowWindow(SW_HIDE);
	pMCList.MoveWindow(&rc);
	pMCList.ShowWindow(SW_SHOW);
	//设置picbox
	static_face->GetWindowRect(&rc);
	ScreenToClient(&rc);
	static_face->EnableWindow(false);
	static_face->ShowWindow(SW_HIDE);
	picbox.MoveWindow(&rc);
	picbox.ShowWindow(SW_SHOW);
	return 0;
}

void CMCEditor::OnCancel()
{
	ShowWindow(SW_HIDE);
}

void CMCEditor::OnEnterKey()
{
}


void CMCEditor::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);
	auto mainEditor = (CMainEditor*)pMainEditor;
	
	if (bShow)
		mainEditor->GetMenu()->GetSubMenu(CMAINEDITOR_MENUINDEX_STORYEDITOR)->CheckMenuItem(ID_MC_EDITOR, MF_BYCOMMAND | MF_CHECKED);
	else
	{
		mainEditor->GetMenu()->GetSubMenu(CMAINEDITOR_MENUINDEX_STORYEDITOR)->CheckMenuItem(ID_MC_EDITOR, MF_BYCOMMAND | MF_UNCHECKED);
		pMCPosTable.ShowWindow(SW_HIDE);
		pMCCharTable.ShowWindow(SW_HIDE);
	}
}

void CMCEditor::OnMcposTable()
{
	
	pMCPosTable.ShowWindow(pMCPosTable.IsWindowVisible() ? SW_HIDE : SW_SHOW);
}

void CMCEditor::OnMccharTable()
{
	
	pMCCharTable.ShowWindow(pMCCharTable.IsWindowVisible() ? SW_HIDE : SW_SHOW);
}

void CMCEditor::OnAddMc()
{
	
	//检查【角色定位表】和【角色性格表】是否都有添加至少一个项目
	auto mainEditor = (CMainEditor*)pMainEditor;
	if (mainEditor->GetDBTabelCount(DBTABLENAME_MYCHAR_POSITION) == 0)
	{
		MessageBoxW(L"在使用【添加角色】功能前，请先在【角色定位表】里添加一些“定位”", L"需要完成一些前置功能的填写", MB_ICONINFORMATION);
		return;
	}
	if (mainEditor->GetDBTabelCount(DBTABLENAME_MYCHAR_CHARACTER) == 0)
	{
		MessageBoxW(L"在使用【添加角色】功能前，请先在【角色性格表】里添加一些“性格”", L"需要完成一些前置功能的填写", MB_ICONINFORMATION);
		return;
	}
	//添加角色
	CNewMC dlg;
	dlg.pMainEditor = pMainEditor;
	if (dlg.DoModal() != IDOK)
		return;
	//复制图像
	CString facepng = mainEditor->ImportPNG(dlg.outMC.facePng);
	//添加角色并单独创建角色标签表
	auto db = mainEditor->OpenDB();
	CString sql;
	std::vector<char> cmdbuff_utf8;
	std::vector<char> pBuff_utf8_image;
	std::vector<char> pBuff_utf8_ideal;
	std::vector<char> pBuff_utf8_labelXML;
	sql.Format(L"INSERT INTO %s(facepng,lifetype,image,name,mcpos,mcchar,gender,ideal,label) VALUES ('%s',%d,?,'%s','%s','%s',%d,?,?);",
		DBTABLENAME_MYCHARACTER, facepng.GetString(), dlg.outMC.lifeType, dlg.outMC.name.GetString(), dlg.outMC.mcpos.GetString(), dlg.outMC.mcChar.GetString(), dlg.outMC.gender);
	UTF16ToUTF8(sql.GetString(), &cmdbuff_utf8);
	sqlite3_stmt* stmt;
	sqlite3_prepare(db, cmdbuff_utf8.data(), -1, &stmt, nullptr);
	//准备缓冲区
	int utf8_buffSize_image;
	UTF16ToUTF8(dlg.outMC.image.GetString(), &pBuff_utf8_image, &utf8_buffSize_image);
	int utf8_buffSize_ideal;
	UTF16ToUTF8(dlg.outMC.ideal.GetString(), &pBuff_utf8_ideal, &utf8_buffSize_ideal);
	int utf8_buffSize_labelXML;
	UTF16ToUTF8(dlg.outMC.labelXML.GetString(), &pBuff_utf8_labelXML, &utf8_buffSize_labelXML);
	//绑定位置
	sqlite3_bind_text(stmt, 1, pBuff_utf8_image.data(), utf8_buffSize_image, nullptr);
	sqlite3_bind_text(stmt, 2, pBuff_utf8_ideal.data(), utf8_buffSize_ideal, nullptr);
	sqlite3_bind_text(stmt, 3, pBuff_utf8_labelXML.data(), utf8_buffSize_labelXML, nullptr);
	sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	mainEditor->CloseDB(db);
	//更新列表
	UpdateContext();
}

void CMCEditor::OnEditMc()
{
	
	if (pMCList.Empty() || mcList.empty())
		return;
	if (pMCList.GetCurSel() < 0)
		return;
	auto mainEditor = (CMainEditor*)pMainEditor;
	auto& mc = mcList.at(pMCList.GetCurSel());
	//编辑角色
	CNewMC dlg;
	dlg.load = true;
	dlg.outMC = mc;
	dlg.pMainEditor = pMainEditor;
	if (dlg.DoModal() != IDOK)
		return;
	//复制图像
	CString facepng = mainEditor->ImportPNG(dlg.outMC.facePng);
	//添加角色并单独创建角色标签表
	auto db = mainEditor->OpenDB();
	CString sql;
	std::vector<char> cmdbuff_utf8;
	std::vector<char> pBuff_utf8_image;
	std::vector<char> pBuff_utf8_ideal;
	std::vector<char> pBuff_utf8_labelXML;
	sql.Format(L"UPDATE %s SET facepng = '%s',lifetype = %d,image = ?,name = '%s',mcpos = '%s',mcchar = '%s',gender = %d,ideal = ?,label = ? WHERE id = %d;",
		DBTABLENAME_MYCHARACTER, facepng.GetString(), dlg.outMC.lifeType, dlg.outMC.name.GetString(), dlg.outMC.mcpos.GetString(), dlg.outMC.mcChar.GetString(), dlg.outMC.gender, dlg.outMC.id);
	UTF16ToUTF8(sql.GetString(), &cmdbuff_utf8);
	sqlite3_stmt* stmt;
	sqlite3_prepare(db, cmdbuff_utf8.data(), -1, &stmt, nullptr);
	//准备缓冲区
	int utf8_buffSize_image;
	UTF16ToUTF8(dlg.outMC.image.GetString(), &pBuff_utf8_image, &utf8_buffSize_image);
	int utf8_buffSize_ideal;
	UTF16ToUTF8(dlg.outMC.ideal.GetString(), &pBuff_utf8_ideal, &utf8_buffSize_ideal);
	int utf8_buffSize_labelXML;
	UTF16ToUTF8(dlg.outMC.labelXML.GetString(), &pBuff_utf8_labelXML, &utf8_buffSize_labelXML);
	//绑定位置
	sqlite3_bind_text(stmt, 1, pBuff_utf8_image.data(), utf8_buffSize_image, nullptr);
	sqlite3_bind_text(stmt, 2, pBuff_utf8_ideal.data(), utf8_buffSize_ideal, nullptr);
	sqlite3_bind_text(stmt, 3, pBuff_utf8_labelXML.data(), utf8_buffSize_labelXML, nullptr);
	sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	mainEditor->CloseDB(db);
	//更新列表
	UpdateContext();
}

void CMCEditor::OnDeleteMc()
{
	
	auto mainEditor = (CMainEditor*)pMainEditor;
	auto& mc = mcList.at(pMCList.GetCurSel());
	CString id_str;
	id_str.Format(L"%d", mc.id);
	mainEditor->DelDBField(DBTABLENAME_MYCHARACTER, L"id", id_str);
	//更新列表
	UpdateContext();
}


void CMCEditor::OnCopyMc()
{
	
	auto mainEditor = (CMainEditor*)pMainEditor;
	auto& mc = mcList.at(pMCList.GetCurSel());
	auto db = mainEditor->OpenDB();
	CString sql;
	std::vector<char> cmdbuff_utf8;
	std::vector<char> pBuff_utf8_image;
	std::vector<char> pBuff_utf8_ideal;
	std::vector<char> pBuff_utf8_labelXML;
	sql.Format(L"INSERT INTO %s(facepng,lifetype,image,name,mcpos,mcchar,gender,ideal,label) VALUES ('%s',%d,?,'%s','%s','%s',%d,?,?);",
		DBTABLENAME_MYCHARACTER, mc.facePng.GetString(), mc.lifeType, mc.name.GetString(), mc.mcpos.GetString(), mc.mcChar.GetString(), mc.gender);
	UTF16ToUTF8(sql.GetString(), &cmdbuff_utf8);
	sqlite3_stmt* stmt;
	sqlite3_prepare(db, cmdbuff_utf8.data(), -1, &stmt, nullptr);
	//准备缓冲区
	int utf8_buffSize_image;
	UTF16ToUTF8(mc.image.GetString(), &pBuff_utf8_image, &utf8_buffSize_image);
	int utf8_buffSize_ideal;
	UTF16ToUTF8(mc.ideal.GetString(), &pBuff_utf8_ideal, &utf8_buffSize_ideal);
	int utf8_buffSize_labelXML;
	UTF16ToUTF8(mc.labelXML.GetString(), &pBuff_utf8_labelXML, &utf8_buffSize_labelXML);
	//绑定位置
	sqlite3_bind_text(stmt, 1, pBuff_utf8_image.data(), utf8_buffSize_image, nullptr);
	sqlite3_bind_text(stmt, 2, pBuff_utf8_ideal.data(), utf8_buffSize_ideal, nullptr);
	sqlite3_bind_text(stmt, 3, pBuff_utf8_labelXML.data(), utf8_buffSize_labelXML, nullptr);
	sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	mainEditor->CloseDB(db);
	//更新列表
	UpdateContext();
}

LRESULT CMCEditor::OnDXListSelChange(WPARAM wParam, LPARAM lParam)
{
	if (!pMCList.Empty() && !mcList.empty())
	{
		if (pMCList.GetCurSel() >= 0)
		{
			auto mainEditor = (CMainEditor*)pMainEditor;
			//显示选中的下标的角色信息
			auto& mc = mcList.at(pMCList.GetCurSel());
			CString id_str;
			id_str.Format(L"ID：%d", mc.id);
			static_id->SetWindowTextW(id_str.GetString());
			picbox.SetPic(mc.facePng);//头像
			radio_creature.SetCheck(false);//生命类型
			radio_sprite.SetCheck(false);
			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.Clear();//角色定位
			combobox_pos.AddString(mc.mcpos);
			combobox_pos.SetCurSel(0);
			combobox_char.Clear();//角色性格
			combobox_char.AddString(mc.mcChar);
			combobox_char.SetCurSel(0);
			radio_female.SetCheck(false);//性别
			radio_male.SetCheck(false);
			radio_none.SetCheck(false);
			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); //理想
			listbox_set.ResetContent();//设定实现表
			std::unordered_map<std::wstring, MyLabelXML> labelList;
			mainEditor->GetXMLLabel_Map(&labelList, mc.labelXML);
			for (auto& p : labelList)
				listbox_set.AddString(p.second.name);
			listbox_set.EnableWindow(true);
		}
		else
		{
			//清空角色信息
			static_id->SetWindowTextW(L"ID：");
			picbox.SetPic(L"");
			radio_creature.SetCheck(false);
			radio_sprite.SetCheck(false);
			edit_image.SetWindowTextW(L"");
			edit_label.SetWindowTextW(L"");
			combobox_pos.ResetContent();
			combobox_char.ResetContent();
			radio_female.SetCheck(false);
			radio_male.SetCheck(false);
			radio_none.SetCheck(false);
			edit_ideal.SetWindowTextW(L"");
			listbox_set.ResetContent();
			listbox_set.EnableWindow(false);
		}
	}
	return 0;
}

void CMCEditor::DisableCtrl()
{
	button_dir.EnableWindow(false);
	static_face->EnableWindow(false);
	radio_creature.EnableWindow(false);
	radio_sprite.EnableWindow(false);
	edit_image.EnableWindow(false);
	edit_label.EnableWindow(false);
	combobox_pos.EnableWindow(false);
	combobox_char.EnableWindow(false);
	radio_female.EnableWindow(false);
	radio_male.EnableWindow(false);
	radio_none.EnableWindow(false);
	edit_ideal.EnableWindow(false);
	listbox_set.EnableWindow(false);
	listbox_event.EnableWindow(false);
	listbox_map.EnableWindow(false);
}
