pandora-api 0.6.4

Low-level bindings to the (unofficial) Pandora web api.
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
/*!
Station support methods.

A station is a collection of one or more user-supplied seeds. Artists or tracks
can be used as seed. Based on the seeds Pandora decides which music to play.
*/
// SPDX-License-Identifier: MIT AND WTFPL
use std::collections::HashMap;
use std::convert::TryFrom;

use pandora_api_derive::PandoraJsonRequest;
use serde::{Deserialize, Serialize};

use crate::errors::Error;
use crate::json::errors::JsonError;
use crate::json::{PandoraJsonApiRequest, PandoraSession, Timestamp};

/// Songs can be “loved” or “banned”. Both influence the music played on the
/// station. Banned songs are never played again on this particular station.
///
/// | Name         | Type    | Description        |
/// | ------------ | ------- | ------------------ |
/// | stationToken | string  |                    |
/// | trackToken   | string  |                    |
/// | isPositive   | boolean | `false` bans track |
///
/// ``` json
/// {
///     "stationToken": "374145764047334893",
///     "trackToken": "fcc2298ec4b1c93e73ad4b2813ceca0dba565bbbe03d8a78bad65ee89a7aaf4d0b3b11954fe6ab08794283f8ef1d44bfc32ce9f8e0513bec",
///     "isPositive": false,
///     "userAuthToken": "XXX",
///     "syncTime": 1404911036
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct AddFeedback {
    /// The unique id (token) for the station on which the track should be rated.
    /// Also sometimes referred to as a stationId.
    pub station_token: String,
    /// The unique id (token) for the track to be rated.
    pub track_token: String,
    /// Whether feedback is positive (true) or negative (false).
    pub is_positive: bool,
}

impl AddFeedback {
    /// Create a new AddFeedback with some values.
    pub fn new(station_token: &str, track_token: &str, is_positive: bool) -> Self {
        Self {
            station_token: station_token.to_string(),
            track_token: track_token.to_string(),
            is_positive,
        }
    }

    /// Create a new AddFeedback with some values, and positive feedback.
    pub fn new_positive(station_token: &str, track_token: &str) -> Self {
        Self::new(station_token, track_token, true)
    }

    /// Create a new AddFeedback with some values, and negative feedback.
    pub fn new_negative(station_token: &str, track_token: &str) -> Self {
        Self::new(station_token, track_token, false)
    }
}

///
/// | Name          | Type    | Description                  |
/// | ------------- | ------- | ---------------------------- |
/// | dateCreated   | object  |                              |
/// | musicToken    | string  |                              |
/// | songName      | string  |                              |
/// | totalThumbsUp | int     |                              |
/// | feedbackId    | string  | See `station-deleteFeedback` |
/// | isPositive    | boolean |                              |
///
/// ``` json
/// {
///     "stat": "ok",
///     "result": {
///         "totalThumbsDown": 4,
///         "stationPersonalizationPercent": 57,
///         "dateCreated": {
///             "date": 9,
///             "day": 3,
///             "hours": 6,
///             "minutes": 3,
///             "month": 6,
///             "seconds": 56,
///             "time": 1404911036840,
///             "timezoneOffset": 420,
///             "year": 114
///         },
///         "albumArtUrl": "http://cont-sv5-2.pandora.com/images/public/amz/2/2/9/5/094632175922_130W_130H.jpg",
///         "musicToken": "23234b0abdbeb37d",
///         "songName": "Nothing Compares 2 U",
///         "artistName": "Sinead O'Connor",
///         "totalThumbsUp": 20,
///         "feedbackId": "21955050420286614",
///         "isPositive": false
///     }
/// }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddFeedbackResponse {
    /// Timestamp for when the bookmark was created.
    pub date_created: Timestamp,
    /// The unique id (token) for the artist. Artist tokens start with 'R',
    /// composers with 'C', songs with 'S', and genres with 'G'.
    pub music_token: String,
    /// Total positive feedback submissions (for this user across stations? across all users?).
    pub total_thumbs_up: u32,
    /// Total negative feedback submissions (for this user across stations? across all users?).
    pub total_thumbs_down: u32,
    /// The unique id (token) for the submitted feedback.
    pub feedback_id: String,
    /// Whether feedback is positive (true) or negative (false).
    pub is_positive: bool,
    /// The name of the song being rated.
    pub song_name: String,
    /// The name of the artist being rated.
    pub artist_name: String,
    /// A link to an image of the artist.
    pub album_art_url: String,
    /// Unknown
    pub station_personalization_percent: u8,
}

/// Convenience function to do a basic addFeedback call.
pub async fn add_feedback(
    session: &mut PandoraSession,
    station_token: &str,
    track_token: &str,
    is_positive: bool,
) -> Result<AddFeedbackResponse, Error> {
    AddFeedback::new(station_token, track_token, is_positive)
        .response(session)
        .await
}

/// music-search results can be used to add new seeds to an existing station.
///
/// | Name | Type | Description |
/// | stationToken | string | Existing station, see user::get_station_list() |
/// | musicToken | string | See music::search() |
/// ``` json
///     {
///         "musicToken": "R1119",
///         "stationToken": "1181753543028256237",
///         "userAuthToken": "XXX",
///         "syncTime": 1404912202
///     }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct AddMusic {
    /// The unique id (token) for the station on which the track should be rated.
    pub station_token: String,
    /// The unique id (token) for the artist/composer/song/genre to be added to
    /// the station.  Artist tokens start with 'R', composers with 'C', songs
    /// with 'S', and genres with 'G'.
    pub music_token: String,
}

impl AddMusic {
    /// Create a new AddMusic with some values.
    pub fn new(station_token: &str, music_token: &str) -> Self {
        Self {
            station_token: station_token.to_string(),
            music_token: music_token.to_string(),
        }
    }
}

///
/// | Name | Type | Description |
/// | seedId | string | Can be used to remove seed with station::delete_music() |
/// ``` json
///     {
///         "stat": "ok",
///         "result": {
///             "artistName": "Foo Fighters",
///             "musicToken": "3bcf3f314419f974",
///             "seedId": "2123197691273031149",
///             "artUrl": "http://cont-dc6-1.pandora.com/images/public/amg/portrait/pic200/drP900/P972/P97242B3S6P.jpg"
///         }
///     }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddMusicResponse {
    /// The name of the artist being rated.
    pub artist_name: String,
    /// The unique id (token) for the music object added. Artist tokens start with 'R',
    /// composers with 'C', songs with 'S', and genres with 'G'.
    pub music_token: String,
    /// Unknown
    pub seed_id: String,
    /// A link to an image of the added object.
    pub art_url: String,
}

/// Convenience function to do a basic addMusic call.
pub async fn add_music(
    session: &mut PandoraSession,
    station_token: &str,
    music_token: &str,
) -> Result<AddMusicResponse, Error> {
    AddMusic::new(station_token, music_token)
        .response(session)
        .await
}

