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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::api::RadioBrowserAPI;
use crate::structs::ApiStation;
use std::fmt::Display;

use std::collections::HashMap;
use std::error::Error;

pub enum StationOrder {
    Name,
    Url,
    Homepage,
    Favicon,
    Tags,
    Country,
    State,
    Language,
    Votes,
    Codec,
    Bitrate,
    Lastcheckok,
    Lastchecktime,
    Clicktimestamp,
    Clickcount,
    Clicktrend,
    Changetimestamp,
    Random,
}

impl Display for StationOrder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        match self {
            StationOrder::Name => write!(f, "name"),
            StationOrder::Url => write!(f, "url"),
            StationOrder::Homepage => write!(f, "homepage"),
            StationOrder::Favicon => write!(f, "favicon"),
            StationOrder::Tags => write!(f, "tags"),
            StationOrder::Country => write!(f, "country"),
            StationOrder::State => write!(f, "state"),
            StationOrder::Language => write!(f, "language"),
            StationOrder::Votes => write!(f, "votes"),
            StationOrder::Codec => write!(f, "codec"),
            StationOrder::Bitrate => write!(f, "bitrate"),
            StationOrder::Lastcheckok => write!(f, "lastcheckok"),
            StationOrder::Lastchecktime => write!(f, "lastchecktime"),
            StationOrder::Clicktimestamp => write!(f, "clicktimestamp"),
            StationOrder::Clickcount => write!(f, "clickcount"),
            StationOrder::Clicktrend => write!(f, "clicktrend"),
            StationOrder::Changetimestamp => write!(f, "changetimestamp"),
            StationOrder::Random => write!(f, "random"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct StationSearchBuilder {
    map: HashMap<String, String>,
    api: RadioBrowserAPI,
}

impl StationSearchBuilder {
    pub fn new(api: RadioBrowserAPI) -> Self {
        StationSearchBuilder {
            api,
            map: HashMap::new(),
        }
    }

    pub fn name<P: AsRef<str>>(mut self, name: P) -> Self {
        self.map
            .insert(String::from("name"), name.as_ref().to_string());
        self
    }

    pub fn name_exact(mut self, name_exact: bool) -> Self {
        self.map
            .insert(String::from("nameExact"), name_exact.to_string());
        self
    }

    pub fn country<P: AsRef<str>>(mut self, country: P) -> Self {
        self.map
            .insert(String::from("country"), country.as_ref().to_string());
        self
    }

    pub fn country_exact(mut self, country_exact: bool) -> Self {
        self.map
            .insert(String::from("countryExact"), country_exact.to_string());
        self
    }

    pub fn countrycode<P: AsRef<str>>(mut self, countrycode: P) -> Self {
        self.map.insert(
            String::from("countrycode"),
            countrycode.as_ref().to_string(),
        );
        self
    }

    pub fn state<P: AsRef<str>>(mut self, state: P) -> Self {
        self.map
            .insert(String::from("state"), state.as_ref().to_string());
        self
    }

    pub fn state_exact(mut self, state_exact: bool) -> Self {
        self.map
            .insert(String::from("stateExact"), state_exact.to_string());
        self
    }

    pub fn language<P: AsRef<str>>(mut self, language: P) -> Self {
        self.map
            .insert(String::from("language"), language.as_ref().to_string());
        self
    }

    pub fn language_exact(mut self, language_exact: bool) -> Self {
        self.map
            .insert(String::from("languageExact"), language_exact.to_string());
        self
    }

    pub fn tag<P: AsRef<str>>(mut self, tag: P) -> Self {
        self.map
            .insert(String::from("tag"), tag.as_ref().to_string());
        self
    }

    pub fn tag_exact(mut self, tag_exact: bool) -> Self {
        self.map
            .insert(String::from("tagExact"), tag_exact.to_string());
        self
    }

    pub fn tag_list(mut self, tags: Vec<&str>) -> Self {
        self.map
            .insert(String::from("tagList"), tags.join(","));
        self
    }

    pub fn codec<P: AsRef<str>>(mut self, codec: P) -> Self {
        self.map
            .insert(String::from("codec"), codec.as_ref().to_string());
        self
    }

    pub fn bitrate_min(mut self, bitrate_min: u16) -> Self {
        self.map
            .insert(String::from("bitrateMin"), bitrate_min.to_string());
        self
    }

    pub fn bitrate_max(mut self, bitrate_max: u16) -> Self {
        self.map
            .insert(String::from("bitrateMax"), bitrate_max.to_string());
        self
    }

    pub fn has_geo_info(mut self, has_geo_info: bool) -> Self {
        self.map
            .insert(String::from("has_geo_info"), has_geo_info.to_string());
        self
    }

    pub fn has_extended_info(mut self, has_extended_info: bool) -> Self {
        self.map.insert(
            String::from("has_extended_info"),
            has_extended_info.to_string(),
        );
        self
    }

    pub fn is_https(mut self, is_https: bool) -> Self {
        self.map
            .insert(String::from("is_https"), is_https.to_string());
        self
    }

    pub fn order(mut self, order: StationOrder) -> Self {
        self.map.insert(String::from("order"), order.to_string());
        self
    }

    pub fn reverse(mut self, reverse: bool) -> Self {
        self.map
            .insert(String::from("reverse"), reverse.to_string());
        self
    }

    pub fn offset<P: AsRef<str>>(mut self, offset: P) -> Self {
        self.map
            .insert(String::from("offset"), offset.as_ref().to_string());
        self
    }

    pub fn limit<P: AsRef<str>>(mut self, limit: P) -> Self {
        self.map
            .insert(String::from("limit"), limit.as_ref().to_string());
        self
    }

    pub fn hidebroken(mut self, hidebroken: bool) -> Self {
        self.map
            .insert(String::from("hidebroken"), hidebroken.to_string());
        self
    }

    pub async fn send(mut self) -> Result<Vec<ApiStation>, Box<dyn Error>> {
        Ok(self.api.send("/json/stations/search", self.map).await?)
    }
}