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
//! General project related entities

use std::sync::Arc;
use derivative::Derivative;
use crate::api::{Api, self};
use crate::cursor::Cursor;
use super::{User, project_stream::*, stream::GeneralStream};
use s2rs_derive::deref;

// region: ProjectWithTitle
/// Extends [`Project`] with it's title
/// # Examples
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// let project_with_title = project.meta().await.unwrap().this; // `this` field stores ProjectWithTitle
/// dbg![meta.title, meta.id];
/// # })
/// ```
#[derive(Debug, PartialEq, Eq)]
#[deref(this)]
pub struct ProjectWithTitle {
    pub title: String,
    pub this: Arc<Project>
}

impl ProjectWithTitle {
    pub fn new(title: String, id: u64, api: Arc<Api>) -> Arc<Self> {
        Self::with_this(title, Project::new(id, api))
    }

    pub fn with_this(title: String, this: Arc<Project>) -> Arc<Self> {
        Arc::new(Self {
            this,
            title
        })
    }
}
// endregion: ProjectWithTitle

// region: ProjectCore
#[derive(Debug)]
pub struct ProjectCoreRaw {
    pub description: String,
    pub instructions: String,
    pub visibility: String,
    pub public: bool,
    pub comments_allowed: bool,
    pub is_published: bool,
    pub image: String,
    pub images: api::ProjectImages,
    pub stats: api::ProjectStats,
    pub remix: api::ProjectRemix,
    pub history: api::ProjectHistory,
}

#[derive(Debug)]
#[deref(this)]
pub struct ProjectCore {
    pub this: Arc<ProjectWithTitle>,
    pub description: String,
    pub instructions: String,
    pub visibility: String,
    pub public: bool,
    pub comments_allowed: bool,
    pub is_published: bool,
    pub image: String,
    pub images: api::ProjectImages,
    pub stats: api::ProjectStats,
    pub remix: api::ProjectRemix,
    pub history: api::ProjectHistory,
}

impl ProjectCore {
    pub fn with_this(data: ProjectCoreRaw, this: Arc<ProjectWithTitle>, api: Arc<Api>) -> Arc<Self> {
        Arc::new(Self {
            this,
            image: data.image,
            images: data.images,
            instructions: data.instructions,
            is_published: data.is_published,
            public: data.public,
            remix: data.remix,
            stats: data.stats,
            visibility: data.visibility,
            comments_allowed: data.comments_allowed,
            description: data.description,
            history: data.history
        })
    }

    pub fn new(data: ProjectCoreRaw, id: u64, title: String, api: Arc<Api>) -> Arc<Self> {
        Self::with_this(data, ProjectWithTitle::new(title, id, api.clone()), api)
    }
}
// endregion: ProjectCore

// region: ProjectMeta
/// Project metadata
/// - Mapping for <https://api.scratch.mit.edu/projects/PROJECT-ID>
/// # Examples
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// let meta = project.meta().await.unwrap();
/// dbg![
///     &meta.author.history.joined,
///     &meta.token
///     // ...
/// ];
/// # })
/// ```
#[derive(Debug)]
#[deref(this)]
pub struct ProjectMeta {
    pub this: Arc<ProjectCore>,
    pub author: ProjectAuthor,
    pub token: String
}

impl ProjectMeta {
    pub fn with_this_this(data: api::Project, this: Arc<ProjectWithTitle>, api: Arc<Api>) -> Arc<Self> {
        Arc::new(Self {
            this: ProjectCore::with_this(
                ProjectCoreRaw {
                    comments_allowed: data.comments_allowed,
                    description: data.description,
                    history: data.history,
                    image: data.image,
                    images: data.images,
                    instructions: data.instructions,
                    is_published: data.is_published,
                    public: data.public,
                    remix: data.remix,
                    stats: data.stats,
                    visibility: data.visibility
                },
            this, api.clone()),
            author: ProjectAuthor::new(data.author, api),
            token: data.token
        })
    }

