#include "pch.h"

#include "RegDumpViewFDD.h"
#include "Platform.h"
#include "Screen_Sizes.h"

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

constexpr auto COLUMN_WIDTH_PRT = 78;
constexpr auto COLUMN_WIDTH_REG = 38;

constexpr auto COLNUM_HDD = 6;
constexpr auto ROWNUM_FDD = 4;
constexpr auto ROWNUM_HDD = 11;

static const CRegDumpDocPaneDlgViewHelper::ColParam_t arFDDCols[COLNUM_STD] =
{
	{COL_WIDTH | COL_COLOR, COLUMN_WIDTH_PRT, BG_COLOR},
	{COL_WIDTH, COLUMN_WIDTH, 0}
};


static const CRegDumpDocPaneDlgViewHelper::CellParam_t arFDDRows[ROWNUM_FDD][COLNUM_STD] =
{
	{ {COL_VALUE | COL_COLOR, IDS_MEMORY_177130IN,  BG_COLOR}, {0, 0, 0} },
	{ {COL_VALUE | COL_COLOR, IDS_MEMORY_177130OUT, BG_COLOR}, {0, 0, 0} },
	{ {COL_VALUE | COL_COLOR, IDS_MEMORY_177132IN,  BG_COLOR}, {0, 0, 0} },
	{ {COL_VALUE | COL_COLOR, IDS_MEMORY_177132OUT, BG_COLOR}, {0, 0, 0} },
};


static const CRegDumpDocPaneDlgViewHelper::ColParam_t arHDDCols[COLNUM_HDD] =
{
	{COL_WIDTH | COL_COLOR, COLUMN_WIDTH_REG, BG_COLOR},
	{COL_WIDTH, COLUMN_WIDTH, 0},
	{COL_WIDTH, COLUMN_WIDTH, 0},
	{COL_WIDTH | COL_COLOR, COLUMN_WIDTH_REG, BG_COLOR},
	{COL_WIDTH, COLUMN_WIDTH, 0},
	{COL_WIDTH, COLUMN_WIDTH, 0}
};

static const CRegDumpDocPaneDlgViewHelper::CellParam_t arHDDRows[ROWNUM_HDD][COLNUM_HDD] =
{
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_MODE, BG_COLOR}, {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_READ, BG_COLOR}, {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_WRITE, BG_COLOR}, {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_MODE, BG_COLOR}, {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_READ, BG_COLOR}, {COL_VALUE | COL_COLOR, IDS_HDDLST_HDR_WRITE, BG_COLOR}, },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F0, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F0, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F1, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F1, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F2, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F2, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F3, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F3, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F4, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F4, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F5, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F5, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F6, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F6, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F7, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_1F7, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_3F6, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_3F6, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
	{ {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_3F7, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               {COL_VALUE | COL_COLOR, IDS_HDDLST_REG_3F7, BG_COLOR},  {0, 0, 0},                                              {0, 0, 0},                                               },
};


/////////////////////////////////////////////////////////////////////////////
// CRegDumpViewFDD
IMPLEMENT_DYNAMIC(CRegDumpViewFDD, CRegDumpDocPaneDlgViewHelper)

CRegDumpViewFDD::CRegDumpViewFDD()
	= default;

CRegDumpViewFDD::~CRegDumpViewFDD()
    = default;

void CRegDumpViewFDD::DoDataExchange(CDataExchange *pDX)
{
	CRegDumpDocPaneDlgViewHelper::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_RDD_FDDREGS, m_listFDD);
	DDX_Control(pDX, IDC_LIST_RDD_HDDREGS, m_listHDD);
}

BEGIN_MESSAGE_MAP(CRegDumpViewFDD, CRegDumpDocPaneDlgViewHelper)
	ON_MESSAGE(WM_INITDIALOG, &CRegDumpViewFDD::HandleInitDialog)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_RDD_FDDREGS, &CRegDumpViewFDD::OnLvnItemchangedMdFddData)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// обработчики сообщений CRegDumpViewFDD

