# project_item.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, Gio
from .request_item import RequestItem


@Gtk.Template(resource_path='/cz/bugsy/roster/widgets/project-item.ui')
class ProjectItem(Gtk.Box):
    """Widget for displaying a project with its requests."""

    __gtype_name__ = 'ProjectItem'

    header_box = Gtk.Template.Child()
    project_icon = Gtk.Template.Child()
    name_label = Gtk.Template.Child()
    requests_revealer = Gtk.Template.Child()
    requests_listbox = Gtk.Template.Child()

    __gsignals__ = {
        'edit-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'delete-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'add-request-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
        'request-load-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
        'request-delete-requested': (GObject.SIGNAL_RUN_FIRST, None, (object,)),
        'manage-environments-requested': (GObject.SIGNAL_RUN_FIRST, None, ()),
    }

    def __init__(self, project):
        super().__init__()
        self.project = project
        self.expanded = False
        self._setup_actions()
        self._populate()

        # Click gesture for header
        gesture = Gtk.GestureClick.new()
        gesture.connect('released', self._on_header_clicked)
        self.header_box.add_controller(gesture)

    def _setup_actions(self):
        """Setup action group for menu."""
        actions = Gio.SimpleActionGroup.new()

        action = Gio.SimpleAction.new("manage-environments", None)
        action.connect("activate", lambda *_: self.emit('manage-environments-requested'))
        actions.add_action(action)

        action = Gio.SimpleAction.new("add-request", None)
        action.connect("activate", lambda *_: self.emit('add-request-requested'))
        actions.add_action(action)

        action = Gio.SimpleAction.new("edit", None)
        action.connect("activate", lambda *_: self.emit('edit-requested'))
        actions.add_action(action)

        action = Gio.SimpleAction.new("delete", None)
        action.connect("activate", lambda *_: self.emit('delete-requested'))
        actions.add_action(action)

        self.insert_action_group("project", actions)

    def _populate(self):
        """Populate with project data."""
        self.name_label.set_text(self.project.name)
        self.project_icon.set_from_icon_name(self.project.icon)

        # Clear and add requests
        while child := self.requests_listbox.get_first_child():
            self.requests_listbox.remove(child)

        for saved_request in self.project.requests:
            item = RequestItem(saved_request)
            item.connect('load-requested',
                        lambda w, sr=saved_request: self.emit('request-load-requested', sr))
            item.connect('delete-requested',
                        lambda w, sr=saved_request: self.emit('request-delete-requested', sr))
            self.requests_listbox.append(item)

    def _on_header_clicked(self, gesture, n_press, x, y):
        """Toggle expansion."""
        self.expanded = not self.expanded
        self.requests_revealer.set_reveal_child(self.expanded)

    def refresh(self):
        """Refresh from project data."""
        self._populate()
