#!/usr/bin/env python3
"""
Package View: Creates UI elements for packages.

Dependencies:
- python3-gi

Author: Arjen Balfoort
Date: 2026-06-07
"""

import os
import gettext

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk, Gdk, Pango  #pylint: disable=wrong-import-position

_ = gettext.translation('deb-software', fallback=True).gettext

class SoftwareView:
    """Creates GTK UI elements for packages."""

    def __init__(self, model, image_handler, icon_theme, images_dir):
        self.model = model  # PackageManager
        self.image_handler = image_handler
        self.icon_theme = icon_theme
        self.images_dir = images_dir

    def get_gtk_box_summary(self, pkg_info) -> Gtk.Box:
        """
        Create a summary GTK box for a package.
        CSS Classes: summary_box, summary_box label, summary_box label.{name,summary,origin}
        """
        box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=12)
        box.add_css_class('summary_box')
        box.set_cursor(Gdk.Cursor.new_from_name("pointer", None))

        # Icon
        icon = Gtk.Picture()
        self.image_handler.set_picture(icon, pkg_info.icon, 48)
        icon.set_content_fit(Gtk.ContentFit.SCALE_DOWN)
        icon.set_valign(Gtk.Align.CENTER)
        box.append(icon)

        # Labels
        label_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, hexpand=True)

        # Name
        label_name = Gtk.Label(label=pkg_info.name, halign=Gtk.Align.START)
        label_name.get_style_context().add_class("name")
        label_box.append(label_name)

        # Summary
        label_summary = Gtk.Label(label=pkg_info.summary, halign=Gtk.Align.START)
        label_summary.set_ellipsize(Pango.EllipsizeMode.END)
        label_summary.get_style_context().add_class("summary")
        label_box.append(label_summary)

        # Origin
        label_origin = Gtk.Label(label=pkg_info.origin, halign=Gtk.Align.START)
        label_origin.get_style_context().add_class("origin")
        label_box.append(label_origin)

        box.append(label_box)

        # Installed indicator
        if pkg_info.is_installed:
            installed_icon = Gtk.Picture().new_for_filename(
                os.path.join(self.images_dir, "installed.svg")
            )
            installed_icon.set_tooltip_text(_("Installed"))
            installed_icon.set_content_fit(Gtk.ContentFit.SCALE_DOWN)
            installed_icon.set_valign(Gtk.Align.END)
            box.append(installed_icon)

        return box
