# header_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/header-row.ui')
class HeaderRow(Gtk.Box):
    """Widget for editing a single HTTP header key-value pair."""

    __gtype_name__ = 'HeaderRow'

    key_entry = Gtk.Template.Child()
    value_entry = Gtk.Template.Child()
    remove_button = Gtk.Template.Child()

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

    def __init__(self):
        super().__init__()

        # Connect to entry changes to emit our own changed signal
        self.key_entry.connect('changed', self._on_entry_changed)
        self.value_entry.connect('changed', self._on_entry_changed)

    def _on_entry_changed(self, entry):
        """Handle changes to key or value entries."""
        self.emit('changed')

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

    def get_header(self):
        """Return (key, value) tuple."""
        return (self.key_entry.get_text(), self.value_entry.get_text())

    def set_header(self, key: str, value: str):
        """Set header key and value."""
        self.key_entry.set_text(key)
        self.value_entry.set_text(value)
