use json::{JsonValue, object};

#[derive(Clone, Debug)]
pub struct ContentType {
    pub content_type: String,
    pub suffix: String,
}

impl ContentType {
    pub fn from(content_type: &str) -> Self {
        let data = ContentType::type_suffix();
        let suffix = data[content_type].as_str().unwrap_or("");
        let suffix = suffix.to_lowercase();
        let suffix = suffix.as_str();
        Self {
            content_type: content_type.to_string(),
            suffix: suffix.to_string(),
        }
    }
    pub fn from_suffix(suffix: &str) -> Self {
        let suffix = suffix.to_lowercase();
        let suffix = suffix.as_str();
        let data = ContentType::suffix_type();

        let content_type = data[suffix].as_str().unwrap_or("");
        Self {
            content_type: content_type.to_string(),
            suffix: suffix.to_string(),
        }
    }
    fn type_suffix() -> JsonValue {
        object! {
            "image/png" => "png",
            "image/jpeg" => "jpeg",
            "image/jpg" => "jpg",
            "image/gif"=>"gif",
            "image/tiff"=>"tiff",
            "image/tif"=>"tif",
            "image/bmp"=>"bmp",
            "image/avif" => "avif",
            "image/webp" => "webp",
            "application/pdf" => "pdf",
            "application/zip" => "zip",
            "application/x-msdos-program" => "exe",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
            "application/vnd.ms-excel" => "xls",
            "application/vnd.ms-excel.sheet.macroenabled.12" => "xlsm",
            "application/vnd.apple.numbers" => "numbers",
            "image/x-icon" => "ico",
            "image/vnd.dwg" => "dwg",
            "application/xml" => "xml",
            "text/xml" => "xml",
            "text/markdown" => "md",
            "image/heic" => "heic",
            "text/csv" => "csv",
            "text/html" => "html",
            "image/svg+xml" => "svg",
            "application/msword" => "doc",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",

            "application/x-www-form-urlencoded" => "x-www-form-urlencoded",
            "application/javascript" => "js",
            "multipart/form-data" => "form-data",
            "application/json" => "json",
            "text/plain" => "txt"
        }
    }
    fn suffix_type() -> JsonValue {
        let mut data = object! {};
        let res = ContentType::type_suffix();
        for (content_type, suffix) in res.entries() {
            data[suffix.as_str().unwrap()] = content_type.to_string().into();
        }
        data
    }
    pub fn suffix_type_name(self) -> &'static str {
        match self.suffix.as_str() {
            "png" | "jpeg" | "jpg" | "ico" | "heic" | "svg" |
            "avif" | "webp" | "gif" | "tiff" | "tif" | "bmp" => "图片",
            "txt" | "xml" | "md" | "doc" | "docx" => "文档",
            "pdf" => "PDF",
            "numbers" | "xlsx" | "xlsm" | "csv" | "xls" => "表格",
            "zip" => "压缩包",
            "log" => "日志",
            "dwg" => "图纸",
            "mp3" | "mp4" => "视频",
            "wav" | "mpeg" => "音频",
            "exe" => "程序",
            "html" => "网页",
            _ => "未知"
        }
    }
}
