//..............................................................................
//
// TX/RX Filter layer
//

import "doc_Layer.jnc"
import "ui_ToggleUi.jnc"
import "ui_GroupBox.jnc"

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

class TxRxFilterLayer:
	doc.Layer,
	log.Filter {
	ui.ToggleUi m_isTxVisibleToggle;
	ui.ToggleUi m_isRxVisibleToggle;

	construct(doc.PluginHost* pluginHost);

	override void load(doc.Storage* storage);
	override void save(doc.Storage* storage);
	override void updateProperties();
	override bool errorcode applyProperties();
	override void restoreDefaultProperties();

	override bool filter(
		uint64_t timestamp,
		uint64_t recordCode,
		void const* p,
		size_t size
	);

protected:
	void createUi(doc.PluginHost* pluginHost);

	void onRebuildLog() {
		m_pluginHost.m_log.rebuild();
	}
}

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

TxRxFilterLayer.construct(doc.PluginHost* pluginHost) {
	basetype.construct(pluginHost);
	createUi(pluginHost);
	pluginHost.m_log.addFilter(this);
}

void TxRxFilterLayer.createUi(doc.PluginHost* pluginHost) {
	ui.VBoxLayout layout;
	ui.GroupBox groupBox("TX/RX Filter", layout);

	ui.Action applyAction("images/apply.png", "Rebuild log", onRebuildLog);
	ui.ToolBar applyToolBar;
	applyToolBar.addAction(applyAction);

	m_isTxVisibleToggle.create(
		layout,,
		pluginHost.m_propertyGrid,
		"Show TX",
		"Display TX data log records",
		true
	);

	m_isRxVisibleToggle.create(
		layout,
		applyToolBar,
		pluginHost.m_propertyGrid,
		"Show RX",
		"Display RX data log records",
		true
	);

	pluginHost.createForm(groupBox);
}

void TxRxFilterLayer.load(doc.Storage* storage) {
	m_isTxVisibleToggle := storage.readBool("isTxVisible", true);
	m_isRxVisibleToggle := storage.readBool("isRxVisible", true);
}

void TxRxFilterLayer.save(doc.Storage* storage) {
	storage.writeBool("isTxVisible", m_isTxVisibleToggle.m_value);
	storage.writeBool("isRxVisible", m_isRxVisibleToggle.m_value);
}

void TxRxFilterLayer.updateProperties() {
	m_isTxVisibleToggle.update();
	m_isRxVisibleToggle.update();
}

bool errorcode TxRxFilterLayer.applyProperties() {
	m_isTxVisibleToggle.apply();
	m_isRxVisibleToggle.apply();
	return true;
}

void TxRxFilterLayer.restoreDefaultProperties() {
	m_isTxVisibleToggle.m_value = true;
	m_isRxVisibleToggle.m_value = true;
}

bool TxRxFilterLayer.filter(
	uint64_t timestamp,
	uint64_t recordCode,
	void const* p,
	size_t size
) {
	switch (recordCode) {
	case log.StdRecordCode.Tx:
		return m_isTxVisibleToggle.m_value;

	case log.StdRecordCode.Rx:
		return m_isRxVisibleToggle.m_value;

	case log.StdRecordCode.TxRx:
		return m_isTxVisibleToggle.m_value || m_isRxVisibleToggle.m_value;

	default:
		return true; // everything else is visible
	}
}

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