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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::globals::*;
use crate::Job;
use crate::Language;

pub type ResultJSON = std::result::Result<serde_json::Value, std::string::String>;
pub type ResultBytes = std::result::Result<bytes::Bytes, std::string::String>;

macro_rules! endpoint_all {
    ($name:ident, $endpoint:expr) => {
        pub async fn $name(self) -> ResultJSON {
            match reqwest::get(
                (*$endpoint).replace(GARLAND_LANGUAGE_TOKEN, &self.language.get_language()),
            )
            .await
            {
                Ok(response) => match response.error_for_status() {
                    Ok(response) => Ok(serde_json::from_str(
                        &response.text().await.map_err(|e| format!("{:?}", e))?,
                    )
                    .map_err(|e| format!("Error while parsing JSON: {}", e))?),
                    Err(e) => Err(format!("{:?}", e)),
                },
                Err(e) => Err(format!("{:?}", e)),
            }
        }
    };
}

macro_rules! endpoint_with_one_id {
    ($name:ident, $endpoint:expr) => {
        pub async fn $name(self, id: u64) -> ResultJSON {
            match reqwest::get(
                format!("{}{id}.json", *$endpoint)
                    .replace(GARLAND_LANGUAGE_TOKEN, &self.language.get_language()),
            )
            .await
            {
                Ok(response) => match response.error_for_status() {
                    Ok(response) => Ok(serde_json::from_str(
                        &response.text().await.map_err(|e| format!("{:?}", e))?,
                    )
                    .map_err(|e| format!("Error while parsing JSON: {}", e))?),
                    Err(e) => Err(format!("{:?}", e)),
                },
                Err(e) => Err(format!("{:?}", e)),
            }
        }
    };
}

macro_rules! endpoint_with_two_ids {
    ($name:ident, $endpoint:expr, $id0_type:ident, $id1_type:ident) => {
        pub async fn $name(self, id0: $id0_type, id1: $id1_type) -> ResultBytes {
            match reqwest::get(
                format!("{}{id0}/{id1}.png", *$endpoint)
                    .replace(GARLAND_LANGUAGE_TOKEN, &self.language.get_language()),
            )
            .await
            {
                Ok(response) => match response.error_for_status() {
                    Ok(response) => Ok(response.bytes().await.map_err(|e| format!("{:?}", e))?),
                    Err(e) => Err(format!("{:?}", e)),
                },
                Err(e) => Err(format!("{:?}", e)),
            }
        }
    };
}

macro_rules! endpoint_with_job {
    ($name:ident, $endpoint:expr) => {
        pub async fn $name(self, job: Job) -> ResultJSON {
            match reqwest::get(
                format!("{}{}.json", *$endpoint, job.get_short_name())
                    .replace(GARLAND_LANGUAGE_TOKEN, &self.language.get_language()),
            )
            .await
            {
                Ok(response) => match response.error_for_status() {
                    Ok(response) => Ok(serde_json::from_str(
                        &response.text().await.map_err(|e| format!("{:?}", e))?,
                    )
                    .map_err(|e| format!("Error while parsing JSON: {}", e))?),
                    Err(e) => Err(format!("{:?}", e)),
                },
                Err(e) => Err(format!("{:?}", e)),
            }
        }
    };
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct GarlandTools {
    pub language: Language,
}

impl GarlandTools {
    pub fn new(language: Language) -> GarlandTools {
        GarlandTools { language }
    }

    // Achievements
    endpoint_with_one_id!(achievement, GARLAND_TOOLS_ACHIEVEMENT_ENDPOINT);
    endpoint_all!(achievements, GARLAND_TOOLS_ACHIEVEMENTS_ENDPOINT);

    // Data
    endpoint_all!(data, GARLAND_TOOLS_DATA_ENDPOINT);

    // FATE
    endpoint_with_one_id!(fate, GARLAND_TOOLS_FATE_ENDPOINT);
    endpoint_all!(fates, GARLAND_TOOLS_FATES_ENDPOINT);

    // Fishing
    endpoint_all!(fishing, GARLAND_TOOLS_FISHING_ENDPOINT);

    // Instance
    endpoint_with_one_id!(instance, GARLAND_TOOLS_INSTANCE_ENDPOINT);
    endpoint_all!(instances, GARLAND_TOOLS_INSTANCES_ENDPOINT);

    // Item
    endpoint_with_one_id!(item, GARLAND_TOOLS_ITEM_ENDPOINT);

    // Leve
    endpoint_with_one_id!(leve, GARLAND_TOOLS_LEVE_ENDPOINT);
    endpoint_all!(leves, GARLAND_TOOLS_LEVES_ENDPOINT);

    // Mob
    endpoint_with_one_id!(mob, GARLAND_TOOLS_MOB_ENDPOINT);
    endpoint_all!(mobs, GARLAND_TOOLS_MOBS_ENDPOINT);

    // Node
    endpoint_with_one_id!(node, GARLAND_TOOLS_NODE_ENDPOINT);
    endpoint_all!(nodes, GARLAND_TOOLS_NODES_ENDPOINT);

    // NPC
    endpoint_with_one_id!(npc, GARLAND_TOOLS_NPC_ENDPOINT);
    endpoint_all!(npcs, GARLAND_TOOLS_NPCS_ENDPOINT);

    // Quest
    endpoint_with_one_id!(quest, GARLAND_TOOLS_QUEST_ENDPOINT);
    endpoint_all!(quests, GARLAND_TOOLS_QUESTS_ENDPOINT);

    // Status
    endpoint_with_one_id!(status, GARLAND_TOOLS_STATUS_ENDPOINT);
    endpoint_all!(statuses, GARLAND_TOOLS_STATUSES_ENDPOINT);

    // Gear
    endpoint_with_job!(leveling_gear, GARLAND_TOOLS_LEVELLING_ENDPOINT);
    endpoint_with_job!(endgame_gear, GARLAND_TOOLS_ENDGAME_GEAR_ENDPOINT);

    // Maps
    endpoint_with_two_ids!(map_zone, GARLAND_TOOLS_MAP_ENDPOINT, String, String);

    // Icons
    endpoint_with_two_ids!(icon, GARLAND_TOOLS_ICON_ENDPOINT, String, u64);

    // Search
    pub async fn search(self, query: String) -> ResultJSON {
        match reqwest::get(format!(
            "{}?text={}&lang={}",
            *GARLAND_TOOLS_SEARCH_ENDPOINT,
            query,
            self.language.get_language()
        ))
        .await
        {
            Ok(response) => match response.error_for_status() {
                Ok(response) => Ok(serde_json::from_str(
                    &response.text().await.map_err(|e| format!("{:?}", e))?,
                )
                .map_err(|e| format!("Error while parsing JSON: {}", e))?),
                Err(e) => Err(format!("{:?}", e)),
            },
            Err(e) => Err(format!("{:?}", e)),
        }
    }
}