use krx_core::{Action, Category, FileHandler, FileInfo, FileType, Result, HANDLERS};
use linkme::distributed_slice;
use std::path::Path;

pub struct VideoHandler;

fn video_types() -> &'static [FileType] {
    &[
        FileType { category: Category::Video, mime: "video/mp4", extension: "mp4" },
        FileType { category: Category::Video, mime: "video/x-matroska", extension: "mkv" },
        FileType { category: Category::Video, mime: "video/x-msvideo", extension: "avi" },
        FileType { category: Category::Video, mime: "video/quicktime", extension: "mov" },
        FileType { category: Category::Video, mime: "video/x-ms-wmv", extension: "wmv" },
        FileType { category: Category::Video, mime: "video/webm", extension: "webm" },
    ]
}

impl FileHandler for VideoHandler {
    fn supported_types(&self) -> &[FileType] { video_types() }

    fn info(&self, path: &Path) -> Result<FileInfo> {
        let meta = std::fs::metadata(path)?;
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_lowercase();
        let detected = video_types().iter().find(|t| t.extension == ext).cloned()
            .unwrap_or_else(|| video_types()[0].clone());
        let mut m = indexmap::IndexMap::new();
        m.insert("Formato", detected.extension.to_uppercase());
        Ok(FileInfo { path: path.to_path_buf(), size: meta.len(), detected_type: detected, metadata: m, available_actions: self.available_actions() })
    }

    fn available_actions(&self) -> Vec<Action> {
        vec![Action::Info, Action::Play, Action::Open]
    }
}

#[distributed_slice(HANDLERS)]
static VIDEO_HANDLER: &'static dyn FileHandler = &VideoHandler;
