//! The type that represents a response of `files.info`.
//!
//! <https://api.slack.com/methods/files.info>

use crate::generated::files_info::FilesInfoResponse as Generated;
use crate::Error;
use serde::Deserialize;

/// The type that represents a response of `files.info`.
///
/// <https://api.slack.com/methods/files.info>
#[derive(Debug, Deserialize)]
pub struct FilesInfoResponse(Generated);

impl FilesInfoResponse {
    /// Converts to generated type.
    pub fn into_inner(self) -> Generated {
        self.0
    }

    /// Takes `url_private_download`.
    pub fn url_private_download(&self) -> Option<&str> {
        self.0
            .file
            .as_ref()
            .and_then(|x| x.url_private_download.as_deref())
    }
}

impl TryFrom<serde_json::Value> for FilesInfoResponse {
    type Error = Error;

    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
        serde_json::from_value(value).map_err(Error::DeserializeJson)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_de_files_info_response() -> anyhow::Result<()> {
        let testdata = r#"{
            "ok": true,
            "file": {
                "id": "F0S43PZDF",
                "created": 1531763342,
                "timestamp": 1531763342,
                "name": "tedair.gif",
                "title": "tedair.gif",
                "mimetype": "image/gif",
                "filetype": "gif",
                "pretty_type": "GIF",
                "user": "U061F7AUR",
                "editable": false,
                "size": 137531,
                "mode": "hosted",
                "is_external": false,
                "external_type": "",
                "is_public": true,
                "public_url_shared": false,
                "display_as_bot": false,
                "username": "",
                "url_private": "https://.../tedair.gif",
                "url_private_download": "https://.../tedair.gif",
                "thumb_64": "https://.../tedair_64.png",
                "thumb_80": "https://.../tedair_80.png",
                "thumb_360": "https://.../tedair_360.png",
                "thumb_360_w": 176,
                "thumb_360_h": 226,
                "thumb_160": "https://.../tedair_=_160.png",
                "thumb_360_gif": "https://.../tedair_360.gif",
                "image_exif_rotation": 1,
                "original_w": 176,
                "original_h": 226,
                "deanimate_gif": "https://.../tedair_deanimate_gif.png",
                "pjpeg": "https://.../tedair_pjpeg.jpg",
                "permalink": "https://.../tedair.gif",
                "permalink_public": "https://.../...",
                "comments_count": 0,
                "is_starred": false,
                "shares": {
                    "public": {
                        "C0T8SE4AU": [
                            {
                                "reply_users": [
                                    "U061F7AUR"
                                ],
                                "reply_users_count": 1,
                                "reply_count": 1,
                                "ts": "1531763348.000001",
                                "thread_ts": "1531763273.000015",
                                "latest_reply": "1531763348.000001",
                                "channel_name": "file-under",
                                "team_id": "T061EG9R6"
                            }
                        ]
                    }
                },
                "channels": [
                    "C0T8SE4AU"
                ],
                "groups": [],
                "ims": [],
                "has_rich_preview": false,
                "alt_txt": "tedair.gif"
            },
            "comments": [],
            "response_metadata": {
                "next_cursor": "dGVhbTpDMUg5UkVTR0w="
            }
        }"#;
        let deserialized: FilesInfoResponse = serde_json::from_str(testdata)?;

        assert_eq!(
            deserialized.url_private_download().unwrap(),
            "https://.../tedair.gif",
        );

        Ok(())
    }
}