    pub fn with_this_this_this(data: api::Project, this: Arc<Project>, api: Arc<Api>) -> Arc<Self> {
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::with_this(title, this), api)
    }

    pub fn new(data: api::Project, api: Arc<Api>) -> Arc<Self> {
        let id = data.id;
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::new(title, id, api.clone()), api)
    }

    pub fn vec_new(data: Vec<api::Project>, api: Arc<Api>) -> Vec<Arc<Self>> {
        data.into_iter().map(|data| Self::new(data, api.clone())).collect()
    }
}
// endregion: ProjectMeta

// region: ProjectAuthor
/// Project author
/// # Examples
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// let author = &project.meta().await.unwrap().author;
/// dbg![&author.scratch_team];
/// # })
/// ```
#[derive(Debug)]
#[deref(this)]
pub struct ProjectAuthor {
    pub this: Arc<User>,
    pub scratch_team: bool,
    pub history: api::UserHistory,
    pub profile: api::ProjectAuthorProfile,
}

impl ProjectAuthor {
    pub fn new(data: api::ProjectAuthor, api: Arc<Api>) -> Self {
        Self {
            this: User::new(data.name, api),
            history: data.history,
            profile: data.profile,
            scratch_team: data.scratch_team,
        }
    }

    pub fn with_this(data: api::ProjectAuthor, this: Arc<User>) -> Self {
        Self {
            this,
            history: data.history,
            profile: data.profile,
            scratch_team: data.scratch_team,
        }
    }
}
// endregion: ProjectAuthor

// region: PartialProject
/// Partial project
/// - Used to map some parts of API that return project metadata which is not the same as [`ProjectMeta`]
/// # Examples TODO
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// let author = &project.meta().await.unwrap().author;
/// dbg![&author.scratch_team];
/// # })
/// ```
#[derive(Debug)]
#[deref(this)]
pub struct PartialProject {
    pub this: Arc<ProjectCore>,
    pub author: api::PartialProjectAuthor,
}

impl PartialProject {
    pub fn with_this_this(data: api::PartialProject, this: Arc<ProjectWithTitle>, api: Arc<Api>) -> Arc<Self> {
        Arc::new(Self {
            author: data.author,
            this: ProjectCore::with_this(
                ProjectCoreRaw {
                    comments_allowed: data.comments_allowed,
                    description: data.description,
                    history: data.history,
                    image: data.image,
                    images: data.images,
                    instructions: data.instructions,
                    is_published: data.is_published,
                    public: data.public,
                    remix: data.remix,
                    stats: data.stats,
                    visibility: data.visibility
                },
            this, api),
        })
    }

    pub fn with_this_this_this(data: api::PartialProject, this: Arc<Project>, api: Arc<Api>) -> Arc<Self> {
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::with_this(title, this), api)
    }

    pub fn new(data: api::PartialProject, api: Arc<Api>) -> Arc<Self> {
        let id = data.id;
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::new(title, id, api.clone()), api)
    }

    pub fn vec_new(data: Vec<api::PartialProject>, api: Arc<Api>) -> Vec<Arc<Self>> {
        data.into_iter().map(|data| Self::new(data, api.clone())).collect()
    }
}
// endregion: PartialProject

// region: PartialProject2
#[deref(this)]
#[derive(Debug)]
pub struct PartialProject2 {
    pub this: Arc<ProjectCore>,
    pub author: ProjectAuthor,
}

impl PartialProject2 {
    pub fn with_this_this(data: api::PartialProject2, this: Arc<ProjectWithTitle>, api: Arc<Api>) -> Arc<Self> {
        Arc::new(Self {
            author: ProjectAuthor::new(data.author, api.clone()),
            this: ProjectCore::with_this(
                ProjectCoreRaw {
                    comments_allowed: data.comments_allowed,
                    description: data.description,
                    history: data.history,
                    image: data.image,
                    images: data.images,
                    instructions: data.instructions,
                    is_published: data.is_published,
                    public: data.public,
                    remix: data.remix,
                    stats: data.stats,
                    visibility: data.visibility
                },
            this, api),
        })
    }

