import "ui_PropertyGrid.jnc"

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

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

	reactor m_uiReactor;

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

	ui.TextAttr buildTextAttr();

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

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

RegexPattern.construct(
	ui.PropertyGrid* propertyGrid,
	ui.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",
		"Enter a 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"
	);

	ui.FlagPropertyOption fontFlagTable[] = {
		{ "Bold",      ui.FontFlags.Bold },
		{ "Italic",    ui.FontFlags.Italic },
		{ "Underline", ui.FontFlags.Underline },
		{ "Strikeout", ui.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]+", ui.StdColor.LightRed },
		{ "[0-9]+",        ui.StdColor.LightPurple },
		{ "[A-Za-z]+",     ui.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();
}

ui.TextAttr RegexPattern.buildTextAttr() {
	ui.TextAttr attr = {
		m_backColor = ui.ColorFlags.Transparent,
		m_textColor = m_colorProp.m_value,
		m_fontFlags = (ui.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;
}

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