//..............................................................................
//
// Serial Tap Pro information grid entries
//

import "ui_InformationGrid.jnc"
import "io_abr.jncx"

//..............................................................................

class SerialTapProInfoSet {
protected:
	enum InfoId {
		Iface,
		Cts,
		Dsr,
		Ring,
		Dcd,
		Rts,
		Dtr,
		Break_0,
		Break_1,
		Overflows,
		Abr_0,
		Abr_1,
		_Count,
	}

	ui.InformationValue* m_infoTable[InfoId._Count];

public:
	void create(
		ui.InformationGrid* infoGrid,
		ui.InformationGroup* infoGroup = null
	);

	void enable(
		bool isEnabled,
		bool isAbrEnabled_0,
		bool isAbrEnabled_1
	);

	void setIface(SerialTapProIface iface) {
		m_infoTable[InfoId.Iface].m_value = getSerialTapProIfaceString(iface);
	}

	void setOverflows(SerialTapProOverflows overflows);

	void updateLines(
		SerialTapProLines lines,
		SerialTapProLines mask = (SerialTapProLines)-1
	);

	void updateAbr(
		size_t channel,
		io.AutoBaudRate* abr
	);

protected:
	void setLineState(
		InfoId infoId,
		bool state
	) {
		m_infoTable[infoId].m_value = state ? "on" : "off";
		m_infoTable[infoId].m_valueColor = state ? ui.StdColor.BrightRed : ui.StdColor.BrightBlack;
	}
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

void SerialTapProInfoSet.create(
	ui.InformationGrid* infoGrid,
	ui.InformationGroup* infoGroup
) {
	m_infoTable[InfoId.Iface] = infoGrid.createValue(infoGroup,,, "Interface");
	m_infoTable[InfoId.Dtr] = infoGrid.createValue(infoGroup,,, "DTR");
	m_infoTable[InfoId.Rts] = infoGrid.createValue(infoGroup,,, "RTS");
	m_infoTable[InfoId.Dsr] = infoGrid.createValue(infoGroup,,, "DSR");
	m_infoTable[InfoId.Cts] = infoGrid.createValue(infoGroup,,, "CTS");
	m_infoTable[InfoId.Dcd] = infoGrid.createValue(infoGroup,,, "DCD");
	m_infoTable[InfoId.Ring] = infoGrid.createValue(infoGroup,,, "RI");
	m_infoTable[InfoId.Break_0] = infoGrid.createValue(infoGroup,,, "BREAK (TX)");
	m_infoTable[InfoId.Break_1] = infoGrid.createValue(infoGroup,,, "BREAK (RX)");
	m_infoTable[InfoId.Overflows] = infoGrid.createValue(infoGroup,,, "Overflow");

	ui.InformationGroup* subGroup = infoGrid.createGroup(infoGroup,, "Auto baud rate detector");
	m_infoTable[InfoId.Abr_0] = infoGrid.createValue(subGroup,,, "Baud rate (TX)");
	m_infoTable[InfoId.Abr_1] = infoGrid.createValue(subGroup,,, "Baud rate (RX)");

	updateLines(0); // set all off
}

void SerialTapProInfoSet.enable(
	bool isEnabled,
	bool isAbrEnabled_0,
	bool isAbrEnabled_1
) {
	for (size_t i = 0; i < InfoId.Abr_0; i++)
		m_infoTable[i].m_isEnabled = isEnabled;

	m_infoTable[InfoId.Abr_0].m_isEnabled = isEnabled && isAbrEnabled_0;
	m_infoTable[InfoId.Abr_1].m_isEnabled = isEnabled && isAbrEnabled_1;
}

void  SerialTapProInfoSet.setOverflows(SerialTapProOverflows overflows) {
	if (!overflows) {
		m_infoTable[InfoId.Overflows].m_value = "none";
		m_infoTable[InfoId.Overflows].m_valueColor = ui.StdColor.BrightBlack;
		return;
	}

	m_infoTable[InfoId.Overflows].m_value = getSerialTapProOverflowsString(overflows);
	m_infoTable[InfoId.Overflows].m_valueColor = ui.StdColor.BrightRed;
}

void SerialTapProInfoSet.updateLines(
	SerialTapProLines lines,
	SerialTapProLines mask
) {
	if (mask & SerialTapProLines.Cts)
		setLineState(InfoId.Cts, lines & SerialTapProLines.Cts);

	if (mask & SerialTapProLines.Dsr)
		setLineState(InfoId.Dsr, lines & SerialTapProLines.Dsr);

	if (mask & SerialTapProLines.Dcd)
		setLineState(InfoId.Dcd, lines & SerialTapProLines.Dcd);

	if (mask & SerialTapProLines.Ring)
		setLineState(InfoId.Ring, lines & SerialTapProLines.Ring);

	if (mask & SerialTapProLines.Rts)
		setLineState(InfoId.Rts, lines & SerialTapProLines.Rts);

	if (mask & SerialTapProLines.Dtr)
		setLineState(InfoId.Dtr, lines & SerialTapProLines.Dtr);

	if (mask & SerialTapProLines.Break_0)
		setLineState(InfoId.Break_0, lines & SerialTapProLines.Break_0);

	if (mask & SerialTapProLines.Break_1)
		setLineState(InfoId.Break_1, lines & SerialTapProLines.Break_1);
}

void SerialTapProInfoSet.updateAbr(
	size_t channel,
	io.AutoBaudRate* abr
) {
	m_infoTable[InfoId.Abr_0 + channel].m_value = abr.m_baudRate ?
		$"%1 bps %2 bits"(abr.m_baudRate, abr.m_frameBits) :
		"unknown";
}

//..............................................................................