    pub fn with_this_this_this(data: api::PartialProject2, this: Arc<Project>, api: Arc<Api>) -> Arc<Self> {
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::with_this(title, this), api)
    }

    pub fn new(data: api::PartialProject2, api: Arc<Api>) -> Arc<Self> {
        let id = data.id;
        let title = data.title.clone();
        Self::with_this_this(data, ProjectWithTitle::new(title, id, api.clone()), api)
    }

    pub fn vec_new(data: Vec<api::PartialProject2>, api: Arc<Api>) -> Vec<Arc<Self>> {
        data.into_iter().map(|data| Self::new(data, api.clone())).collect()
    }
}
// endregion: PartialProject2

// region: Project

/// Project identifier
/// # Examples
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// project.love().await.unwrap();
/// // ...
/// # })
/// ```
/// ```
/// # tokio_test::block_on(async {
/// # use s2rs::session::Session;
/// # let session = Session::new("YourUsername");
/// let project = session.project(823872487);
/// project.love().await.unwrap(); // Love project
/// dbg![studio.meta().await.unwrap()]; // Gets project metadata
/// project.set_title("This is a cool project!").await.unwrap();
/// project.send_comment("epic comment").await.unwrap();
/// for project in project.remixes((0, 13)).all().await.unwrap() {
///     project.love().await.unwrap();
/// }
/// // ...
/// # })
/// ```
#[derive(Derivative)]
#[derivative(Debug, PartialEq, Eq)]
pub struct Project {
    #[derivative(Debug="ignore", PartialEq="ignore")]
    api: Arc<Api>,
    pub id: u64,
}

impl Project {
    pub fn new(id: u64, api: Arc<Api>) -> Arc<Self> {
        Arc::new(Self {
            api,
            id
        })
    }
}

impl Project {
    pub async fn meta(self: &Arc<Self>) -> Result<Arc<ProjectMeta>, api::GeneralError> {
        Ok(ProjectMeta::with_this_this_this(self.api.get_project_meta(self.id).await?, self.clone(), self.api.clone()))
    }

    pub fn remixes(self: &Arc<Self>, cursor: impl Into<Cursor>) -> GeneralStream<ProjectRemixes> {
        GeneralStream::with_this(ProjectRemixes, cursor.into(), self.clone(), self.api.clone())
    }

    pub fn comments(self: &Arc<Self>, cursor: impl Into<Cursor>) -> GeneralStream<ProjectComments> {
        GeneralStream::with_this(ProjectComments, cursor.into(), self.clone(), self.api.clone())
    }

    pub fn cloud_activity(self: &Arc<Self>, cursor: impl Into<Cursor>) -> GeneralStream<ProjectCloudActivity> {
        GeneralStream::with_this(ProjectCloudActivity, cursor.into(), self.clone(), self.api.clone())
    }

    pub async fn love(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.love_project(self.id).await?)
    }

    pub async fn unlove(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.unlove_project(self.id).await?)
    }

    pub async fn favorite(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.favorite_project(self.id).await?)
    }

    pub async fn unfavorite(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.unfavorite_project(self.id).await?)
    }

    pub async fn unshare(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.unshare_project(self.id).await?)
    }

    pub async fn send_comment(&self, content: &str) -> Result<(), api::GeneralError> {
        Ok(self.api.send_project_comment(self.id, content, None, None).await?)
    }

    pub async fn delete_comment(&self, id: u64) -> Result<(), api::GeneralError> {
        Ok(self.api.delete_project_comment(self.id, id).await?)
    }

    pub async fn view(&self) -> Result<(), api::GeneralError> {
        Ok(self.api.view_project(self.id).await?)
    }

    pub async fn set_commenting(&self, allowed: bool) -> Result<(), api::GeneralError> {
        Ok(self.api.set_project_commenting(self.id, allowed).await?)
    }
}
// endregion: Project