use seed::{prelude::*, *};
use seed_hooks::topo;

use crate::{
    graphql::show_thread_query::*,
    state::{Context, Model, Msg},
    view::{self, reading_progress, view_header, view_search_results},
};

#[topo::nested]
pub(super) fn view(model: &Model) -> Node<Msg> {
    let show_icon_text = true;
    // Do two queries, one without `unread` so it loads fast, then a second with unread.
    let content = match &model.context {
        Context::None => div![h1!["Loading"]],
        Context::ThreadResult {
            thread: ShowThreadQueryThread::EmailThread(thread),
            open_messages,
        } => view::thread(thread, open_messages, show_icon_text, &model.content_el),
        Context::ThreadResult {
            thread: ShowThreadQueryThread::NewsPost(post),
            ..
        } => view::news_post(post, show_icon_text, &model.content_el),
        Context::SearchResult {
            query,
            results,
            count,
            pager,
            selected_threads,
        } => view_search_results(
            &query,
            results.as_slice(),
            *count,
            pager,
            selected_threads,
            show_icon_text,
        ),
    };
    div![
        C!["main-content"],
        reading_progress(model.read_completion_ratio),
        div![view::tags(model), view::versions(&model.versions)],
        div![
            view_header(&model.query, &model.refreshing_state),
            content,
            view_header(&model.query, &model.refreshing_state),
        ]
    ]
}
