use json::JsonValue;

#[derive(Clone, Debug)]
pub struct Request {
    /// 配置参数
    pub config: JsonValue,
    /// 当前请求类型
    pub method: Method,
    /// 请求地址
    pub url: String,
    pub paths: Vec<String>,
    /// 地址参数
    pub query: JsonValue,
    /// header信息
    pub header: JsonValue,
    /// Cookie信息
    pub cookie: JsonValue,
    /// 自定义数据
    pub custom: JsonValue,
    /// 请求体
    pub body: JsonValue,
    /// 客户端IP
    pub client_ip: String,
    /// 服务器IP
    pub server_ip: String,
    /// 请求时间
    pub request_time: i64,
    /// 耗时
    pub handle_time: String,
}
impl Default for Request {
    fn default() -> Self {
        Self {
            config: JsonValue::Null,
            method: Method::None,
            url: String::new(),
            paths: vec![],
            query: JsonValue::Null,
            header: JsonValue::Null,
            cookie: JsonValue::Null,
            custom: JsonValue::Null,
            body: JsonValue::Null,
            client_ip: String::new(),
            server_ip: String::new(),
            request_time: 0,
            handle_time: String::new(),
        }
    }
}
#[derive(Clone, Debug)]
pub enum Method {
    Post,
    Get,
    Head,
    Put,
    Delete,
    Options,
    Patch,
    Trace,
    None,
}
impl Method {
    #[must_use]
    pub fn from(name: &str) -> Self {
        match name.to_lowercase().as_str() {
            "post" => Self::Post,
            "get" => Self::Get,
            "head" => Self::Head,
            "put" => Self::Put,
            "delete" => Self::Delete,
            "options" => Self::Options,
            "patch" => Self::Patch,
            "trace" => Self::Trace,
            _ => Self::None,
        }
    }
    pub fn str(&mut self) -> &'static str {
        match self {
            Method::Post => "POST",
            Method::Get => "GET",
            Method::Head => "HEAD",
            Method::Put => "PUT",
            Method::Delete => "DELETE",
            Method::Options => "OPTIONS",
            Method::Patch => "PATCH",
            Method::Trace => "TRACE",
            Method::None => "",
        }
    }
}
#[derive(Debug, Clone)]
pub enum ContentType {
    FormData,
    FormUrlencoded,
    Json,
    Xml,
    Javascript,
    Text,
    Html,
    Other(String),
}
impl ContentType {
    #[must_use]
    pub fn from(name: &str) -> Self {
        match name {
            "multipart/form-data" => Self::FormData,
            "application/x-www-form-urlencoded" => Self::FormUrlencoded,
            "application/json" => Self::Json,
            "application/xml" | "text/xml" => Self::Xml,
            "application/javascript" => Self::Javascript,
            "text/html" => Self::Html,
            "text/plain" => Self::Text,
            _ => Self::Other(name.to_string()),
        }
    }
    pub fn str(&mut self) -> String {
        match self {
            Self::FormData => "multipart/form-data",
            Self::FormUrlencoded => "application/x-www-form-urlencoded",
            Self::Json => "application/json",
            Self::Xml => "application/xml",
            Self::Javascript => "application/javascript",
            Self::Text => "text/plain",
            Self::Html => "text/html",
            Self::Other(name) => name,
        }.to_string()
    }
}
