/* release_notes_dialog.rs
 *
 * Copyright 2026 Christian Kruse
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

use adw::prelude::*;
use gettextrs::gettext;
use relm4::gtk;

use crate::release_notes::{ReleaseNote, ReleaseSection};

pub fn present_release_notes_dialog(parent: Option<&gtk::Window>, notes: &[&ReleaseNote]) {
    if notes.is_empty() {
        return;
    }

    relm4::view! {
        #[name = "dialog"]
        adw::Dialog {
            set_title: &gettext("What's New"),
            set_content_width: 560,
            set_content_height: 640,

            #[wrap(Some)]
            set_child = &adw::ToolbarView {
                add_top_bar = &adw::HeaderBar {
                    set_css_classes: &["flat"],
                },

                #[wrap(Some)]
                set_content = &gtk::ScrolledWindow {
                    set_hscrollbar_policy: gtk::PolicyType::Never,
                    set_vscrollbar_policy: gtk::PolicyType::Automatic,
                    set_propagate_natural_height: true,
                    set_vexpand: true,

                    #[name = "content"]
                    gtk::Box {
                        set_orientation: gtk::Orientation::Vertical,
                        add_css_class: "gap-xl",
                        add_css_class: "mt-l",
                        add_css_class: "mb-xl",
                        add_css_class: "ms-xl",
                        add_css_class: "me-xl",
                    }
                }
            }
        }
    }

    for note in notes {
        content.append(&build_release_card(note));
    }

    dialog.present(parent);
}

fn build_release_card(note: &ReleaseNote) -> gtk::Widget {
    relm4::view! {
        #[name = "card"]
        gtk::Box {
            set_orientation: gtk::Orientation::Vertical,
            set_css_classes: &["card"],

            #[name = "inner"]
            gtk::Box {
                set_orientation: gtk::Orientation::Vertical,
                add_css_class: "gap-l",
                add_css_class: "mt-l",
                add_css_class: "mb-xl",
                add_css_class: "ms-xl",
                add_css_class: "me-xl",

                gtk::Box {
                    set_orientation: gtk::Orientation::Horizontal,
                    add_css_class: "gap-l",

                    gtk::Label {
                        set_label: &format!("Gitte {}", note.version),
                        set_halign: gtk::Align::Start,
                        set_xalign: 0.0,
                        set_hexpand: true,
                        add_css_class: "title-2",
                    },

                    gtk::Label {
                        set_label: note.date,
                        set_valign: gtk::Align::Center,
                        set_halign: gtk::Align::End,
                        add_css_class: "caption",
                        add_css_class: "dim-label",
                    },
                },
            }
        }
    }

    if !note.summary.is_empty() {
        relm4::view! {
            summary = gtk::Label {
                set_label: note.summary,
                set_halign: gtk::Align::Start,
                set_xalign: 0.0,
                set_wrap: true,
                set_wrap_mode: gtk::pango::WrapMode::WordChar,
                add_css_class: "body",
            }
        }

        inner.append(&summary);
    }

    for section in note.sections {
        inner.append(&build_section(section));
    }

    card.upcast()
}

fn build_section(section: &ReleaseSection) -> gtk::Box {
    relm4::view! {
        #[name = "container"]
        gtk::Box {
            set_orientation: gtk::Orientation::Vertical,
            add_css_class: "gap-s",
            add_css_class: "mt-s",
        }
    }

    if !section.heading.is_empty() {
        relm4::view! {
            heading = gtk::Label {
                set_label: section.heading,
                set_halign: gtk::Align::Start,
                set_xalign: 0.0,
                add_css_class: "heading",
            }
        }

        container.append(&heading);
    }

    relm4::view! {
        #[name = "list"]
        gtk::Box {
            set_orientation: gtk::Orientation::Vertical,
            add_css_class: "gap-xs",
            add_css_class: "ms-xs",
        }
    }

    for item in section.items {
        list.append(&build_bullet_row(item));
    }

    container.append(&list);
    container
}

fn build_bullet_row(item: &str) -> gtk::Box {
    relm4::view! {
        #[name = "row"]
        gtk::Box {
            set_orientation: gtk::Orientation::Horizontal,
            add_css_class: "gap-m",

            gtk::Label {
                set_label: "•",
                set_valign: gtk::Align::Start,
                set_halign: gtk::Align::Start,
                set_width_chars: 1,
                add_css_class: "dim-label",
            },

            gtk::Label {
                set_label: item,
                set_halign: gtk::Align::Start,
                set_xalign: 0.0,
                set_wrap: true,
                set_wrap_mode: gtk::pango::WrapMode::WordChar,
                set_hexpand: true,
            },
        }
    }

    row
}
