/* tag_info_box.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, ngettext};
use relm4::prelude::*;

use crate::git::TagInfo;
use crate::ui_helpers::format_time;

pub struct TagInfoBox {
    tag_info: Option<TagInfo>,
}

#[derive(Debug)]
pub enum TagInfoBoxMsg {
    ShowTagInfo(TagInfo),
    Clear,
}

impl TagInfoBox {
    fn name(&self) -> String {
        self.tag_info.as_ref().map(|t| t.name.clone()).unwrap_or_default()
    }

    fn message(&self) -> String {
        self.tag_info
            .as_ref()
            .and_then(|t| t.message.clone())
            .unwrap_or_default()
    }

    fn has_message(&self) -> bool {
        self.tag_info.as_ref().is_some_and(|t| t.message.is_some())
    }

    fn author_line(&self) -> String {
        self.tag_info
            .as_ref()
            .map(|t| {
                if let (Some(tagger), Some(tagger_time)) = (&t.tagger, t.tagger_time) {
                    format!("{tagger} · {}", format_time(tagger_time))
                } else {
                    format!("{} · {}", t.commit_author, format_time(t.commit_time))
                }
            })
            .unwrap_or_default()
    }

    fn commit_summary(&self) -> String {
        self.tag_info
            .as_ref()
            .map(|t| t.commit_summary.clone())
            .unwrap_or_default()
    }

    fn commits_line(&self) -> String {
        self.tag_info
            .as_ref()
            .map(|t| {
                if t.total_commits_capped {
                    gettext("{}+ Commits").replace("{}", &t.total_commits.to_string())
                } else {
                    format!("{} {}", t.total_commits, ngettext("Commit", "Commits", t.total_commits as u32))
                }
            })
            .unwrap_or_default()
    }
}

#[relm4::component(pub)]
impl Component for TagInfoBox {
    type Init = ();
    type Input = TagInfoBoxMsg;
    type Output = ();
    type CommandOutput = ();

    view! {
        gtk::Box {
            set_orientation: gtk::Orientation::Vertical,
            add_css_class: "card",
            add_css_class: "gap-xs",
            add_css_class: "mb-xs",
            #[watch]
            set_visible: model.tag_info.is_some(),

            gtk::Box {
                set_orientation: gtk::Orientation::Horizontal,
                add_css_class: "gap-m",
                add_css_class: "mt-l",
                add_css_class: "ms-l",
                add_css_class: "me-l",

                gtk::Label {
                    add_css_class: "title-4",
                    set_halign: gtk::Align::Start,
                    set_hexpand: true,
                    set_ellipsize: gtk::pango::EllipsizeMode::End,
                    set_use_markup: false,
                    #[watch]
                    set_label: &model.name(),
                },
            },

            gtk::Label {
                set_halign: gtk::Align::Start,
                set_wrap: true,
                set_wrap_mode: gtk::pango::WrapMode::Word,
                add_css_class: "ms-l",
                add_css_class: "me-l",
                add_css_class: "dim-label",
                set_use_markup: false,
                #[watch]
                set_visible: model.has_message(),
                #[watch]
                set_label: &model.message(),
            },

            gtk::Label {
                set_halign: gtk::Align::Start,
                set_wrap: true,
                set_wrap_mode: gtk::pango::WrapMode::Word,
                add_css_class: "ms-l",
                add_css_class: "me-l",
                add_css_class: "dim-label",
                set_use_markup: false,
                #[watch]
                set_label: &model.commit_summary(),
            },

            gtk::Label {
                set_halign: gtk::Align::Start,
                add_css_class: "dim-label",
                add_css_class: "caption",
                add_css_class: "ms-l",
                add_css_class: "me-l",
                set_use_markup: false,
                #[watch]
                set_label: &model.author_line(),
            },

            gtk::Label {
                set_halign: gtk::Align::Start,
                add_css_class: "dim-label",
                add_css_class: "caption",
                add_css_class: "ms-l",
                add_css_class: "me-l",
                add_css_class: "mb-l",
                #[watch]
                set_label: &model.commits_line(),
            },
        }
    }

    fn init(_init: Self::Init, root: Self::Root, _sender: ComponentSender<Self>) -> ComponentParts<Self> {
        let model = TagInfoBox { tag_info: None };
        let widgets = view_output!();
        ComponentParts { model, widgets }
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>, _root: &Self::Root) {
        match msg {
            TagInfoBoxMsg::ShowTagInfo(info) => {
                self.tag_info = Some(info);
            }

            TagInfoBoxMsg::Clear => {
                self.tag_info = None;
            }
        }
    }
}
