use crate::app::App;
use eframe::emath::Align;
use egui::{Hyperlink, Layout, Ui};

fn version_and_changelog(ui: &mut Ui) {
    ui.add(
        Hyperlink::from_label_and_url(
            format!("v{}", env!("CARGO_PKG_VERSION")),
            "https://weather.ax/changelog",
        )
        .open_in_new_tab(true),
    );
}

fn frame_time_warning(ui: &mut Ui, app: &App) {
    if let Some(frame_time) = app.frame_time_history.average() {
        ui.separator();

        let ms = frame_time * 1e3;
        let visuals = ui.visuals();
        let color = if ms < 15.0 {
            visuals.weak_text_color()
        } else {
            visuals.warn_fg_color
        };
        let text = format!("{ms:.1} ms");
        ui.label(egui::RichText::new(text).monospace().color(color))
            .on_hover_text("CPU time used by wxbox client each frame, lower is better");
    }
}

fn e3team_credits(ui: &mut Ui) {
    ui.add(
        Hyperlink::from_label_and_url("built with <3 by e3team", "https://e3t.cc")
            .open_in_new_tab(true),
    );
}

pub fn footer(ctx: &egui::Context, app: &App) {
    egui::TopBottomPanel::bottom("footer")
        .resizable(false)
        .show(ctx, |ui| {
            ui.style_mut().visuals.hyperlink_color = ui.visuals().weak_text_color();

            ui.horizontal(|ui| {
                version_and_changelog(ui);
                frame_time_warning(ui, app);

                ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                    e3team_credits(ui);
                });
            });
        });
}