/// Stations can either be created with a musicToken obtained by Search or
/// trackToken from playlists (Retrieve playlist). The latter needs a musicType
/// to specify whether the track itself or its artist should be used as seed.
///
/// | Name | Type | Description |
/// | trackToken | string | See Retrieve playlist |
/// | musicType  | string | “song” or “artist” (“song” for genre stations) |
/// | musicToken | string | See Search |
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct CreateStation {
    /// The unique id (token) for the track around which the station should
    /// be created.
    pub track_token: String,
    /// Whether the artist or the song referred to by the trackToken should be
    /// used to create the station.
    pub music_type: MusicType,
    /// The unique id (token) for the artist/composer/song/genre to be added to
    /// the station.  Artist tokens start with 'R', composers with 'C', songs
    /// with 'S', and genres with 'G'.
    pub music_token: String,
}

impl CreateStation {
    /// Create a new station from a track, usually from a playlist.
    pub fn new_from_track(track_token: &str, music_type: MusicType) -> Self {
        Self {
            track_token: track_token.to_string(),
            music_type,
            music_token: String::new(),
        }
    }

    /// Create a new station from a musicToken, usually returned by a search.
    pub fn new_from_music_token(music_token: &str) -> Self {
        Self {
            track_token: String::new(),
            music_type: MusicType::Artist,
            music_token: music_token.to_string(),
        }
    }

    /// Create a new CreateStation for a song with some values.
    pub fn new_from_track_song(track_token: &str) -> Self {
        Self::new_from_track(track_token, MusicType::Song)
    }

    /// Create a new CreateStation for an artist with some values.
    pub fn new_from_track_artist(track_token: &str) -> Self {
        Self::new_from_track(track_token, MusicType::Artist)
    }
}

/// Used for selecting whether a musicToken should be interpreted
/// as referring to the associated artist or the associated song.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MusicType {
    /// Use the song referred by the musicToken
    Song,
    /// Use the artist for the song referred by the musicToken
    Artist,
}

/// station.createStation has no known response
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateStationResponse {
    /// The unique id (token) for the just-created station.
    pub station_token: String,
    /// The fields of the createStation response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic createStation call.
pub async fn create_station_from_track_song(
    session: &mut PandoraSession,
    track_token: &str,
) -> Result<CreateStationResponse, Error> {
    CreateStation::new_from_track_song(track_token)
        .response(session)
        .await
}

/// Convenience function to do a basic createStation call.
pub async fn create_station_from_artist(
    session: &mut PandoraSession,
    track_token: &str,
) -> Result<CreateStationResponse, Error> {
    CreateStation::new_from_track_artist(track_token)
        .response(session)
        .await
}

/// Convenience function to do a basic createStation call.
pub async fn create_station_from_music_token(
    session: &mut PandoraSession,
    music_token: &str,
) -> Result<CreateStationResponse, Error> {
    CreateStation::new_from_music_token(music_token)
        .response(session)
        .await
}

/// Feedback added by Rate track can be removed from the station.
///
/// | Name |   Type |   Description |
/// | feedbackId | string | See Retrieve extended station information |
/// ``` json
/// {
///     "feedbackId": "3738252050522320365",
///     "userAuthToken": "XXX",
///     "syncTime": 1404910760
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct DeleteFeedback {
    /// The unique id (token) for the feedback submission that should be deleted.
    pub feedback_id: String,
}

impl<TS: ToString> From<&TS> for DeleteFeedback {
    fn from(feedback_id: &TS) -> Self {
        Self {
            feedback_id: feedback_id.to_string(),
        }
    }
}

/// This method does not return data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteFeedbackResponse {
    /// The fields of the deleteFeedback response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic deleteFeedback call.
pub async fn delete_feedback(
    session: &mut PandoraSession,
    feedback_id: &str,
) -> Result<DeleteFeedbackResponse, Error> {
    DeleteFeedback::from(&feedback_id).response(session).await
}

/// Seeds can be removed from a station, except for the last one.
///
/// | Name   | Type   | Description |
/// | seedId | string | See Retrieve extended station information and Add seed |
/// ``` json
/// {
///     "seedId": "1230715903914683885",
///     "userAuthToken": "XXX",
///     "syncTime": 1404912023
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct DeleteMusic {
    /// The unique id (token) for the music seed that should be deleted
    /// from this station.
    pub seed_id: String,
}

impl<TS: ToString> From<&TS> for DeleteMusic {
    fn from(seed_id: &TS) -> Self {
        Self {
            seed_id: seed_id.to_string(),
        }
    }
}

/// This method does not return data.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteMusicResponse {
    /// The fields of the deleteMusic response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic deleteMusic call.
pub async fn delete_music(
    session: &mut PandoraSession,
    seed_id: &str,
) -> Result<DeleteMusicResponse, Error> {
    DeleteMusic::from(&seed_id).response(session).await
}

/// | Name   | Type  |  Description |
/// | stationToken  |  string | Existing station, see Retrieve station list |
/// ``` json
/// {
///     "stationToken": "374145764047334893",
///     "userAuthToken": "XXX",
///     "syncTime": 1404911699
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct DeleteStation {
    /// The unique id (token) for the station that should be deleted.
    pub station_token: String,
}

impl<TS: ToString> From<&TS> for DeleteStation {
    fn from(station_token: &TS) -> Self {
        Self {
            station_token: station_token.to_string(),
        }
    }
}

/// No data is returned in response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteStationResponse {
    /// The fields of the deleteStation response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic deleteStation call.
pub async fn delete_station(
    session: &mut PandoraSession,
    station_token: &str,
) -> Result<DeleteStationResponse, Error> {
    DeleteStation::from(&station_token).response(session).await
}

/// Check to see if the list of genre stations has changed.
///
/// | Name   | Type   | Description |
/// | includeGenreCategoryAdUrl  | bool  |  (optional) |
#[derive(Debug, Clone, Default, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct GetGenreStationsChecksum {
    /// The fields of the deleteStation response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

impl GetGenreStationsChecksum {
    /// Create a new GetGenreStationsChecksum with some default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Convenience function for setting boolean flags in the request. (Chaining call)
    pub fn and_boolean_option(mut self, option: &str, value: bool) -> Self {
        self.optional
            .insert(option.to_string(), serde_json::value::Value::from(value));
        self
    }

    /// Whether to request that genre category ad url should be included in the reply. (Chaining call)
    pub fn include_genre_category_ad_url(self, value: bool) -> Self {
        self.and_boolean_option("includeGenreCategoryAdUrl", value)
    }
}

/// | Name   | Type  |  Description |
/// | checksum  |  string | |
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetGenreStationsChecksumResponse {
    /// The checksum for the list of genre stations. This is useful to detect
    /// when the list of genre stations has changed so that it can be requested
    /// and refreshed for the user.  This also allows for app caching of the
    /// list across session.
    pub checksum: String,
}

/// Convenience function to do a basic getGenreStationsChecksum call.
pub async fn get_genre_stations_checksum(
    session: &mut PandoraSession,
) -> Result<GetGenreStationsChecksumResponse, Error> {
    GetGenreStationsChecksum::default()
        .include_genre_category_ad_url(false)
        .response(session)
        .await
}

/// Pandora provides a list of predefined stations ("genre stations").
/// The request has no parameters.
#[derive(Debug, Clone, Default, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct GetGenreStations {}

impl GetGenreStations {
    /// Create a new GetGenreStations.
    pub fn new() -> Self {
        Self::default()
    }
}

/// Each station belongs to one category, usually a genre name. stationToken
/// can be used as musicToken to create a new station with Create.
///
/// | Name   | Type  |  Description |
/// | categories | array  | List of categories |
/// | categories.stations | array |  List of stations in category |
/// | categories.stations.stationToken |   string | Actually a musicToken, see Create |
/// | categories.categoryName | string | Category name |
/// ``` json
/// {
///     "stat": "ok",
///     "result": {
///         "categories": [{
///             "stations": [{
///                 "stationToken": "G165",
///                 "stationName": "90s Alternative ",
///                 "stationId": "G165"
///             }],
///             "categoryName": "Alternative"
///         }]
///     }
/// }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetGenreStationsResponse {
    /// The checksum for the list of genre stations. This is useful to detect
    /// when the list of genre stations has changed so that it can be requested
    /// and refreshed for the user.  This also allows for app caching of the
    /// list across session.
    pub categories: Vec<GenreCategory>,
}

