# environment_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
from .environment_column_header import EnvironmentColumnHeader


@Gtk.Template(resource_path='/cz/bugsy/roster/widgets/environment-header-row.ui')
class EnvironmentHeaderRow(Gtk.Box):
    """Widget for the header row containing all environment column headers."""

    __gtype_name__ = 'EnvironmentHeaderRow'

    variable_cell = Gtk.Template.Child()
    variable_label = Gtk.Template.Child()
    columns_box = Gtk.Template.Child()

    __gsignals__ = {
        'environment-edit-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
        'environment-delete-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
        'environment-copy-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
    }

    def __init__(self, environments, size_group):
        super().__init__()
        self.environments = environments
        self.size_group = size_group
        self.column_headers = {}

        # Add variable cell to size group for alignment
        if size_group:
            size_group.add_widget(self.variable_cell)

        self._populate()

    def _populate(self):
        """Populate with environment column headers."""
        # Clear existing
        while child := self.columns_box.get_first_child():
            self.columns_box.remove(child)

        self.column_headers = {}

        # Create column header for each environment
        for env in self.environments:
            header = EnvironmentColumnHeader(env)
            header.connect('edit-requested', self._on_edit_requested, env)
            header.connect('delete-requested', self._on_delete_requested, env)
            header.connect('copy-requested', self._on_copy_requested, env)

            # Add to size group for alignment with data rows
            if self.size_group:
                self.size_group.add_widget(header)

            self.columns_box.append(header)
            self.column_headers[env.id] = header

    def _on_edit_requested(self, widget, environment):
        """Handle edit request from column header."""
        self.emit('environment-edit-requested', environment)

    def _on_delete_requested(self, widget, environment):
        """Handle delete request from column header."""
        self.emit('environment-delete-requested', environment)

    def _on_copy_requested(self, widget, environment):
        """Handle copy request from column header."""
        self.emit('environment-copy-requested', environment)

    def refresh(self, environments):
        """Refresh with updated environments list."""
        self.environments = environments
        self._populate()