LRESULT CRegDumpViewFDD::HandleInitDialog(WPARAM wp, LPARAM lp)
{
	CRegDumpDocPaneDlgViewHelper::HandleInitDialog(wp, lp);
	CString strHeader;

	std::vector <ListParam_t> vList;

	// Создадим список регистров FDD

	vList.push_back({
		COLNUM_STD, // 2 колонки, 1-я - имя регистра, 2-я - его значение
		ROWNUM_FDD, // rows
		IDC_STATIC_RDD_FDDREGTITLE, // group ID
		&m_listFDD,		// list
		&arFDDCols[0],	// colparam
		&arFDDRows[0][0] // rowparam
	});

	// Создадим список регистров HDD

	vList.push_back({
		COLNUM_HDD, // 6 колонок, 1-я и 4-я - адрес порта/регистра, 2-я и 5-я - их значения по чтению, 3-я и 6-я - их значения по записи
		ROWNUM_HDD, // rows
		IDC_STATIC_RDD_HDDREGTITLE, // group ID
		&m_listHDD,		// list
		&arHDDCols[0],	// colparam
		&arHDDRows[0][0] // rowparam
	});

	int nMaxGrpWidth = 0;
	CPoint ptLT{ 0, 0 };
	for (auto &i : vList)
	{
		CreateList(i);
		ResizeList(i, ptLT, nMaxGrpWidth);
	}
	m_listHDD.SetRowBkColor(BG_COLOR, LSTHDD_ROW::MODE);

	// теперь сделаем группы все одной ширины
	for (auto &i : vList)
	{
		SetGrpWidth(GetDlgItem(i.nGrpId), nMaxGrpWidth);
	}

	//и под конец изменим размеры самого окна
	CRect rect;
	GetWindowRect(&rect);
	ScreenToClient(&rect);
	rect.bottom = ptLT.y - ::GetSystemMetrics(SM_CYCAPTION) / 2;
	rect.right = ptLT.x + nMaxGrpWidth;
	m_sizeDefault = rect.Size();
	return TRUE; // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CRegDumpViewFDD::DisplayRegDump()
{
	if (IsWindowVisible())
	{
		// выводим на экран данные FDD
		DisplayFDDData();
		DisplayHDDData();
	}
}

void CRegDumpViewFDD::DisplayFDDData()
{
	for (int i = LSTFDD_ROW::REG177130IN; i <= LSTFDD_ROW::REG177132OUT; ++i)
	{
		m_listFDD.SetItemWithModified(g_Platform.GetDebuggerPtr()->GetFDDData(i), i, LSTSTD_COL::VALUE);
	}
}

void CRegDumpViewFDD::DisplayHDDData()
{
	for (int i = LSTHDD_ROW::R1F0; i <= LSTHDD_ROW::R3F7; ++i)
	{
		m_listHDD.SetItemWithModified(g_Platform.GetDebuggerPtr()->GetDebugHDDRegs(0, i, true), i, LSTHDD_COL::VALUE11);
		m_listHDD.SetItemWithModified(g_Platform.GetDebuggerPtr()->GetDebugHDDRegs(0, i, false), i, LSTHDD_COL::VALUE12);
		m_listHDD.SetItemWithModified(g_Platform.GetDebuggerPtr()->GetDebugHDDRegs(1, i, true), i, LSTHDD_COL::VALUE21);
		m_listHDD.SetItemWithModified(g_Platform.GetDebuggerPtr()->GetDebugHDDRegs(1, i, false), i, LSTHDD_COL::VALUE22);
	}
}

void CRegDumpViewFDD::OnLvnItemchangedMdFddData(NMHDR *pNMHDR, LRESULT *pResult)
{
	auto pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: добавьте свой код обработчика уведомлений
	*pResult = S_OK;
}

