/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use super::error::MyResult;
use crate::{request_info::RequestInfo, ui_responder::UiResponse};
use jellycommon::{
    jellyobject::{Object, ObjectBuffer, ObjectBufferBuilder, Path},
    *,
};
use jellydb::{Filter, Query, Sort};
use rocket::get;

// fn jellynative_url(action: &str, seek: f64, secret: &str, node: &str, session: &str) -> String {
//     let protocol = if CONF.tls { "https" } else { "http" };
//     let host = &CONF.hostname;
//     let stream_url = format!(
//         "/n/{node}/stream{}",
//         StreamSpec::HlsMultiVariant {
//             container: StreamContainer::Matroska
//         }
//         .to_query()
//     );
//     format!("jellynative://{action}/{secret}/{session}/{seek}/{protocol}://{host}{stream_url}",)
// }

#[get("/n/<slug>/player?<t>", rank = 4)]
pub fn r_player(ri: RequestInfo<'_>, t: Option<f64>, slug: &str) -> MyResult<UiResponse> {
    ri.require_user()?;
    let _ = t;

    let mut page_out = ObjectBuffer::empty();
    ri.state.database.transaction(&mut |txn| {
        if let Some(row) = txn.query_single(Query {
            filter: Filter::Match(Path(vec![NO_SLUG.0]), slug.into()),
            sort: Sort::None,
        })? {
            let n = txn.get(row)?.unwrap();
            let nku = Object::EMPTY.insert(NKU_NODE, n.as_object());

            let mut page = ObjectBufferBuilder::default();
            let title = nku
                .as_object()
                .get(NKU_NODE)
                .unwrap_or_default()
                .get(NO_TITLE)
                .unwrap_or_default();
            page.push(VIEW_TITLE, title);
            page.push(VIEW_PLAYER, nku.as_object());

            page_out = page.finish();
        }
        Ok(())
    })?;

    Ok(ri.respond_ui(page_out))
}

// pub fn player_conf<'a>(item: Arc<Node>, playing: bool) -> anyhow::Result<DynRender<'a>> {
//     let mut audio_tracks = vec![];
//     let mut video_tracks = vec![];
//     let mut sub_tracks = vec![];
//     let tracks = item
//         .media
//         .clone()
//         .ok_or(anyhow!("node does not have media"))?
//         .tracks
//         .clone();
//     for (tid, track) in tracks.into_iter().enumerate() {
//         match &track.kind {
//             SourceTrackKind::Audio { .. } => audio_tracks.push((tid, track)),
//             SourceTrackKind::Video { .. } => video_tracks.push((tid, track)),
//             SourceTrackKind::Subtitles => sub_tracks.push((tid, track)),
//         }
//     }

//     Ok(markup::new! {
//         form.playerconf[method = "GET", action = ""] {
//             h2 { "Select tracks for " @item.title }

//             fieldset.video {
//                 legend { "Video" }
//                 @for (i, (tid, track)) in video_tracks.iter().enumerate() {
//                     input[type="radio", id=tid, name="v", value=tid, checked=i==0];
//                     label[for=tid] { @format!("{track}") } br;
//                 }
//                 input[type="radio", id="v-none", name="v", value=""];
//                 label[for="v-none"] { "No video" }
//             }

//             fieldset.audio {
//                 legend { "Audio" }
//                 @for (i, (tid, track)) in audio_tracks.iter().enumerate() {
//                     input[type="radio", id=tid, name="a", value=tid, checked=i==0];
//                     label[for=tid] { @format!("{track}") } br;
//                 }
//                 input[type="radio", id="a-none", name="a", value=""];
//                 label[for="a-none"] { "No audio" }
//             }

//             fieldset.subtitles {
//                 legend { "Subtitles" }
//                 @for (_i, (tid, track)) in sub_tracks.iter().enumerate() {
//                     input[type="radio", id=tid, name="s", value=tid];
//                     label[for=tid] { @format!("{track}") } br;
//                 }
//                 input[type="radio", id="s-none", name="s", value="", checked=true];
//                 label[for="s-none"] { "No subtitles" }
//             }

//             input[type="submit", value=if playing { "Change tracks" } else { "Start playback" }];
//         }
//     })
// }
