// EditVMSItemsPg.cpp : インプリメンテーション ファイル
//

#include "stdafx.h"
#include "dm2gded.h"
#include "EditVMSItemsPg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditVMSItemsPg ダイアログ


void CEditVMSItemsPg::Construction()
{
	//{{AFX_DATA_INIT(CEditVMSItemsPg)
	m_iCat1 = -1;
	m_iCat2 = -1;
	m_iSelSrc = -1;
	m_iSelDyn = -1;
	//}}AFX_DATA_INIT
}


void CEditVMSItemsPg::DoDataExchange(CDataExchange* pDX)
{
	CEditVPg::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditVMSItemsPg)
	DDX_Control(pDX, IDC_COMBO_CAT2, m_wndCat2);
	DDX_Control(pDX, IDC_COMBO_CAT1, m_wndCat1);
	DDX_Control(pDX, IDC_LIST_ITEMS, m_wndListDyn);
	DDX_Control(pDX, IDC_LIST_ITEM, m_wndListSrc);
	DDX_CBIndex(pDX, IDC_COMBO_CAT1, m_iCat1);
	DDX_CBIndex(pDX, IDC_COMBO_CAT2, m_iCat2);
	DDX_LBIndex(pDX, IDC_LIST_ITEM, m_iSelSrc);
	DDX_LBIndex(pDX, IDC_LIST_ITEMS, m_iSelDyn);
	//}}AFX_DATA_MAP

	if (pDX->m_bSaveAndValidate) {
		m_strText = m_tl.Mk();
	}
}


BEGIN_MESSAGE_MAP(CEditVMSItemsPg, CEditVPg)
	//{{AFX_MSG_MAP(CEditVMSItemsPg)
	ON_WM_CREATE()
	ON_CBN_SELCHANGE(IDC_COMBO_CAT1, OnSelchangeComboCat1)
	ON_CBN_SELCHANGE(IDC_COMBO_CAT2, OnSelchangeComboCat2)
	ON_LBN_DBLCLK(IDC_LIST_ITEM, OnDblclkListItem)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
	ON_BN_CLICKED(IDC_BUTTON_SORT, OnButtonSort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditVMSItemsPg メッセージ ハンドラ

BOOL CEditVMSItemsPg::OnInitDialog() 
{
	CEditVPg::OnInitDialog();
	
	m_wndCat1.ShowWindow(m_fUseCatA ? SW_SHOW : SW_HIDE);
	m_wndCat2.ShowWindow(m_fUseCatA ? SW_HIDE : SW_SHOW);

	InitDynContents();
	
	return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
	              // 例外: OCX プロパティ ページの戻り値は FALSE となります
}

int CEditVMSItemsPg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	m_tl.Parse(m_strInitText, m_fUseCatA ? rtmAll : rtmC);

	if (CEditVPg::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	return 0;
}

void CEditVMSItemsPg::OnSelchangeComboCat1() 
{
	OnSel();
}

void CEditVMSItemsPg::OnSelchangeComboCat2() 
{
	OnSel();
}

void CEditVMSItemsPg::OnDblclkListItem() 
{
	OnButtonAdd();
}

void CEditVMSItemsPg::OnSel()
{
	if (!UpdateData()) return;

	BOOL (CEditVCtx::*fn)(int, int, CString &) = m_fUseCatA
		? CEditVCtx::Get6CatItemName
		: CEditVCtx::Get1CatItemName
		;
	int iCat = GetCat();

	m_wndListSrc.SetRedraw(FALSE);
	m_wndListSrc.ResetContent();

	CString strText, strName;
	for (int i = 0; i < 128; i++) {
		if (!(m_vctx.*fn)(Sel2RTT(iCat), i, strName)) strName.Empty();
		strText.Format(strName.IsEmpty() ? "$%02X" : "$%02X - %s", 0U +i, (LPCTSTR)strName);
		m_wndListSrc.AddString(strText);
	}
	m_wndListSrc.SetRedraw(TRUE);
	m_wndListSrc.Invalidate();
}

void CEditVMSItemsPg::OnButtonAdd() 
{
	if (!UpdateData()) return;

	CDM2RTToken t(Sel2RTT(GetCat()), m_iSelSrc);
	m_tl.push_back(t);
	m_wndListDyn.AddString(FormatDynName(t));
}

void CEditVMSItemsPg::OnButtonRemove() 
{
	if (!UpdateData()) return;

	UINT i = m_iSelDyn;
	if (i < m_tl.size()) {
		VERIFY(m_wndListDyn.DeleteString(i) != LB_ERR);
		m_wndListDyn.SetCurSel(__min(m_wndListDyn.GetCount() - 1, i));

		CDM2RTTokenList::iterator iterPos = m_tl.begin();
		advance(iterPos, i);
		m_tl.erase(iterPos);
	}
}

CString CEditVMSItemsPg::FormatDynName(CDM2RTToken &rt)
{
	BOOL (CEditVCtx::*fn)(int, int, CString &) = m_fUseCatA
		? CEditVCtx::Get6CatItemName
		: CEditVCtx::Get1CatItemName
		;
	int iCat = rt.nRTT;

	LPCTSTR pszCat = "";
	if (m_fUseCatA) {
		switch (iCat) {
		case rttC: pszCat = "Chest"; break;
		case rttWeapons: pszCat = "Weapon"; break;
		case rttClothes: pszCat = "Clothe"; break;
		case rttScrolls: pszCat = "Scroll"; break;
		case rttPotions: pszCat = "Potion"; break;
		case rttMisc: pszCat = "Misc"; break;
		}
	} else {
		switch (iCat) {
		case rttC: pszCat = "Creature"; break;
		}
	}

	CString strText, strName;

	if (!(m_vctx.*fn)(iCat, rt.iIdx, strName)) strName.Empty();
	strText.Format(strName.IsEmpty() ? "%s: $%02X" : "%s: $%02X - %s", pszCat, 0U +rt.iIdx, (LPCTSTR)strName);

	return strText;
}

void CEditVMSItemsPg::OnButtonSort() 
{
	/*
	set<CDM2RTToken> s;
	{
		CDM2RTTokenList::iterator
			iterPos = m_tl.begin(),
			iterEnd = m_tl.end();
		while (iterPos != iterEnd) s.insert(*iterPos), iterPos++;
	}
	m_tl.clear();
	{
		set<CDM2RTToken>::iterator
			iterPos = s.begin(),
			iterEnd = s.end();
		while (iterPos != iterEnd) m_tl.push_back(*iterPos), iterPos++;
	}
	*/
	m_tl.sort();
	m_tl.unique();

	InitDynContents();
}

void CEditVMSItemsPg::InitDynContents()
{
	CDM2RTTokenList::iterator
		iterPos = m_tl.begin(),
		iterEnd = m_tl.end();
	m_wndListDyn.SetRedraw(FALSE);
	m_wndListDyn.ResetContent();
	while (iterPos != iterEnd) {
		CString strText = FormatDynName(*iterPos);
		m_wndListDyn.AddString(strText);
		iterPos++;
	}
	m_wndListDyn.SetRedraw(TRUE);
	m_wndListDyn.Invalidate();
}
