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
use super::PrivacyStatus;
use crate::{
    auth::AuthToken,
    common::{ApiOutcome, PlaylistID, SetVideoID, YoutubeID},
    query::{PostMethod, PostQuery, Query},
};
use serde_json::json;
use std::borrow::Cow;

// TODO: Confirm if all options can be passed - or mutually exclusive.
pub struct EditPlaylistQuery<'a> {
    id: PlaylistID<'a>,
    new_title: Option<Cow<'a, str>>,
    new_description: Option<Cow<'a, str>>,
    new_privacy_status: Option<PrivacyStatus>,
    swap_videos_order: Option<(SetVideoID<'a>, SetVideoID<'a>)>,
    change_add_order: Option<AddOrder>,
    add_playlist: Option<PlaylistID<'a>>,
}

#[derive(Default)]
pub enum AddOrder {
    AddToTop,
    #[default]
    AddToBottom,
}

impl<'a> EditPlaylistQuery<'a> {
    pub fn new_title<T: Into<PlaylistID<'a>>, S: Into<Cow<'a, str>>>(id: T, new_title: S) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: Some(new_title.into()),
            new_description: None,
            new_privacy_status: None,
            swap_videos_order: None,
            change_add_order: None,
            add_playlist: None,
        }
    }
    pub fn new_description<T: Into<PlaylistID<'a>>, S: Into<Cow<'a, str>>>(
        id: T,
        new_description: S,
    ) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: None,
            new_description: Some(new_description.into()),
            new_privacy_status: None,
            swap_videos_order: None,
            change_add_order: None,
            add_playlist: None,
        }
    }
    pub fn new_privacy_status<T: Into<PlaylistID<'a>>>(
        id: T,
        new_privacy_status: PrivacyStatus,
    ) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: None,
            new_privacy_status: Some(new_privacy_status),
            new_description: None,
            swap_videos_order: None,
            change_add_order: None,
            add_playlist: None,
        }
    }
    pub fn swap_videos_order<T: Into<PlaylistID<'a>>>(
        id: T,
        video_1: SetVideoID<'a>,
        video_2: SetVideoID<'a>,
    ) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: None,
            swap_videos_order: Some((video_1, video_2)),
            new_privacy_status: None,
            new_description: None,
            change_add_order: None,
            add_playlist: None,
        }
    }
    pub fn change_add_order<T: Into<PlaylistID<'a>>>(id: T, change_add_order: AddOrder) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: None,
            change_add_order: Some(change_add_order),
            new_privacy_status: None,
            swap_videos_order: None,
            new_description: None,
            add_playlist: None,
        }
    }
    pub fn add_playlist<T: Into<PlaylistID<'a>>>(id: T, add_playlist: PlaylistID<'a>) -> Self {
        let id = id.into();
        Self {
            id,
            new_title: None,
            add_playlist: Some(add_playlist),
            new_privacy_status: None,
            swap_videos_order: None,
            change_add_order: None,
            new_description: None,
        }
    }
    pub fn with_new_description<S: Into<Cow<'a, str>>>(mut self, new_description: S) -> Self {
        self.new_description = Some(new_description.into());
        self
    }
    pub fn with_new_privacy_status(mut self, new_privacy_status: PrivacyStatus) -> Self {
        self.new_privacy_status = Some(new_privacy_status);
        self
    }
    pub fn with_change_add_order(mut self, change_add_order: AddOrder) -> Self {
        self.change_add_order = Some(change_add_order);
        self
    }
    pub fn with_add_playlist(mut self, add_playlist: PlaylistID<'a>) -> Self {
        self.add_playlist = Some(add_playlist);
        self
    }
    pub fn with_swap_videos_order(
        mut self,
        first_video: SetVideoID<'a>,
        second_video: SetVideoID<'a>,
    ) -> Self {
        self.swap_videos_order = Some((first_video, second_video));
        self
    }
}

impl<'a, A: AuthToken> Query<A> for EditPlaylistQuery<'a> {
    type Output = ApiOutcome;
    type Method = PostMethod;
}
impl<'a> PostQuery for EditPlaylistQuery<'a> {
    fn header(&self) -> serde_json::Map<String, serde_json::Value> {
        let mut actions = Vec::new();
        if let Some(new_title) = &self.new_title {
            actions.push(json!({
                "action" : "ACTION_SET_PLAYLIST_NAME",
                "playlistName" : new_title
            }))
        };
        if let Some(new_description) = &self.new_description {
            actions.push(json!({
                "action" : "ACTION_SET_PLAYLIST_DESCRIPTION",
                "playlistDescription" : new_description
            }))
        };
        if let Some(new_privacy_status) = &self.new_privacy_status {
            actions.push(json!({
                "action" : "ACTION_SET_PLAYLIST_PRIVACY",
                "playlistPrivacy" : new_privacy_status
            }))
        };
        if let Some((video_1, video_2)) = &self.swap_videos_order {
            actions.push(json!({
                "action" : "ACTION_MOVE_VIDEO_BEFORE",
                "setVideoId" : video_1,
                "movedSetVideoIdSuccessor" : video_2
            }))
        };
        if let Some(add_playlist) = &self.add_playlist {
            actions.push(json!({
                "action" : "ACTION_ADD_PLAYLIST",
                "addedFullListId" : add_playlist
            }))
        };
        if let Some(change_add_order) = &self.change_add_order {
            let add_to_top = match change_add_order {
                AddOrder::AddToTop => true,
                AddOrder::AddToBottom => false,
            };
            actions.push(json!({
                "action" : "ACTION_SET_ADD_TO_TOP",
                "addToTop" : add_to_top
            }))
        };
        if let Some(new_privacy_status) = &self.new_privacy_status {
            actions.push(json!({
                "action" : "ACTION_SET_PLAYLIST_PRIVACY",
                "playlistPrivacy" : new_privacy_status
            }))
        };
        // TODO: Confirm if VL needs to be stripped / added from playlistId
        // Confirmed!
        let serde_json::Value::Object(map) = json!({
            "playlistId" : self.id.get_raw(),
            "actions" : actions,
        }) else {
            unreachable!()
        };
        map
    }
    fn path(&self) -> &str {
        "browse/edit_playlist"
    }
    fn params(&self) -> Option<Cow<str>> {
        None
    }
}