# variable_row.py
#
# Copyright 2025 Pavel Baksy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

from gi.repository import Gtk, GObject


@Gtk.Template(resource_path='/cz/bugsy/roster/widgets/variable-row.ui')
class VariableRow(Gtk.Box):
    """Widget for editing a variable name."""

    __gtype_name__ = 'VariableRow'

    name_entry = Gtk.Template.Child()
    remove_button = Gtk.Template.Child()

    __gsignals__ = {
        'remove-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'changed': (GObject.SIGNAL_RUN_FIRST, None, ())
    }

    def __init__(self, variable_name=""):
        super().__init__()
        self.name_entry.set_text(variable_name)
        self.name_entry.connect('changed', self._on_entry_changed)

    def _on_entry_changed(self, entry):
        """Handle changes to name entry."""
        self.emit('changed')

    @Gtk.Template.Callback()
    def on_remove_clicked(self, button):
        """Handle remove button click."""
        self.emit('remove-requested')

    def get_variable_name(self):
        """Return variable name."""
        return self.name_entry.get_text().strip()

    def set_variable_name(self, name: str):
        """Set variable name."""
        self.name_entry.set_text(name)