/// A collection of stations that fall in a broad genre category
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GenreCategory {
    /// Genre/music category name
    pub category_name: String,
    /// List of stations in the category
    pub stations: Vec<GenreStation>,
}

/// A specific genre station
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GenreStation {
    /// Actually a musicToken, which can be used with station.createStation.
    pub station_token: String,
    /// User-friendly name for the station.
    pub station_name: String,
    /// Unknown
    pub station_id: String,
}

/// Convenience function to do a basic getGenreStations call.
pub async fn get_genre_stations(
    session: &mut PandoraSession,
) -> Result<GetGenreStationsResponse, Error> {
    GetGenreStations::default().response(session).await
}

/// This method must be sent over a TLS-encrypted connection.
///
/// | Name | Type | Description |
/// | stationToken | string | station token from Retrieve station list |
/// | additionalAudioUrl | string | Comma separated list of additional audio formats to return. (optional) |
/// | stationIsStarting | boolean | (optional) |
/// | includeTrackLength | boolean | (optional) |
/// | includeAudioToken | boolean | (optional) |
/// | xplatformAdCapable | boolean | (optional) |
/// | includeAudioReceiptUrl | boolean | (optional) |
/// | includeBackstageAdUrl | boolean | (optional) |
/// | includeSharingAdUrl | boolean | (optional) |
/// | includeSocialAdUrl | boolean | (optional) |
/// | includeCompetitiveSepIndicator | boolean | (optional) |
/// | includeCompletePlaylist | boolean | (optional) |
/// | includeTrackOptions | boolean | (optional) |
/// | audioAdPodCapable | boolean | (optional) |
///
/// Valid values for additionalAudioUrl are:
///
/// * HTTP_40_AAC_MONO
/// * HTTP_64_AAC
/// * HTTP_32_AACPLUS
/// * HTTP_64_AACPLUS
/// * HTTP_24_AACPLUS_ADTS
/// * HTTP_32_AACPLUS_ADTS
/// * HTTP_64_AACPLUS_ADTS
/// * HTTP_128_MP3
/// * HTTP_32_WMA
///
/// Usually a playlist contains four tracks.
/// ``` json
/// {
///      "userAuthToken": "XXX",
///      "additionalAudioUrl":  "HTTP_32_AACPLUS_ADTS,HTTP_64_AACPLUS_ADTS",
///      "syncTime": 1335841463,
///      "stationToken": "121193154444133035"
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct GetPlaylist {
    /// The unique id (token) for the station to request a playlist from
    pub station_token: String,
    /// Optional parameters on the call
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

impl GetPlaylist {
    /// Convenience function for setting boolean flags in the request. (Chaining call)
    pub fn and_boolean_option(mut self, option: &str, value: bool) -> Self {
        self.optional
            .insert(option.to_string(), serde_json::value::Value::from(value));
        self
    }

    /// Additional (non-default) audio formats that should be included in the
    /// response. Repeat call to include additional formats. (Chaining call)
    pub fn additional_audio_url(mut self, value: &str) -> Self {
        // TODO: Verify this logic works
        self.optional
            .entry("additionalAudioUrl".to_string())
            .and_modify(|s| {
                if let serde_json::value::Value::String(s) = s {
                    s.push(',');
                    s.push_str(value);
                }
            })
            .or_insert_with(|| serde_json::value::Value::from(value));
        self
    }

    /// Whether request should also mark the station as starting. (Chaining call)
    pub fn station_is_starting(self, value: bool) -> Self {
        self.and_boolean_option("stationIsStarting", value)
    }

    /// Whether playlist entries should include the track length in the response. (Chaining call)
    pub fn include_track_length(self, value: bool) -> Self {
        self.and_boolean_option("includeTrackLength", value)
    }

    /// Whether playlist entries should include the audio token in the response. (Chaining call)
    pub fn include_audio_token(self, value: bool) -> Self {
        self.and_boolean_option("includeAudioToken", value)
    }

    /// Whether the client is cross-platform ad capable. (Chaining call)
    pub fn xplatform_ad_capable(self, value: bool) -> Self {
        self.and_boolean_option("xplatformAdCapable", value)
    }

    /// Whether to include audio receipt url in the response. (Chaining call)
    pub fn include_audio_receipt_url(self, value: bool) -> Self {
        self.and_boolean_option("includeAudioReceiptUrl", value)
    }

    /// Whether to include backstage ad url in the response. (Chaining call)
    pub fn include_backstage_ad_url(self, value: bool) -> Self {
        self.and_boolean_option("includeBackstageAdUrl", value)
    }

    /// Whether to include sharing ad url in the response. (Chaining call)
    pub fn include_sharing_ad_url(self, value: bool) -> Self {
        self.and_boolean_option("includeSharingAdUrl", value)
    }

    /// Whether to include social ad url in the response. (Chaining call)
    pub fn include_social_ad_url(self, value: bool) -> Self {
        self.and_boolean_option("includeSocialAdUrl", value)
    }

    /// Whether to include competitive sep indicator in the response. (Chaining call)
    pub fn include_competitive_sep_indicator(self, value: bool) -> Self {
        self.and_boolean_option("includeCompetitiveSepIndicator", value)
    }

    /// Whether to include complete playlist in the response. (Chaining call)
    pub fn include_complete_playlist(self, value: bool) -> Self {
        self.and_boolean_option("includeCompletePlaylist", value)
    }

    /// Whether to include track options in the response. (Chaining call)
    pub fn include_track_options(self, value: bool) -> Self {
        self.and_boolean_option("includeTrackOptions", value)
    }

    /// Indicate to Pandora whether the client is audio ad pod capable. (Chaining call)
    pub fn audio_ad_pod_capable(self, value: bool) -> Self {
        self.and_boolean_option("audioAdPodCapable", value)
    }
}

impl<TS: ToString> From<&TS> for GetPlaylist {
    fn from(station_token: &TS) -> Self {
        Self {
            station_token: station_token.to_string(),
            optional: HashMap::new(),
        }
        .additional_audio_url(&AudioFormat::Mp3128.to_string())
    }
}

/// Valid values for additionalAudioUrl are:
///
/// * HTTP_40_AAC_MONO
/// * HTTP_64_AAC
/// * HTTP_32_AACPLUS
/// * HTTP_64_AACPLUS
/// * HTTP_24_AACPLUS_ADTS
/// * HTTP_32_AACPLUS_ADTS
/// * HTTP_64_AACPLUS_ADTS
/// * HTTP_128_MP3
/// * HTTP_32_WMA
#[derive(Debug, Clone, PartialEq)]
pub enum AudioFormat {
    /// AAC format, monaural audio, 40kbps
    AacMono40,
    /// AAC format, 64kbps
    Aac64,
    /// AACPlus format, 32kbps
    AacPlus32,
    /// AACPlus format, 64kbps
    AacPlus64,
    /// AACPlus format in an ADTS container, 24kbps
    AacPlusAdts24,
    /// AACPlus format in an ADTS container, 32kbps
    AacPlusAdts32,
    /// AACPlus format in an ADTS container, 64kbps
    AacPlusAdts64,
    /// MP3 format, 128kbps
    Mp3128,
    /// WMA format, 32kbps
    Wma32,
}

impl AudioFormat {
    /// Determine the audio format from the encoding and bitrate information
    /// returned as part of a playlist track.
    pub fn new_from_audio_url_map(encoding: &str, bitrate: &str) -> Result<Self, Error> {
        match (encoding, bitrate) {
            ("aac", "64") => Ok(Self::AacPlus64),
            ("aacplus", "32") => Ok(Self::AacPlus32),
            ("aacplus", "64") => Ok(Self::AacPlus64),
            _ => Err(
                JsonError::new(None, Some(String::from("Unsupported audioUrlMap format"))).into(),
            ),
        }
    }

    /// Determine the associated file extension for this format.
    pub fn get_extension(&self) -> String {
        match self {
            // TODO: verify container format for all aac types
            Self::AacMono40 => String::from("m4a"),
            Self::Aac64 => String::from("m4a"),
            Self::AacPlus32 => String::from("m4a"),
            Self::AacPlus64 => String::from("m4a"),
            Self::AacPlusAdts24 => String::from("aac"),
            Self::AacPlusAdts32 => String::from("aac"),
            Self::AacPlusAdts64 => String::from("aac"),
            Self::Mp3128 => String::from("mp3"),
            Self::Wma32 => String::from("wma"),
        }
    }

    /// Determine the encoded audio bitrate for this format.
    pub fn get_bitrate(&self) -> u32 {
        match self {
            Self::AacMono40 => 40,
            Self::Aac64 => 64,
            Self::AacPlus32 => 32,
            Self::AacPlus64 => 64,
            Self::AacPlusAdts24 => 24,
            Self::AacPlusAdts32 => 32,
            Self::AacPlusAdts64 => 64,
            Self::Mp3128 => 128,
            Self::Wma32 => 32,
        }
    }

    /// Estimator of relative audio quality. The actual numbers don't
    /// mean anything, it's just for assigning an ordering.
    fn get_quality_weight(&self) -> u8 {
        match self {
            Self::AacPlusAdts64 => 10,
            Self::AacPlus64 => 9,
            // MP3 at 128kbps using a high quality encoder is estimated
            // to be equivalent to AAC-HE at 64kbps.  Because we don't
            // know the quality of the mp3 encoder, we weigh it below 64kbps
            // AacPlus, but above 64kbps Aac.
            // https://en.wikipedia.org/wiki/High-Efficiency_Advanced_Audio_Coding
            Self::Mp3128 => 8,
            Self::Aac64 => 7,
            Self::AacPlusAdts32 => 6,
            Self::AacPlus32 => 5,
            Self::AacPlusAdts24 => 4,
            // Aac is a good codec, but AacPlus holds up much better at low
            // bitrates, plus this is monoaural.
            Self::AacMono40 => 2,
            // 32kbps is an incredibly low bitrate, on an old codec
            // so this is theorized to be the lowest quality
            Self::Wma32 => 1,
        }
    }
}

impl PartialOrd for AudioFormat {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.get_quality_weight().cmp(&other.get_quality_weight()))
    }
}

