import "gui_PropertyGrid.jnc"

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

class RegexPattern
{
	gui.GroupProperty* m_groupProp;
	gui.BoolProperty* m_isTxEnabledProp;
	gui.BoolProperty* m_isRxEnabledProp;
	gui.StringProperty* m_regexProp;
	gui.ColorProperty* m_colorProp;
	gui.FlagProperty* m_fontFlagProp;

	reactor m_uiReactor;

	construct(
		gui.PropertyGrid* propertyGrid,
		gui.Property* beforeProp,
		size_t index
		);

	gui.TextAttr buildTextAttr();

	void load(doc.Storage* storage);
	void save(doc.Storage* storage);
}

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

RegexPattern.construct(
	gui.PropertyGrid* propertyGrid,
	gui.Property* beforeProp,
	size_t index
	)
{
	m_groupProp = propertyGrid.createGroupProperty(
		null,
		beforeProp,
		$"Pattern #$(index + 1)",
		"Pattern properties"
		);

	m_isTxEnabledProp = propertyGrid.createBoolProperty(
		m_groupProp,,
		"Apply to TX",
		"Select whether or not to search for this pattern in TX stream"
		);

	m_isRxEnabledProp = propertyGrid.createBoolProperty(
		m_groupProp,,
		"Apply to RX",
		"Select whether or not to search for this pattern in RX stream"
		);

	m_regexProp = propertyGrid.createStringProperty(
		m_groupProp,,
		"Regular expression",
		"Write a regular expression describing this pattern"
		);

	m_colorProp = propertyGrid.createColorProperty(
		m_groupProp,,
		"Color",
		"Select a color to apply when this pattern is found"
		);

	gui.FlagPropertyOption fontFlagTable[] =
	{
		{ "Bold",      gui.FontFlags.Bold },
		{ "Italic",    gui.FontFlags.Italic },
		{ "Underline", gui.FontFlags.Underline },
		{ "Strikeout", gui.FontFlags.Strikeout },
	}

	m_fontFlagProp = propertyGrid.createFlagProperty(
		m_groupProp,,
		"Font",
		"Specify font decorations to apply when this pattern is found",
		fontFlagTable,
		countof(fontFlagTable)
		);

	struct Initializer
	{
		char const* m_regex;
		uint_t m_color;
	}

	static Initializer initializerTable[] =
	{
		{ r"[\x00-\x1f]+", gui.StdColor.LightRed },
		{ "[0-9]+",        gui.StdColor.LightPurple },
		{ "[A-Za-z]+",     gui.StdColor.LightBlue },
	};

	index %= countof(initializerTable);

	m_isTxEnabledProp.m_value = true;
	m_isRxEnabledProp.m_value = true;
	m_regexProp.m_value = initializerTable[index].m_regex;
	m_colorProp.m_value = initializerTable[index].m_color;

	m_uiReactor.start();
}

gui.TextAttr RegexPattern.buildTextAttr()
{
	gui.TextAttr attr =
	{
		m_backColor = gui.ColorFlags.Transparent,
		m_textColor = m_colorProp.m_value,
		m_fontFlags = (gui.FontFlags) m_fontFlagProp.m_value,
	}

	return attr;
}

void RegexPattern.load(doc.Storage* storage)
{
	m_isTxEnabledProp.m_value = storage.readBool("isTxEnabled");
	m_isRxEnabledProp.m_value = storage.readBool("isRxEnabled");
	m_regexProp.m_value = storage.readString("regex");
	m_colorProp.m_value = storage.readInt("color");
	m_fontFlagProp.m_value = storage.readInt("fontFlags");
}

void RegexPattern.save(doc.Storage* storage)
{
	storage.writeBool("isTxEnabled", m_isTxEnabledProp.m_value);
	storage.writeBool("isRxEnabled", m_isRxEnabledProp.m_value);
	storage.writeString("regex", m_regexProp.m_value);
	storage.writeInt("color", m_colorProp.m_value);
	storage.writeInt("fontFlags", m_fontFlagProp.m_value);
}

reactor RegexPattern.m_uiReactor
{
	bindable bool isEnabled = m_isTxEnabledProp.m_value || m_isRxEnabledProp.m_value;

	m_regexProp.m_isEnabled = isEnabled;
	m_colorProp.m_isEnabled = isEnabled;
	m_fontFlagProp.m_isEnabled = isEnabled;
}

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