1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use super::error::Error;
use crate::RsPixel;
use serde_json::Value;

pub async fn username_to_uuid(rs_pixel: &RsPixel, username: &str) -> Result<Response, Error> {
    uuid_username(rs_pixel, username, false).await
}

pub async fn uuid_to_username(rs_pixel: &RsPixel, uuid: &str) -> Result<Response, Error> {
    uuid_username(rs_pixel, uuid, true).await
}

async fn uuid_username(
    rs_pixel: &RsPixel,
    uuid_username: &str,
    is_uuid: bool,
) -> Result<Response, Error> {
    match rs_pixel
        .config
        .client
        .get(match rs_pixel.config.minecraft_api_type {
            ApiType::Mojang => {
                if is_uuid {
                    format!(
                        "https://api.mojang.com/user/profiles/{}/names",
                        uuid_username
                    )
                } else {
                    format!(
                        "https://api.mojang.com/users/profiles/minecraft/{}",
                        uuid_username
                    )
                }
            }
            ApiType::Ashcon => {
                format!("https://api.ashcon.app/mojang/v2/user/{}", uuid_username)
            }
            ApiType::PlayerDb => {
                format!("https://playerdb.co/api/player/minecraft/{}", uuid_username)
            }
        })
        .send()
        .await
    {
        Ok(mut res_unwrap) => {
            let json = res_unwrap.body_json::<Value>().await.map_err(Error::from);

            if res_unwrap.status() == 200 {
                return match json {
                    Ok(json_unwrap) => Ok(match rs_pixel.config.minecraft_api_type {
                        ApiType::Mojang => Response {
                            username: (if is_uuid {
                                json_unwrap
                                    .as_array()
                                    .and_then(|v| v.last())
                                    .and_then(|v| v.get("name"))
                            } else {
                                json_unwrap.get("name")
                            })
                            .and_then(serde_json::Value::as_str)
                            .unwrap_or("")
                            .to_string(),
                            uuid: (if is_uuid {
                                uuid_username
                            } else {
                                json_unwrap
                                    .get("id")
                                    .and_then(serde_json::Value::as_str)
                                    .unwrap_or("")
                            })
                            .to_string(),
                        },
                        ApiType::Ashcon => Response {
                            username: json_unwrap
                                .get("username")
                                .and_then(serde_json::Value::as_str)
                                .unwrap_or("")
                                .to_string(),
                            uuid: json_unwrap
                                .get("uuid")
                                .and_then(serde_json::Value::as_str)
                                .unwrap_or("")
                                .to_string()
                                .replace('-', ""),
                        },
                        ApiType::PlayerDb => Response {
                            username: json_unwrap
                                .get("data")
                                .and_then(|v| v.get("player"))
                                .and_then(|v| v.get("username"))
                                .and_then(serde_json::Value::as_str)
                                .unwrap_or("")
                                .to_string(),
                            uuid: json_unwrap
                                .get("data")
                                .and_then(|v| v.get("player"))
                                .and_then(|v| v.get("id"))
                                .and_then(serde_json::Value::as_str)
                                .unwrap_or("")
                                .to_string()
                                .replace('-', ""),
                        },
                    }),
                    Err(err) => Err(err),
                };
            }

            Err(Error::from((
                res_unwrap.status(),
                json.ok()
                    .as_ref()
                    .and_then(|json_unwrap| {
                        json_unwrap.get(match rs_pixel.config.minecraft_api_type {
                            ApiType::Mojang => "errorMessage",
                            ApiType::Ashcon => "reason",
                            ApiType::PlayerDb => "code",
                        })
                    })
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or("Unknown fail cause")
                    .to_string(),
            )))
        }
        Err(err) => Err(Error::from(err)),
    }
}

#[derive(Debug)]
pub struct Response {
    pub username: String,
    pub uuid: String,
}

pub enum ApiType {
    Mojang,
    Ashcon,
    PlayerDb,
}