use async_trait::async_trait;
use serde_json::{json, Value};

use crate::filetree::mode::{DisplayMode, FilterOption, SortOption};
use crate::filetree::FileTree;

pub struct GridSmMode;

#[async_trait]
impl DisplayMode for GridSmMode {
    fn name(&self) -> &'static str {
        "grid_sm"
    }

    fn render(&self, tree: &FileTree) -> Value {
        let nodes: Vec<Value> = tree
            .nodes
            .iter()
            .map(|n| {
                json!({
                    "node_id": n.node_id,
                    "label": n.label,
                    "node_type": n.node_type.as_str(),
                    "icon": n.icon,
                    "color": n.color,
                    "bg_color": n.bg_color,
                })
            })
            .collect();

        json!({
            "mode": "grid_sm",
            "user_id": tree.user_id,
            "cell_size": 96,
            "nodes": nodes,
        })
    }

    fn sort_options(&self) -> Vec<SortOption> {
        vec![
            SortOption {
                key: "name_asc".into(),
                label: "Name A-Z".into(),
            },
            SortOption {
                key: "name_desc".into(),
                label: "Name Z-A".into(),
            },
            SortOption {
                key: "date_desc".into(),
                label: "Newest First".into(),
            },
        ]
    }

    fn filter_options(&self) -> Vec<FilterOption> {
        vec![
            FilterOption {
                key: "all".into(),
                label: "All".into(),
            },
            FilterOption {
                key: "folder".into(),
                label: "Folders".into(),
            },
            FilterOption {
                key: "file".into(),
                label: "Files".into(),
            },
        ]
    }
}
