//! MNOTE 首页组件（Wolai 风格）

use super::{
    document::{DocumentPane, DocumentPaneViewModel},
    layout::PageLayout,
};
use leptos::prelude::*;

/// MNOTE 首页。
#[component]
pub fn HomePage(
    /// 侧栏页面树 HTML（可选）
    #[prop(optional)]
    sidebar_tree_html: Option<String>,
    /// 工作区名称（可选）
    #[prop(optional)]
    workspace_name: Option<String>,
    /// 工作区 id（可选）
    #[prop(optional)]
    workspace_id: Option<String>,
    /// workspace shell 侧栏 sections HTML（可选）
    #[prop(optional)]
    workspace_sidebar_html: Option<String>,
    /// 当前选中的页面 id（可选）
    #[prop(optional)]
    active_page_id: Option<String>,
    /// 当前选中的页面标题（可选）
    #[prop(optional)]
    active_page_title: Option<String>,
    /// 是否显示管理员授权入口
    #[prop(optional)]
    show_admin_access_policy: bool,
) -> impl IntoView {
    let active_page_id = active_page_id.unwrap_or_default();
    let active_page_title = active_page_title
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
        .unwrap_or_else(|| "个人".to_string());
    let has_active_page = !active_page_id.trim().is_empty();
    let workspace_id = workspace_id
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty());
    let active_href = workspace_id
        .as_deref()
        .map(|workspace_id| format!("/documents/{active_page_id}?workspaceId={workspace_id}"))
        .unwrap_or_else(|| format!("/documents/{active_page_id}"));
    let enable_tree_live = false;
    view! {
        <PageLayout current_nav="home" sidebar_tree_html={sidebar_tree_html.unwrap_or_default()} workspace_name={workspace_name.unwrap_or_default()} workspace_sidebar_html={workspace_sidebar_html.unwrap_or_default()} topbar_title={active_page_title.clone()} show_admin_access_policy={show_admin_access_policy} enable_tree_live={enable_tree_live}>
            {move || if has_active_page {
                view! {
                    <main class="document-shell document-shell--workspace-entry" data-root-active-page-id={active_page_id.clone()} data-editor-host="leptos_tiptap_island">
                        <header class="document-shell-header">
                            <div class="document-page-icon" aria-hidden="true">"⌂"</div>
                            <h1>{active_page_title.clone()}</h1>
                            <div class="document-shell-meta" aria-label="页面元信息">
                                <span><span aria-hidden="true">"◌"</span>"个人空间"</span>
                                <span><span aria-hidden="true">"▣"</span>"工作区首页"</span>
                            </div>
                        </header>
                        <section class="mnote-workspace-active-state" data-testid="mnote-workspace-active-page-state">
                            <a class="mnote-workspace-active-link" href={active_href.clone()}>"打开当前页面"</a>
                        </section>
                    </main>
                }.into_any()
            } else {
                let empty_primary_model = DocumentPaneViewModel {
                    pane_role: "primary",
                    title: "文件夹".to_string(),
                    document_id: String::new(),
                    workspace_id: workspace_id.clone().unwrap_or_default(),
                    page_wide_layout: false,
                    page_small_text: false,
                    page_layout_density: "normal".to_string(),
                    page_font: "default".to_string(),
                    page_show_heading_numbers: false,
                    has_page_subtree: false,
                    primary_legacy_ids: true,
                    visible: true,
                    hide_title_header: true,
                };
                view! {
                    <main
                        class="document-workspace mnote-workspace-empty-editor-host"
                        data-testid="mnote-document-workspace"
                        data-has-secondary-pane="false"
                        data-workspace-id={workspace_id.clone().unwrap_or_default()}
                    >
                        <section class="document-main-editor-group" data-testid="mnote-main-editor-tab-host" data-pane-role="primary">
                            <div class="mnote-main-tab-strip" data-mnote-main-tab-strip="true" data-pane-role="primary" role="tablist" aria-label="主编辑区标签页">
                                <button
                                    type="button"
                                    class="mnote-main-tab is-active"
                                    data-mnote-main-tab="page"
                                    data-mnote-tab-kind="page"
                                    data-pane-role="primary"
                                    data-workspace-id={workspace_id.clone().unwrap_or_default()}
                                    role="tab"
                                    aria-selected="true"
                                    tabindex="0"
                                >
                                    <span class="mnote-main-tab-badge" aria-hidden="true"></span>
                                    <span class="mnote-main-tab-title">"文件夹"</span>
                                </button>
                            </div>
                            <div class="mnote-main-tab-panels">
                                <div data-mnote-page-tab-panel="true" data-pane-role="primary">
                                    <DocumentPane model={empty_primary_model} />
                                </div>
                                <section
                                    class="mnote-resource-tab-host"
                                    data-mnote-resource-tab-host="true"
                                    data-pane-role="primary"
                                    data-testid="mnote-resource-tab-host"
                                    hidden=true
                                >
                                    <div class="mnote-resource-tab-panel-root" data-mnote-resource-tab-panel-root="true" data-pane-role="primary"></div>
                                </section>
                            </div>
                        </section>
                    </main>
                }.into_any()
            }}
        </PageLayout>
    }
}
