//! Static screen: the app's own license + the third-party software notices (reached from
//! Settings).

use super::style::*;
use super::{AppCtx, Screen};
use std::sync::Arc;
use windows_reactor::*;

/// punktfunk's own license (MIT OR Apache-2.0).
const APP_LICENSE: &str = concat!(
    include_str!("../../../../LICENSE-MIT"),
    "\n\n================================ Apache-2.0 ================================\n\n",
    include_str!("../../../../LICENSE-APACHE"),
);
/// Third-party software notices for the Rust crates THIS CLIENT links — the shell, the
/// session streamer and the headless CLI (generated by scripts/gen-third-party-notices.sh;
/// the MSIX ships the same file under licenses/).
///
/// Deliberately the client-scoped file and not the workspace-wide one at the repo root:
/// that root file is the HOST's, it still carries `ffmpeg-next` and the full FFmpeg licence
/// text — and after M10 this app bundles no FFmpeg at all, so printing that attribution
/// three lines under a card saying so would be a false statement to the user's face.
const THIRD_PARTY_NOTICES: &str = include_str!("../../THIRD-PARTY-NOTICES.txt");

pub(crate) fn licenses_page(ctx: &Arc<AppCtx>, set_screen: &AsyncSetState<Screen>) -> Element {
    let back_btn = button("Back").accent().icon(Symbol::Back).on_click({
        let (c, ss) = (ctx.clone(), set_screen.clone());
        move || {
            // Back RE-ENTERS the settings page — re-base its snapshot on the file, same
            // as the hosts page's Settings button (see settings::refresh_snapshot).
            super::settings::refresh_snapshot(&c);
            ss.call(Screen::Settings)
        }
    });

    let app_card = card(
        vstack((
            text_block("Punktfunk").font_size(15.0).semibold(),
            text_block("Licensed under MIT OR Apache-2.0, at your option.")
                .font_size(12.0)
                .wrap()
                .foreground(ThemeRef::SecondaryText),
            text_block(APP_LICENSE)
                .font_size(11.0)
                .wrap()
                .foreground(ThemeRef::SecondaryText),
        ))
        .spacing(8.0),
    );

    let natives_card = card(
        vstack((
            text_block("Bundled components").font_size(15.0).semibold(),
            text_block(
                "SDL 3 (Zlib) and the Windows App SDK (Microsoft) are linked; their notices ship \
                 in the installed licenses\\ folder. Video decoding uses Windows' own DXVA and \
                 Vulkan Video, with OpenH264 and rav1d (both BSD-2-Clause) as the CPU fallback — \
                 no FFmpeg is bundled.",
            )
            .font_size(12.0)
            .wrap()
            .foreground(ThemeRef::SecondaryText),
        ))
        .spacing(8.0),
    );

    let notices_card = card(
        vstack((
            text_block("Rust crates").font_size(15.0).semibold(),
            text_block(THIRD_PARTY_NOTICES)
                .font_size(11.0)
                .wrap()
                .foreground(ThemeRef::SecondaryText),
        ))
        .spacing(8.0),
    );

    page(vec![
        page_header("Third-party licenses", back_btn),
        section("PUNKTFUNK"),
        app_card.into(),
        section("BUNDLED"),
        natives_card.into(),
        section("OPEN SOURCE"),
        notices_card.into(),
    ])
}