impl std::fmt::Display for AudioFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AudioFormat::AacMono40 => write!(f, "HTTP_40_AAC_MONO"),
            AudioFormat::Aac64 => write!(f, "HTTP_64_AAC"),
            AudioFormat::AacPlus32 => write!(f, "HTTP_32_AACPLUS"),
            AudioFormat::AacPlus64 => write!(f, "HTTP_64_AACPLUS"),
            AudioFormat::AacPlusAdts24 => write!(f, "HTTP_24_AACPLUS_ADTS"),
            AudioFormat::AacPlusAdts32 => write!(f, "HTTP_32_AACPLUS_ADTS"),
            AudioFormat::AacPlusAdts64 => write!(f, "HTTP_64_AACPLUS_ADTS"),
            AudioFormat::Mp3128 => write!(f, "HTTP_128_MP3"),
            AudioFormat::Wma32 => write!(f, "HTTP_32_WMA"),
        }
    }
}

impl TryFrom<&str> for AudioFormat {
    type Error = Error;
    fn try_from(fmt: &str) -> std::result::Result<Self, Self::Error> {
        match fmt {
            "HTTP_40_AAC_MONO" => Ok(AudioFormat::AacMono40),
            "HTTP_64_AAC" => Ok(AudioFormat::Aac64),
            "HTTP_32_AACPLUS" => Ok(AudioFormat::AacPlus32),
            "HTTP_64_AACPLUS" => Ok(AudioFormat::AacPlus64),
            "HTTP_24_AACPLUS_ADTS" => Ok(AudioFormat::AacPlusAdts24),
            "HTTP_32_AACPLUS_ADTS" => Ok(AudioFormat::AacPlusAdts32),
            "HTTP_64_AACPLUS_ADTS" => Ok(AudioFormat::AacPlusAdts64),
            "HTTP_128_MP3" => Ok(AudioFormat::Mp3128),
            "HTTP_32_WMA" => Ok(AudioFormat::Wma32),
            x => Err(Self::Error::InvalidAudioFormat(x.to_string())),
        }
    }
}

impl TryFrom<String> for AudioFormat {
    type Error = Error;
    fn try_from(fmt: String) -> std::result::Result<Self, Self::Error> {
        Self::try_from(fmt.as_str())
    }
}

