<?xml version="1.0"?>

<PropertyList>
    <name>input-config-select</name>
    <layout>vbox</layout>
    <resizable>true</resizable>
    <pref-width>350</pref-width>

    <nasal>
        <open>
            <![CDATA[
            var DLGROOT = cmdarg();
            var dlg_prefix = "/sim/gui/dialogs/"
                ~DLGROOT.getNode("name", 1).getValue()~"/";

            var devices = input_helpers.config_manager.devicesWithVariants();
            props.getNode(dlg_prefix~"_max_devices",1).setValue(size(devices) - 1);

            var current_device = props.getNode(dlg_prefix~"_current_device",1)
                .initNode('', 0, "INT");

            var selected_variant = props.getNode(dlg_prefix~"variant", 1);


            # show previous device
            var prev = func() {
                if (current_device.getValue() > 0)
                    current_device.decrement();
                DLGROOT.setValues({"dialog-name": DLGROOT.getNode("name", 1).getValue()});
				fgcommand("dialog-close", DLGROOT);
				fgcommand("dialog-show", DLGROOT);
            }

            # show next device
            var next = func() {
                if (current_device.getValue() < size(devices) - 1)
                    current_device.increment();
                DLGROOT.setValues({"dialog-name": DLGROOT.getNode("name", 1).getValue()});
				fgcommand("dialog-close", DLGROOT);
				fgcommand("dialog-show", DLGROOT);
            }

            # update dialog elements
            var update = func(device) {
                setprop(dlg_prefix~"devname", device.getNode("name").getValue());
                setprop(dlg_prefix~"devpath", device.getPath());
                setprop(dlg_prefix~"variant", '');
                setprop(dlg_prefix~"has-device-dialog", 0);
                var variants = device.getNode("config-variants", 1).getChildren("variant");
                var selected = device.getNode("_selected-variant", 1).getValue() or '';
                var list_items = props.globals.getNode(dlg_prefix~"variants", 1);
                list_items.removeChildren();
                selected_variant.setValue('');

                if (size(variants)) {
                    foreach (var v; variants) {
                        var id = v.getNode("id").getValue();
                        var description = v.getNode("description").getValue();
                        list_items.addChild("id").setValue(id);
                        list_items.addChild("value").setValue(description);
                        if (id == selected) {
                            selected_variant.setValue(description);
                        }
                    }
                    if (device.getNode("device-dialog"))
                        setprop(dlg_prefix~"has-device-dialog", 1);
                }
                DLGROOT.setValues({"dialog-name": DLGROOT.getNode("name", 1).getValue()});
				fgcommand("dialog-update", DLGROOT);
            }

            # apply config variant
            var applyVariant = func() {
                var cfg = devices[current_device.getIntValue()];
                var list = props.globals.getNode(dlg_prefix~"variants", 1);
                foreach (var v; list.getChildren("value")) {
                    if (v.getValue() == selected_variant.getValue()) {
                        cfg.getNode("_selected-variant", 1)
                            .setValue(list.getChild("id", v.getIndex()).getValue());
                    }
                }
            }

            var deviceDialog = func() {
                var d = devices[current_device.getIntValue()].getNode("device-dialog");
                if (d) {
                    var dname = d.getValue();
                    if (dname)
                        fgcommand("dialog-show", props.Node.new({"dialog-name": dname}));
                }
            }

            # init dialog
            if (size(devices)) {
                update(devices[current_device.getIntValue()]);
            }
            ]]>
        </open>
    </nasal>

    <group>
        <layout>hbox</layout>
        <text>
            <label>Configuration Selector</label>
        </text>
        <empty><stretch>true</stretch></empty>
        <button>
            <legend>X</legend>
            <key>Esc</key>
            <pref-width>20</pref-width>
            <pref-height>20</pref-height>
            <binding>
                <command>dialog-close</command>
            </binding>
        </button>
    </group>
    <hrule/>
    <group>
        <layout>vbox</layout>
        <valign>top</valign>
        <text>
            <pref-width>200</pref-width>
            <name>devname</name>
            <halign>left</halign>
            <live>true</live>
            <property>/sim/gui/dialogs/input-config-select/devname</property>
        </text>
        <text>
            <pref-width>200</pref-width>
            <name>devpath</name>
            <halign>left</halign>
            <live>true</live>
            <property>/sim/gui/dialogs/input-config-select/devpath</property>
        </text>
        <text>
            <label>Available variants</label>
            <halign>left</halign>
        </text>
        <list>
            <name>listvariant</name>
            <halign>left</halign>
            <pref-width>250</pref-width>
            <pref-height>150</pref-height>
            <property>/sim/gui/dialogs/input-config-select/variant</property>
            <properties>/sim/gui/dialogs/input-config-select/variants</properties>
            <binding>
                <command>dialog-apply</command>
                <object-name>listvariant</object-name>
            </binding>
            <binding>
                <command>nasal</command>
                <script>applyVariant();</script>
            </binding>
        </list>
    </group>
    <group>
        <layout>hbox</layout>
        <valign>top</valign>
        <text>
            <label>Selected:</label>
            <halign>left</halign>
        </text>
        <text>
            <pref-width>200</pref-width>
            <name>selected</name>
            <halign>left</halign>
            <live>true</live>
            <property>/sim/gui/dialogs/input-config-select/variant</property>
        </text>
    </group>
    <hrule/>
    <group>
        <name>button-bar</name>
        <layout>hbox</layout>
        <button>
            <name>prev</name>
            <legend>&lt;</legend>
            <enable>
                <greater-than>
                    <property>/sim/gui/dialogs/input-config-select/_current_device</property>
                    <value type="int">0</value>
                </greater-than>
            </enable>
            <binding>
                <command>nasal</command>
                <script>prev();</script>
            </binding>
        </button>
        <button>
            <legend>Done</legend>
            <binding>
                <command>dialog-close</command>
                <dialog-name>input-config-select</dialog-name>
            </binding>
        </button>
        <button>
            <legend>Device Dialog</legend>
            <enable>
                <property>/sim/gui/dialogs/input-config-select/has-device-dialog</property>
            </enable>
            <binding>
                <command>nasal</command>
                <script>deviceDialog();</script>
            </binding>
        </button>
        <button>
            <name>next</name>
            <legend>&gt;</legend>
            <enable>
                <less-than>
                    <property>/sim/gui/dialogs/input-config-select/_current_device</property>
                    <property>/sim/gui/dialogs/input-config-select/_max_devices</property>
                </less-than>
            </enable>
            <binding>
                <command>nasal</command>
                <script>next();</script>
            </binding>
        </button>
    </group>
</PropertyList>
