//..............................................................................
//
// Pcap device user interface
//

import "gui_Base.jnc"
import "gui_PropertyGrid.jnc"
import "io_pcap.jncx"

namespace io {

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

gui.EnumPropertyOption const* createPcapDeviceOptionArray (bool canOpenFile = true)
{
	size_t count;
	io.PcapDeviceDesc const* device = io.createPcapDeviceDescList (&count);

	if (canOpenFile)
		count++;

	gui.EnumPropertyOption* optionArray = new gui.EnumPropertyOption [count];
	gui.EnumPropertyOption* option = optionArray;

	for (; device; device = device->m_next, option++)
	{
		option.m_text = $"%1 - %2" (
			device->m_description ? device->m_description : device->m_name,
			device->m_address.m_address.getString ()
			);

		option.m_value = device;
	}

	if (canOpenFile)
	{
		option.m_text = "Wireshark File (*.cap; *.pcap)";
		option.m_value = null;
	}

	return optionArray;
}

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

gui.EnumProperty* createPcapDeviceProperty (
	gui.PropertyGrid* propertyGrid,
	bool canOpenFile = true,
	char const* name,
	char const* toolTipText
	)
{
	gui.EnumPropertyOption const* optionArray = createPcapDeviceOptionArray (canOpenFile);
	size_t optionCount = dynamic countof (optionArray);

	return propertyGrid.createEnumProperty (
		name,
		toolTipText,
		optionArray,
		optionCount
		);
}

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

gui.ComboBox*
createPcapDeviceComboBox (
	gui.ToolBar* toolBar,
	bool canOpenFile = true,
	uint_t width = -1
	)
{
	gui.EnumPropertyOption const* optionArray = createPcapDeviceOptionArray (canOpenFile);
	size_t optionCount = dynamic countof (optionArray);

	gui.ComboBox* comboBox = toolBar.addComboBox (width);

	for (size_t i = 0; i < optionCount; i++)
		comboBox.addItem (
			optionArray [i].m_text,
			optionArray [i].m_value
			);

	return comboBox;
}

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

} // namespace io {