/// | Name | Type | Description |
/// | items.additionalAudioUrl | array/string | List of additional audio urls in the requested order or single string if only one format was requested |
/// | items.songRating | int | 1 if song was given a thumbs up, 0 if song was not rated yet |
/// | items.audioUrlMap | object | Song audio format and bitrates returned differ based on what partner credentials are used. |
/// ``` json
/// {
///      "stat": "ok",
///      "result": {
///          "items": [{
///              "trackToken": "40b892bc5376e695c2e5c2b347227b85af2761b6aa417f736d9a79319b8f4cb97c9695a5f9a9a32aa2abaed43571235c",
///              "artistName": "Cannabich, Christian",
///              "albumName": "London Mozart Players, Christian Cannabich: Symphonies",
///              "amazonAlbumUrl": "http://www.amazon.com/dp/B000GW8ATU/?tag=wwwpandoracom-20",
///              "songExplorerUrl": "http://www.pandora.com/xml/music/song/london-mozart-players/christian-cannabich-symphonies/2-andantino?explicit=false",
///              "albumArtUrl": "http://cont-sv5-2.pandora.com/images/public/amz/5/2/9/7/095115137925_500W_488H.jpg",
///              "artistDetailUrl": "http://www.pandora.com/christian-cannabich?...",
///              "audioUrlMap": {
///                  "highQuality": {
///                      "bitrate": "64",
///                      "encoding": "aacplus",
///                      "audioUrl": "http://audio-sjl-t1-2.pandora.com/access/166132182435087962.mp4?...",
///                      "protocol": "http"
///                  },
///                  "mediumQuality": {
///                      "bitrate": "64",
///                      "encoding": "aacplus",
///                      "audioUrl": "http://t1-2.cdn.pandora.com/access/4127124196771074419.mp4?...",
///                      "protocol": "http"
///                  },
///                  "lowQuality": {
///                      "bitrate": "32",
///                      "encoding": "aacplus",
///                      "audioUrl": "http://audio-sv5-t1-1.pandora.com/access/3464788359714661029.mp4?...",
///                      "protocol": "http"
///                  }
///              },
///              "itunesSongUrl": "http://click.linksynergy.com/fs-bin/stat?...",
///              "additionalAudioUrl": [
///                  "http://t1-2.cdn.pandora.com/access/6705986462049243054.mp4?...",
///                  "http://audio-sjl-t1-1.pandora.com/access/2473529637452270302.mp4?..."
///              ],
///              "amazonAlbumAsin": "B000GW8ATU",
///              "amazonAlbumDigitalAsin": "B003H37NN4",
///              "artistExplorerUrl": "http://www.pandora.com/xml/music/composer/christian-cannabich?explicit=false",
///              "songName": "Symphony In G Major",
///              "albumDetailUrl": "http://www.pandora.com/london-mozart-players/christian-cannabich-symphonies?...",
///              "songDetailUrl": "http://www.pandora.com/london-mozart-players/christian-cannabich-symphonies/2-andantino?...",
///              "stationId": "121193154444133035",
///              "songRating": 0,
///              "trackGain": "10.09",
///              "albumExplorerUrl": "http://www.pandora.com/xml/music/album/london-mozart-players/christian-cannabich-symphonies?explicit=false",
///              "allowFeedback": true,
///              "amazonSongDigitalAsin": "B003H39AGW",
///              "nowPlayingStationAdUrl": "http://ad.doubleclick.net/pfadx/pand.android/prod.nowplaying..."
///          }, {
///              "adToken": "121193154444133035-none"
///          },
///          ]
///      }
/// }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetPlaylistResponse {
    /// Contains a list of playlist entries, each being either a song/track or
    /// an ad.
    pub items: Vec<PlaylistEntry>,
}

/// Responses can be either a track or an ad.
/// The responses don't have a standard tag identifying which type it is,
/// but ads have only one value: adToken: String.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum PlaylistEntry {
    /// Playlist entry representing an ad.
    PlaylistAd(PlaylistAd),
    /// Playlist entry representing a song/track.
    PlaylistTrack(Box<PlaylistTrack>),
}

impl PlaylistEntry {
    /// Returns whether the playlist entry is an ad
    pub fn is_ad(&self) -> bool {
        matches!(self, PlaylistEntry::PlaylistAd(_))
    }

    /// Returns whether the playlist entry is a track
    pub fn is_track(&self) -> bool {
        matches!(self, PlaylistEntry::PlaylistTrack(_))
    }

    /// Returns the PlaylistAd object for this entry, if any
    pub fn get_ad(&self) -> Option<PlaylistAd> {
        match self {
            PlaylistEntry::PlaylistAd(a) => Some(a.clone()),
            _ => None,
        }
    }

    /// Returns the PlaylistTrack object for this entry, if any
    pub fn get_track(&self) -> Option<PlaylistTrack> {
        match self {
            PlaylistEntry::PlaylistTrack(t) => Some(*t.clone()),
            _ => None,
        }
    }
}

/// Represents an ad entry in a playlist.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaylistAd {
    /// The unique id (token) for the ad which should be played.
    pub ad_token: String,
    /// Additional, optional fields in the response
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Represents a track (song) entry in a playlist.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlaylistTrack {
    /// The unique id (token) for the track to be played.
    pub track_token: String,
    /// The music id (token) used with GetTrack to request additional track
    /// information.
    pub music_id: String,
    /// The unique id (token) for the station from which this track was
    /// requested.
    pub station_id: String,
    /// The default audio streams available for this track.
    pub audio_url_map: AudioQuality,
    /// The name of the artist for this track.
    pub artist_name: String,
    /// The name of the album for this track.
    pub album_name: String,
    /// The name of the song for this track.
    pub song_name: String,
    /// The rating of the song for this track.
    pub song_rating: u32,
    /// Additional, optional fields in the response
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

///                  "lowQuality": {
///                      "bitrate": "32",
///                      "encoding": "aacplus",
///                      "audioUrl": "http://audio-sv5-t1-1.pandora.com/access/3464788359714661029.mp4?...",
///                      "protocol": "http"
///                  }
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioQuality {
    /// Attributes for the high quality audio stream.
    pub high_quality: AudioStream,
    /// Attributes for the medium quality audio stream.
    pub medium_quality: AudioStream,
    /// Attributes for the low quality audio stream.
    pub low_quality: AudioStream,
}

/// Playback/decoding attributes of an available audio stream.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioStream {
    /// The audio bitrate/quality for this stream.
    pub bitrate: String,
    /// The audio encoding format for this stream.
    pub encoding: String,
    /// The url to stream audio from.
    pub audio_url: String,
    /// The protocol to use with the audio URL.
    pub protocol: String,
}

