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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use super::{
    search_query_header, search_query_params, AuthToken, PostMethod, PostQuery, Query, SearchQuery,
    SearchType, SpellingMode, SEARCH_QUERY_PATH, SPECIALIZED_PLAYLIST_EXACT_MATCH_PARAMS,
    SPECIALIZED_PLAYLIST_PREFIX_PARAMS, SPECIALIZED_PLAYLIST_WITH_SUGGESTIONS_PARAMS,
};
use crate::parse::{
    SearchResultAlbum, SearchResultArtist, SearchResultEpisode, SearchResultFeaturedPlaylist,
    SearchResultPlaylist, SearchResultPodcast, SearchResultProfile, SearchResultSong,
    SearchResultVideo,
};
use std::borrow::Cow;

// TODO Seal
// TODO: Add param bits
// Implements Default to allow simple implementation of
// Into<SearchQuery<FilteredSearch<F>>>
pub trait FilteredSearchType: Default {
    fn filtered_param_bits(&self) -> Cow<str>;
    // By implementing a default method, we can specialize for cases were these
    // params are incorrect.
    fn filtered_spelling_param(&self, spelling_mode: &SpellingMode) -> Cow<str> {
        match spelling_mode {
            SpellingMode::ExactMatch => "AWoMEA4QChADEAQQCRAF".into(),
            SpellingMode::WithSuggestions => "AUICCAFqDBAOEAoQAxAEEAkQBQ%3D%3D".into(),
        }
    }
    // By implementing a default method, we can specialize for cases were these
    // params are incorrect.
    fn filtered_prefix_param(&self) -> Cow<str> {
        "EgWKAQ".into()
    }
}
/// Helper struct for SearchQuery
#[derive(Default, Debug, Clone, PartialEq)]
pub struct FilteredSearch<F: FilteredSearchType> {
    pub(crate) filter: F,
}
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct SongsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct VideosFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct AlbumsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct ArtistsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct PlaylistsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct CommunityPlaylistsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct FeaturedPlaylistsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct EpisodesFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct PodcastsFilter;
/// Helper struct for FilteredSearch type state pattern.
#[derive(Default, PartialEq, Debug, Clone)]
pub struct ProfilesFilter;

impl<F: FilteredSearchType> SearchType for FilteredSearch<F> {
    fn specialised_params(&self, spelling_mode: &SpellingMode) -> Option<Cow<str>> {
        Some(
            format!(
                "{}{}{}",
                self.filter.filtered_prefix_param(),
                self.filter.filtered_param_bits(),
                self.filter.filtered_spelling_param(spelling_mode),
            )
            .into(),
        )
    }
}

// Implementations of FilteredSearchType
impl FilteredSearchType for SongsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "II".into()
    }
}
impl FilteredSearchType for VideosFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "IQ".into()
    }
}
impl FilteredSearchType for AlbumsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "IY".into()
    }
}
impl FilteredSearchType for ArtistsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "Ig".into()
    }
}
impl FilteredSearchType for PlaylistsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        // When filtering for Library params should be "Io"...
        "".into()
    }
    fn filtered_spelling_param(&self, spelling_mode: &SpellingMode) -> Cow<str> {
        match spelling_mode {
            SpellingMode::ExactMatch => "MABCAggBagoQBBADEAkQBRAK",
            SpellingMode::WithSuggestions => "MABqChAEEAMQCRAFEAo%3D",
        }
        .into()
    }
    fn filtered_prefix_param(&self) -> Cow<str> {
        "Eg-KAQwIABAAGAAgACgB".into()
    }
}
impl FilteredSearchType for CommunityPlaylistsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "EA".into()
    }
    fn filtered_spelling_param(&self, spelling_mode: &SpellingMode) -> Cow<str> {
        match spelling_mode {
            SpellingMode::ExactMatch => SPECIALIZED_PLAYLIST_EXACT_MATCH_PARAMS,
            SpellingMode::WithSuggestions => SPECIALIZED_PLAYLIST_WITH_SUGGESTIONS_PARAMS,
        }
        .into()
    }
    fn filtered_prefix_param(&self) -> Cow<str> {
        SPECIALIZED_PLAYLIST_PREFIX_PARAMS.into()
    }
}
impl FilteredSearchType for FeaturedPlaylistsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "Dg".into()
    }
    fn filtered_spelling_param(&self, spelling_mode: &SpellingMode) -> Cow<str> {
        match spelling_mode {
            SpellingMode::ExactMatch => SPECIALIZED_PLAYLIST_EXACT_MATCH_PARAMS,
            SpellingMode::WithSuggestions => SPECIALIZED_PLAYLIST_WITH_SUGGESTIONS_PARAMS,
        }
        .into()
    }
    fn filtered_prefix_param(&self) -> Cow<str> {
        SPECIALIZED_PLAYLIST_PREFIX_PARAMS.into()
    }
}
impl FilteredSearchType for EpisodesFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "JI".into()
    }
}
impl FilteredSearchType for PodcastsFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "JQ".into()
    }
}
impl FilteredSearchType for ProfilesFilter {
    fn filtered_param_bits(&self) -> Cow<str> {
        "JY".into()
    }
}
// Implementations of Query
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<SongsFilter>> {
    type Output = Vec<SearchResultSong>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<SongsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<PlaylistsFilter>> {
    type Output = Vec<SearchResultPlaylist>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<PlaylistsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<CommunityPlaylistsFilter>> {
    type Output = Vec<SearchResultPlaylist>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<CommunityPlaylistsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<AlbumsFilter>> {
    type Output = Vec<SearchResultAlbum>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<AlbumsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<ArtistsFilter>> {
    type Output = Vec<SearchResultArtist>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<ArtistsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<FeaturedPlaylistsFilter>> {
    type Output = Vec<SearchResultFeaturedPlaylist>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<FeaturedPlaylistsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<EpisodesFilter>> {
    type Output = Vec<SearchResultEpisode>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<EpisodesFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<PodcastsFilter>> {
    type Output = Vec<SearchResultPodcast>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<PodcastsFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<VideosFilter>> {
    type Output = Vec<SearchResultVideo>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<VideosFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}
impl<'a, A: AuthToken> Query<A> for SearchQuery<'a, FilteredSearch<ProfilesFilter>> {
    type Output = Vec<SearchResultProfile>;
    type Method = PostMethod;
}
impl<'a> PostQuery for SearchQuery<'a, FilteredSearch<ProfilesFilter>> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        search_query_header(self)
    }
    fn path(&self) -> &str {
        SEARCH_QUERY_PATH
    }
    fn params(&self) -> Option<Cow<str>> {
        search_query_params(self)
    }
}