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

IMPLEMENT_DYNAMIC(CMCPosTable, CDialog)

BEGIN_MESSAGE_MAP(CMCPosTable, CDialog)
	ON_COMMAND(IDOK, &CMCPosTable::OnEnterKey)
	ON_WM_SIZE()
	ON_COMMAND(ID_ADD_LABEL, &CMCPosTable::OnAddLabel)
	ON_COMMAND(ID_RENAME_LABEL, &CMCPosTable::OnRenameLabel)
	ON_COMMAND(ID_DELETE_LABEL, &CMCPosTable::OnDeleteLabel)
	ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()

CMCPosTable::CMCPosTable(CWnd* pParent)
	: CDialog(IDD_CMCPOS_TABLE, pParent)
{
}

CMCPosTable::~CMCPosTable()
{
}

void CMCPosTable::UpdateContext()
{
	list_mcchar.ResetContent();
	auto mainEditor = (CMainEditor*)pMainEditor;
	std::vector<CString> labelList;
	mainEditor->GetDBField_List(&labelList, DBTABLENAME_MYCHAR_POSITION, L"mcpos");
	for (auto& p : labelList)
		list_mcchar.AddString(p);
}

void CMCPosTable::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	groupbox_mcchar = (CStatic*)GetDlgItem(IDC_STATIC_MCPOS);
	DDX_Control(pDX, IDC_LIST_MCPOS, list_mcchar);
	groupbox_same = (CStatic*)GetDlgItem(IDC_STATIC_MCPOS_SAME);
	DDX_Control(pDX, IDC_LIST_MCPOS_SAME, list_same);
}

BOOL CMCPosTable::OnInitDialog()
{
	CDialog::OnInitDialog();
	//设置应用程序图标
	auto hIcon = AfxGetApp()->LoadIcon(IDI_APPICON_TABLE);
	SetIcon(hIcon, true);
	//设置控件
	list_mcchar.SetMenu(IDR_CMCPREP_RMENU, IDR_CMCPREP_RMENU_NOSEL);
	return 0;
}

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

void CMCPosTable::OnEnterKey()
{
}

void CMCPosTable::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	
	if (!groupbox_mcchar)
		return;
	int dx = 20;
	int dy = 20;
	int c = 36;
	groupbox_mcchar->MoveWindow(dx, dy, cx / 2 - dx - dx / 2, cy - dy * 2);
	list_mcchar.MoveWindow(dx, dy + c, cx / 2 - dx - dx / 2, cy - dy * 2 - c);
	groupbox_same->MoveWindow(dx / 2 + cx / 2, dy, cx / 2 - dx - dx / 2, cy - dy * 2);
	list_same.MoveWindow(dx / 2 + cx / 2, dy + c, cx / 2 - dx - dx / 2, cy - dy * 2 - c);

}

void CMCPosTable::OnAddLabel()
{
	
	//添加标签
	COSLabel dlg;
	dlg.SetWindowTitleAndLabel(L"添加角色定位标签", L"标签名：");
	if (dlg.DoModal() != IDOK)
		return;
	//在数据库里添加标签
	auto mainEditor = (CMainEditor*)pMainEditor;
	mainEditor->SetDBField(DBTABLENAME_MYCHAR_POSITION, L"mcpos", L"", dlg.outLabel);
	UpdateContext();
}


void CMCPosTable::OnRenameLabel()
{
	
	//重命名标签
	int index = list_mcchar.GetCurSel();
	CString mcposStr;
	list_mcchar.GetText(index, mcposStr);

	COSLabel dlg;
	dlg.outLabel = mcposStr;
	dlg.load = true;
	dlg.SetWindowTitleAndLabel(L"重命名角色定位标签", L"标签名：");
	if (dlg.DoModal() != IDOK)
		return;
	if (mcposStr == dlg.outLabel)
		return;
	//更新标签
	auto mainEditor = (CMainEditor*)pMainEditor;
	mainEditor->SetDBField(DBTABLENAME_MYCHAR_POSITION, L"mcpos", mcposStr, dlg.outLabel);
	UpdateContext();
}


void CMCPosTable::OnDeleteLabel()
{
	
	//删除标签
	int index = list_mcchar.GetCurSel();
	CString mcposStr;
	list_mcchar.GetText(index, mcposStr);

	auto mainEditor = (CMainEditor*)pMainEditor;
	mainEditor->DelDBField(DBTABLENAME_MYCHAR_POSITION, L"mcpos", mcposStr);
	UpdateContext();
}


void CMCPosTable::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	auto parentWindow = (CMCEditor*)pParentWindow;
	if (bShow)
		parentWindow->GetMenu()->GetSubMenu(CMCEDITOR_MENUINDEX_WINDOW)->CheckMenuItem(ID_MCPOS_TABLE, MF_BYCOMMAND | MF_CHECKED);
	else
		parentWindow->GetMenu()->GetSubMenu(CMCEDITOR_MENUINDEX_WINDOW)->CheckMenuItem(ID_MCPOS_TABLE, MF_BYCOMMAND | MF_UNCHECKED);
}