/// Convenience function to do a basic getPlaylist call.
pub async fn get_playlist(
    session: &mut PandoraSession,
    station_token: &str,
) -> Result<GetPlaylistResponse, Error> {
    GetPlaylist::from(&station_token)
        .station_is_starting(false)
        .include_track_length(false)
        .include_audio_token(false)
        .xplatform_ad_capable(false)
        .include_audio_receipt_url(false)
        .include_backstage_ad_url(false)
        .include_sharing_ad_url(false)
        .include_social_ad_url(false)
        .include_competitive_sep_indicator(false)
        .include_complete_playlist(false)
        .include_track_options(false)
        .audio_ad_pod_capable(false)
        .response(session)
        .await
}

/// Extended station information includes seeds and feedback.
///
/// | Name | Type | Description |
/// | stationToken | string |  |
/// | includeExtendedAttributes | bool |  |
/// ``` json
/// {
///     "stationToken": "374145764047334893",
///     "includeExtendedAttributes": true,
///     "userAuthToken": "XXX",
///     "syncTime": 1404910732
/// }
/// ```
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct GetStation {
    /// The unique id (token) for the station to request information on.
    pub station_token: String,
    /// The fields of the createStation response are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

impl GetStation {
    /// Convenience function for setting boolean flags in the request. (Chaining call)
    pub fn and_boolean_option(mut self, option: &str, value: bool) -> Self {
        self.optional
            .insert(option.to_string(), serde_json::value::Value::from(value));
        self
    }

    /// Whether request should include extended station attributes in the response. (Chaining call)
    pub fn include_extended_attributes(self, value: bool) -> Self {
        self.and_boolean_option("includeExtendedAttributes", value)
    }
}

impl<TS: ToString> From<&TS> for GetStation {
    fn from(station_token: &TS) -> Self {
        GetStation {
            station_token: station_token.to_string(),
            optional: HashMap::new(),
        }
    }
}

/// | Name | Type | Description |
/// | music | object | Station seeds, see Add seed |
/// | music.songs | list | Song seeds |
/// | music.artists | list | Artist seeds |
/// | feedback | object | Feedback added by Rate track |
/// | feedback.thumbsUp | list |   |
/// | feedback.thumbsDown | list |   |
/// ``` json
/// {
///     "stat": "ok",
///     "result": {
///         "suppressVideoAds": false,
///         "stationId": "374145764047334893",
///         "allowAddMusic": true,
///         "dateCreated": {
///             "date": 15,
///             "day": 6,
///             "hours": 7,
///             "minutes": 34,
///             "month": 0,
///             "nanos": 874000000,
///             "seconds": 21,
///             "time": 1295105661874,
///             "timezoneOffset": 480,
///             "year": 111
///         },
///         "stationDetailUrl": "https://www.pandora.com/login?target=%2Fstations%2Fc644756145fc3f5df1916901125ee697495159685ae39575",
///         "artUrl": "http://cont-1.p-cdn.com/images/public/amz/5/2/8/5/075678235825_500W_498H.jpg",
///         "requiresCleanAds": false,
///         "stationToken": "374145764047334893",
///         "stationName": "Winter Radio",
///         "music": {
///             "songs": [{
///                 "seedId": "428301990230109677",
///                 "artistName": "Tori Amos",
///                 "artUrl": "http://cont-sjl-1.pandora.com/images/public/amz/5/2/8/5/075678235825_130W_130H.jpg",
///                 "songName": "Winter",
///                 "musicToken": "87ef9db1c3f04330"
///             }],
///             "artists": [{
///                 "artistName": "Jason Derulo",
///                 "musicToken": "563f577e00d837a5",
///                 "seedId": "31525199612287328",
///                 "artUrl": "http://mediaserver-cont-sv5-1-v4v6.pandora.com/images/public/amg/portrait/pic200/drQ300/Q366/Q36675SDAPJ.jpg"
///             }],
///             "genres": [{
///                 "musicToken": "cc021b31a48b8acf",
///                 "genreName": "Today's Hits",
///                 "seedId": "31525199599467854"
///             }]
///         },
///         "isShared": false,
///         "allowDelete": true,
///         "genre": ["Rock"],
///         "isQuickMix": false,
///         "allowRename": true,
///         "stationSharingUrl": "https://www.pandora.com/login?target=%2Fshare%2Fstation%2Fc644756145fc3f5df1916901125ee697495159685ae39575",
///         "allowEditDescription": true,
///         "feedback": {
///             "thumbsUp": [{
///                 "dateCreated": {
///                     "date": 28,
///                     "day": 5,
///                     "hours": 13,
///                     "minutes": 57,
///                     "month": 2,
///                     "nanos": 760000000,
///                     "seconds": 49,
///                     "time": 1396040269760,
///                     "timezoneOffset": 420,
///                     "year": 114
///                 },
///                 "albumArtUrl": "http://cont-1.p-cdn.com/images/public/amz/9/7/1/4/900004179_130W_130H.jpg",
///                 "musicToken": "d33dd0c199ebaf28425ba2910f7abf8b",
///                 "songName": "Hey Lover",
///                 "artistName": "Keri Noble",
///                 "feedbackId": "-7239441039566426643",
///                 "isPositive": true
///             }],
///             "totalThumbsUp": 20,
///             "totalThumbsDown": 5,
///             "thumbsDown": [{
///                 "dateCreated": {
///                     "date": 28,
///                     "day": 5,
///                     "hours": 10,
///                     "minutes": 43,
///                     "month": 2,
///                     "nanos": 637000000,
///                     "seconds": 30,
///                     "time": 1396028610637,
///                     "timezoneOffset": 420,
///                     "year": 114
///                 },
///                 "albumArtUrl": "http://cont-ch1-1.pandora.com/images/public/amz/9/0/5/1/724383771509_130W_130H.jpg",
///                 "musicToken": "5a0018da7876f6e7",
///                 "songName": "Talk Show Host",
///                 "artistName": "Radiohead",
///                 "feedbackId": "-7241622182873125395",
///                 "isPositive": false
///             }]
///         }
///     }
/// }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetStationResponse {
    /// The unique id (token) for the station for which information was
    /// requested. The stationId (station_id) and stationToken (station_token)
    /// attributes appear to be duplicates.
    pub station_id: String,
    /// The unique id (token) for the station for which information was
    /// requested. The stationId (station_id) and stationToken (station_token)
    /// attributes appear to be duplicates.
    pub station_token: String,
    /// The user-created name of the station.
    pub station_name: String,
    /// Whether the station allows adding music to it.
    pub allow_add_music: Option<bool>,
    /// Unknown
    pub suppress_video_ads: Option<bool>,
    /// When the station was created.
    pub date_created: Timestamp,
    /// Unknown
    pub station_detail_url: Option<String>,
    /// Unknown
    pub art_url: Option<String>,
    /// Unknown
    pub requires_clean_ads: Option<bool>,
    /// Station music seeds.
    pub music: Option<StationSeeds>,
    /// Whether the station is visible for sharing.
    pub is_shared: Option<bool>,
    /// Whether the station can be deleted.
    pub allow_delete: Option<bool>,
    /// The genre(s) the station belongs to.
    #[serde(default)]
    pub genre: Vec<String>,
    /// Whether this is a QuickMix station.
    pub is_quick_mix: Option<bool>,
    /// Whether the station may be renamed.
    pub allow_rename: Option<bool>,
    /// The URL to use for sharing this station.
    pub station_sharing_url: Option<String>,
    /// Whether the description for this station may be edited.
    pub allow_edit_description: Option<bool>,
    /// Feedback submitted for tracks on this station.
    pub feedback: Option<StationFeedback>,
}

