[GtkTemplate(ui = "/io/gitlab/nekocwd/pipetap/gui/slider_overlay.ui")]
public class PipeTap.Ui.SliderOverlay : PipeTap.Ui.FloatingWindow {
    [GtkChild]
    private unowned Gtk.Scale scale;
    [GtkChild]
    private unowned Gtk.ToggleButton lock_btn;


    public Logic.Ctrl.CtrlType slider_type { get; set; }
    public Logic.Ctrl.CtrlType? toggle_type { get; set; }
    public bool is_available { get; protected set; }

    public SliderOverlay(PipeTap.Application app, Logic.Ctrl.CtrlType slider_type, Logic.Ctrl.CtrlType toggle_type, string name, string icon_name) {
        base(app, name);
        this.slider_type = slider_type;
        this.toggle_type = toggle_type;
        scale.value_changed.connect(value_changed);
        lock_btn.toggled.connect(lock_changed);
        app.notify["device"].connect(set_ctrls);
        lock_btn.icon_name = icon_name;
        set_ctrls();
    }

    void set_ctrls() {
        var slider_ctrl = Logic.find_ctrl(slider_type);
        var lock_ctrl = Logic.find_ctrl(toggle_type);
        scale.sensitive = slider_ctrl != null;
        lock_btn.sensitive = lock_ctrl != null;
        is_available = slider_ctrl != null || lock_ctrl != null;
        if (slider_ctrl != null)
            scale.set_value(slider_ctrl.value);
        if (lock_ctrl != null)
            lock_btn.active = lock_ctrl.value == 1;
    }

    void value_changed() {
        message("Value %f", scale.get_value());
        var ctrl = Logic.find_ctrl(slider_type);
        if (ctrl == null) {
            message("Slider ctrl not found");
            return;
        }
        ctrl.value = (int) scale.get_value();
    }

    void lock_changed() {
        message("Lock %s", lock_btn.active ? "on" : "off");
        var ctrl = Logic.find_ctrl(toggle_type);
        if (ctrl == null) {
            message("Toggle ctrl not found");
            return;
        }
        ctrl.value = (int) lock_btn.active;
    }
}