/// ``` json
///         "music": {
///             "songs": [],
///             "artists": [],
///             "genres": []
///         },
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StationSeeds {
    /// Songs used as seeds for this station.
    pub songs: Vec<SongSeed>,
    /// Atrists used as seeds for this station.
    pub artists: Vec<ArtistSeed>,
    /// Genres used as seeds for this station.
    pub genres: Vec<GenreSeed>,
}

/// Attributes of a song seed for a station.
/// ``` json
///             "songs": [{
///                 "seedId": "5629501782357373",
///                 "musicToken": "9d8f932edea76ed8425ba2910f7abf8b",
///                 "songName": "Soul Finger",
///                 "artistName": "The Bar-Kays",
///                 "pandoraType": "TR",
///                 "pandoraId": "TR:852695",
///                 "artUrl": "http://.../081227857165_130W_130H.jpg",
///             }],
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SongSeed {
    /// Unique identifier/handle for this seed.
    pub seed_id: String,
    /// Identifier for the song used for this seed.
    pub music_token: String,
    /// Name of the song used for this seed.
    pub song_name: String,
    /// Name of the artist for the song used for this seed.
    pub artist_name: String,
    /// The type of Pandora object described by the Pandora ID.
    pub pandora_type: String,
    /// An identifier for this Pandora object that is unique across all types of Pandora
    /// objects.
    pub pandora_id: String,
    /// Unknown
    pub art_url: String,
    /// Unknown fields in the response, if any
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Attributes of an artist seed for a station.
/// ``` json
///             "artists": [{
///                 "seedId": "5629501764244877",
///                 "musicToken": "2858b602eb1adfa8",
///                 "artistName": "Michael Bublé",
///                 "pandoraType": "AR"
///                 "pandoraId": "AR:6533",
///                 "artUrl": "http://.../90W_90H.jpg",
///                 "icon": {"dominantColor": "602d30","artUrl": ""},
///             ],}
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArtistSeed {
    /// Unique identifier/handle for this seed.
    pub seed_id: String,
    /// Identifier for the artist used for this seed.
    pub music_token: String,
    /// Name of the artist used for this seed.
    pub artist_name: String,
    /// The type of Pandora object described by the Pandora ID.
    pub pandora_type: String,
    /// An identifier for this Pandora object that is unique across all types of Pandora
    /// objects.
    pub pandora_id: String,
    /// Artist icon
    pub icon: HashMap<String, String>,
    /// Unknown fields in the response, if any
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Attributes of a genre seed for a station.
/// ``` json
///             "genres": [{
///                 "musicToken": "cc021b31a48b8acf",
///                 "genreName": "Today's Hits",
///                 "seedId": "31525199599467854"
///             }]
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GenreSeed {
    /// Unique identifier/handle for this seed.
    pub seed_id: String,
    /// Identifier for the genre used for this seed.
    pub music_token: String,
    /// Name of the genre used for this seed.
    pub genre_name: String,
    /// Unknown fields in the response, if any
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// ``` json
///         "feedback": {
///             "thumbsUp": [],
///             "totalThumbsUp": 20,
///             "totalThumbsDown": 5,
///             "thumbsDown": []
///         }
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StationFeedback {
    /// A list of positive feedback submitted to a station.
    pub thumbs_up: Vec<TrackFeedback>,
    /// The total number of positive submissions to a station.
    pub total_thumbs_up: u32,
    /// A list of negative feedback submitted to a station.
    pub thumbs_down: Vec<TrackFeedback>,
    /// The total number of negative submissions to a station.
    pub total_thumbs_down: u32,
}

/// ``` json
///             "thumbsDown": [{
///                 "dateCreated": {
///                     "date": 28,
///                     "day": 5,
///                     "hours": 10,
///                     "minutes": 43,
///                     "month": 2,
///                     "nanos": 637000000,
///                     "seconds": 30,
///                     "time": 1396028610637,
///                     "timezoneOffset": 420,
///                     "year": 114
///                 },
///                 "albumArtUrl": "http://cont-ch1-1.pandora.com/images/public/amz/9/0/5/1/724383771509_130W_130H.jpg",
///                 "musicToken": "5a0018da7876f6e7",
///                 "songName": "Talk Show Host",
///                 "artistName": "Radiohead",
///                 "feedbackId": "-7241622182873125395",
///                 "isPositive": false
///             }]
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrackFeedback {
    /// Unique identifier/handle referring to this feedback submission.
    pub feedback_id: String,
    /// Name of the song that was rated.
    pub song_name: String,
    /// Name of the artist for the song that was rated.
    pub artist_name: String,
    /// Whether the rating is positive (true) or negative (false).
    pub is_positive: bool,
    /// A token referring to the song that was rated.
    pub music_token: String,
    /// Date the feedback was created.
    pub date_created: Timestamp,
    /// Unknown
    pub album_art_url: String,
}

/// Convenience function to do a basic getStation call.
pub async fn get_station(
    session: &mut PandoraSession,
    station_token: &str,
) -> Result<GetStationResponse, Error> {
    GetStation::from(&station_token)
        .include_extended_attributes(false)
        .response(session)
        .await
}

/// **Unsupported!**
/// Undocumented method
/// [station.publishStationShare()](https://6xq.net/pandora-apidoc/json/methods/)
pub struct PublishStationShareUnsupported {}

/// | Name   | Type |   Description |
/// | stationToken  |  string | Existing station, see Retrieve station list |
/// | stationName | string | New station name |
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct RenameStation {
    /// The unique id (token) for the station that should be renamed.
    /// Also sometimes referred to as a stationId.
    pub station_token: String,
    /// The new name that should be used for this station.
    pub station_name: String,
}

impl RenameStation {
    /// Create a new RenameStation with some initial values.
    pub fn new(station_token: &str, station_name: &str) -> Self {
        Self {
            station_token: station_token.to_string(),
            station_name: station_name.to_string(),
        }
    }
}

/// There's no known response data to this request.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RenameStationResponse {
    /// The fields of the renameStation response, if any, are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic renameStation call.
pub async fn rename_station(
    session: &mut PandoraSession,
    station_token: &str,
    station_name: &str,
) -> Result<RenameStationResponse, Error> {
    RenameStation::new(station_token, station_name)
        .response(session)
        .await
}

/// Shares a station with the specified email addresses. that emails is a string array
///
/// | Name  |  Type |   Description |
/// | stationId |  string | See Retrieve station list |
/// | stationToken |   string | See Retrieve station list |
/// | emails | string[] |   A list of emails to share the station with |
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct ShareStation {
    /// The unique id (token) for the station that should be shared.
    /// Also sometimes referred to as a stationId.
    pub station_id: String,
    /// The unique id (token) for the station that should be shared.
    /// Also sometimes referred to as a stationId.
    pub station_token: String,
    /// A list of emails to share the station with.
    pub emails: Vec<String>,
}

impl ShareStation {
    /// Create a new RenameStation with some initial values.  Call
    /// add_recipient() to add recipient emails to the request.
    pub fn new(station_id: &str, station_token: &str) -> Self {
        Self {
            station_id: station_id.to_string(),
            station_token: station_token.to_string(),
            emails: Vec::new(),
        }
    }

    /// Add a recipient email to the request.
    pub fn add_recipient(&mut self, recipient: &str) {
        self.emails.push(recipient.to_string());
    }
}

/// There's no known response data to this request.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShareStationResponse {
    /// The fields of the shareStation response, if any, are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic shareStation call.
pub async fn share_station(
    session: &mut PandoraSession,
    station_id: &str,
    station_token: &str,
    emails: Vec<String>,
) -> Result<ShareStationResponse, Error> {
    let mut request = ShareStation::new(station_id, station_token);
    request.emails = emails;
    request.response(session).await
}

/// Stations created by other users are added as reference to the user’s
/// station list. These stations cannot be modified (i.e. rate tracks) unless
/// transformed.
///
/// | Name   |  Type  |   Description |
/// | stationToken  |   string |  See Retrieve station list |
#[derive(Debug, Clone, Serialize, PandoraJsonRequest)]
#[pandora_request(encrypted = true)]
#[serde(rename_all = "camelCase")]
pub struct TransformSharedStation {
    /// The unique id (token) for the shared station that should be converted to
    /// a personal station.
    /// Also sometimes referred to as a stationId.
    pub station_token: String,
}

impl<TS: ToString> From<&TS> for TransformSharedStation {
    fn from(station_token: &TS) -> Self {
        Self {
            station_token: station_token.to_string(),
        }
    }
}

/// There's no known response data to this request.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransformSharedStationResponse {
    /// The fields of the transformSharedStation response, if any, are unknown.
    #[serde(flatten)]
    pub optional: HashMap<String, serde_json::value::Value>,
}

/// Convenience function to do a basic transformSharedStation call.
pub async fn transform_shared_station(
    session: &mut PandoraSession,
    station_token: &str,
) -> Result<TransformSharedStationResponse, Error> {
    TransformSharedStation::from(&station_token)
        .response(session)
        .await
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;
    use crate::json::{
        music::search, music::ArtistMatch, tests::session_login, user::get_station_list, Partner,
    };

    // TODO: share_station, transform_shared_station,
    #[tokio::test]
    async fn station_ops_test() {
        // TODO: ensure that the station we intend to create didn't get leaked
        // by a previous, failed test execution, look for stations named either
        // "INXS Radio" or "XSNI Radio"
        let partner = Partner::default();
        let mut session = session_login(&partner)
            .await
            .expect("Failed initializing login session");

        let artist_search = search(&mut session, "INXS")
            .await
            .expect("Failed completing artist search request");

        let additional_artist_search = search(&mut session, "Panic! At the Disco")
            .await
            .expect("Failed completing artist search request");

        if let Some(ArtistMatch { music_token, .. }) = artist_search
            .artists
            .iter()
            .filter(|am| am.score == 100)
            .next()
        {
            let created_station = create_station_from_music_token(&mut session, &music_token)
                .await
                .expect("Failed creating station from search result");

            let _renamed_station =
                rename_station(&mut session, &created_station.station_token, "XSNI Radio")
                    .await
                    .expect("Failed renaming station");

            if let Some(ArtistMatch { music_token, .. }) = additional_artist_search
                .artists
                .iter()
                .filter(|am| am.score == 100)
                .next()
            {
                let added_music =
                    add_music(&mut session, &created_station.station_token, music_token)
                        .await
                        .expect("Failed adding music to station");

                let _del_music = delete_music(&mut session, &added_music.seed_id)
                    .await
                    .expect("Failed deleting music from station");
            }

            let _del_station = delete_station(&mut session, &created_station.station_token)
                .await
                .expect("Failed deleting station");
        }
    }

    /* This test is very demanding on the server, so we disable it until we want
     * to retest.
    #[tokio::test]
    async fn genre_stations_test() {
        let partner = Partner::default();
        let mut session = session_login(&partner).await.expect("Failed initializing login session");

        let genre_stations = get_genre_stations(&mut session).await
            .expect("Failed getting genre stations");

        let genre_stations_checksum = get_genre_stations_checksum(&mut session).await
            .expect("Failed getting genre stations checksum");
    }
    */

    #[tokio::test]
    async fn station_feedback_test() {
        let partner = Partner::default();
        let mut session = session_login(&partner)
            .await
            .expect("Failed initializing login session");

        for station in get_station_list(&mut session)
            .await
            .expect("Failed getting station list to look up a track to bookmark")
            .stations
        {
            // Look through feedback on the station and build up a list of
            // already-rated songs so that we don't mess with any pre-existing
            // ratings during this test.  This also exercises get_station.
            let station = GetStation::from(&station.station_token)
                .include_extended_attributes(true)
                .response(&mut session)
                .await
                .expect("Failed getting station attributes");

            let mut protected_tracks: HashSet<String> = HashSet::new();
            protected_tracks.extend(
                station
                    .feedback
                    .iter()
                    .flat_map(|f| f.thumbs_up.iter())
                    .map(|tf| tf.song_name.clone()),
            );
            protected_tracks.extend(
                station
                    .feedback
                    .iter()
                    .flat_map(|f| f.thumbs_down.iter())
                    .map(|tf| tf.song_name.clone()),
            );

            for track in get_playlist(&mut session, &station.station_token)
                .await
                .expect("Failed completing request for playlist")
                .items
                .iter()
                .flat_map(|p| p.get_track())
            {
                if protected_tracks.contains(&track.song_name) {
                    continue;
                }

                // Thumbs-up track
                let feedback = add_feedback(
                    &mut session,
                    &station.station_token,
                    &track.track_token,
                    true,
                )
                .await
                .expect("Failed adding positive feedback to track");
                // And delete
                let _del_feedback = delete_feedback(&mut session, &feedback.feedback_id)
                    .await
                    .expect("Failed deleting positive feedback from track");
                // Thumbs-down track
                let feedback = add_feedback(
                    &mut session,
                    &station.station_token,
                    &track.track_token,
                    false,
                )
                .await
                .expect("Failed adding negative feedback to track");
                // And delete
                let _del_feedback = delete_feedback(&mut session, &feedback.feedback_id)
                    .await
                    .expect("Failed deleting negative feedback from track");

                // Finished test, stop looping through
                return;
            }
        }
        panic!("Station list request returned no results, so no feedback-capable content.");
    }
}