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
use crate::{BitField8, Error};
use std::{
    fmt::{self, Display},
    str::FromStr,
};

/// (De)Serializable field that tracks which videos have been watched
/// and the latest one watched.
///
/// This is a [`WatchedBitField`] compatible field, (de)serialized
/// without the knowledge of `videos_ids`.
///
/// `{anchor:video_id}:{anchor_length}:{bitfield8}`
///
/// # Examples
///
/// ```
/// use stremio_watched_bitfield::WatchedField;
///
/// // `tt2934286:1:5` - anchor video id
/// // `5` - anchor video length
/// // `eJyTZwAAAEAAIA==` - BitField8
///
/// let watched = "tt2934286:1:5:5:eJyTZwAAAEAAIA=="
///     .parse::<WatchedField>()
///     .expect("Should parse");
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WatchedField {
    /// The anchor video id
    ///
    /// Indicates which is the last watched video id.
    anchor_video: String,
    /// The length from the beginning of the `BitField8` to the last
    /// watched video.
    anchor_length: usize,
    bitfield: BitField8,
}

impl Display for WatchedField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}:{}:{}",
            self.anchor_video, self.anchor_length, self.bitfield
        )
    }
}

impl From<WatchedBitField> for WatchedField {
    fn from(watched_bit_field: WatchedBitField) -> Self {
        let last_id = watched_bit_field.bitfield.last_index_of(true).unwrap_or(0);
        let last_video_id = watched_bit_field
            .video_ids
            .get(last_id)
            .map_or_else(|| "undefined".to_string(), |id| id.clone());

        Self {
            anchor_video: last_video_id,
            anchor_length: last_id + 1,
            bitfield: watched_bit_field.bitfield,
        }
    }
}

impl FromStr for WatchedField {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self, Self::Err> {
        // serialized is formed by {id}:{len}:{serializedBuf}, but since {id} might contain : we have to pop gradually and then keep the rest
        let mut components = string.split(':').collect::<Vec<&str>>();

        if components.len() < 3 {
            return Err(Error("Not enough components".to_string()));
        }
        let bitfield_buf = components
            .pop()
            .ok_or("Cannot obtain the serialized data")?
            .to_string();

        let anchor_length = components
            .pop()
            .ok_or("Cannot obtain the length field")?
            .parse::<usize>()?;
        let anchor_video_id = components.join(":");

        let bitfield = BitField8::try_from((bitfield_buf, None))?;

        Ok(Self {
            bitfield,
            anchor_video: anchor_video_id,
            anchor_length,
        })
    }
}

/// Tracks which videos have been watched.
///
/// Serialized in the format `{id}:{len}:{serializedBuf}` but since `{id}`
/// might contain `:` we pop gradually and then keep the rest.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WatchedBitField {
    bitfield: BitField8,
    video_ids: Vec<String>,
}

impl WatchedBitField {
    pub fn construct_from_array(arr: Vec<bool>, video_ids: Vec<String>) -> WatchedBitField {
        let mut bitfield = BitField8::new(video_ids.len());
        for (i, val) in arr.iter().enumerate() {
            bitfield.set(i, *val);
        }

        WatchedBitField {
            bitfield,
            video_ids,
        }
    }

    pub fn new(bitfield: BitField8, video_ids: Vec<String>) -> WatchedBitField {
        Self {
            bitfield,
            video_ids,
        }
    }

    pub fn construct_with_videos(
        watched_field: WatchedField,
        video_ids: Vec<String>,
    ) -> Result<WatchedBitField, Error> {
        // We can shift the bitmap in any direction, as long as we can find the anchor video
        if let Some(anchor_video_idx) = video_ids
            .iter()
            .position(|s| s == &watched_field.anchor_video)
        {
            // TODO: replace with `usize` and `checked_sub` when more tests are added for negative ids
            let offset = watched_field.anchor_length as i32 - anchor_video_idx as i32 - 1;
            let bitfield =
                BitField8::new_with_values(watched_field.bitfield.values, Some(video_ids.len()));

            // in case of an previous empty array, this will be 0
            if offset != 0 {
                // Resize the buffer
                let mut resized_wbf = WatchedBitField {
                    bitfield: BitField8::new(video_ids.len()),
                    video_ids: video_ids.clone(),
                };

                // rewrite the old buf into the new one, applying the offset
                for i in 0..video_ids.len() {
                    // TODO: Check what will happen if we change it to `usize`
                    let id_in_prev = i as i32 + offset;
                    if id_in_prev >= 0 && (id_in_prev as usize) < bitfield.length {
                        resized_wbf.set(i, bitfield.get(id_in_prev as usize));
                    }
                }
                Ok(resized_wbf)
            } else {
                Ok(WatchedBitField {
                    bitfield,
                    video_ids,
                })
            }
        } else {
            // videoId could not be found, return a totally blank buf
            Ok(WatchedBitField {
                bitfield: BitField8::new(video_ids.len()),
                video_ids,
            })
        }
    }

    pub fn construct_and_resize(
        serialized: &str,
        video_ids: Vec<String>,
    ) -> Result<WatchedBitField, Error> {
        // note: videoIds.length could only be >= from serialized lastLength
        // should we assert?
        // we might also wanna assert that the bitfield.length for the returned wb is the same sa videoIds.length

        let watched_field = serialized.parse()?;

        Self::construct_with_videos(watched_field, video_ids)
    }

    pub fn get(&self, idx: usize) -> bool {
        self.bitfield.get(idx)
    }

    pub fn get_video(&self, video_id: &str) -> bool {
        if let Some(pos) = self.video_ids.iter().position(|s| *s == video_id) {
            self.bitfield.get(pos)
        } else {
            false
        }
    }

    pub fn set(&mut self, idx: usize, v: bool) {
        self.bitfield.set(idx, v);
    }

    pub fn set_video(&mut self, video_id: &str, v: bool) {
        if let Some(pos) = self.video_ids.iter().position(|s| *s == video_id) {
            self.bitfield.set(pos, v);
        }
    }
}

impl fmt::Display for WatchedBitField {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let packed = String::try_from(&self.bitfield).expect("bitfield failed to compress");
        let last_id = self.bitfield.last_index_of(true).unwrap_or(0);
        let last_video_id = self
            .video_ids
            .get(last_id)
            .map_or("undefined", |id| id.as_str());

        write!(f, "{}:{}:{}", last_video_id, last_id + 1, packed)
    }
}

impl From<WatchedBitField> for BitField8 {
    fn from(watched: WatchedBitField) -> Self {
        watched.bitfield
    }
}

/// Module containing all the impls of the `serde` feature
#[cfg(feature = "serde")]
mod serde {
    use std::str::FromStr;

    use serde::{de, Serialize};

    use super::WatchedField;

    impl<'de> serde::Deserialize<'de> for WatchedField {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: serde::Deserializer<'de>,
        {
            let serialized = String::deserialize(deserializer)?;

            WatchedField::from_str(&serialized).map_err(de::Error::custom)
        }
    }

    impl Serialize for WatchedField {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            serializer.serialize_str(&self.to_string())
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{BitField8, WatchedBitField, WatchedField};

    #[test]
    fn parse_and_modify() {
        let videos = [
            "tt2934286:1:1",
            "tt2934286:1:2",
            "tt2934286:1:3",
            "tt2934286:1:4",
            "tt2934286:1:5",
            "tt2934286:1:6",
            "tt2934286:1:7",
            "tt2934286:1:8",
            "tt2934286:1:9",
        ];
        let watched = "tt2934286:1:5:5:eJyTZwAAAEAAIA==";
        let mut wb = WatchedBitField::construct_and_resize(
            watched,
            videos.iter().map(|v| v.to_string()).collect(),
        )
        .unwrap();

        assert!(wb.get_video("tt2934286:1:5"));
        assert!(!wb.get_video("tt2934286:1:6"));

        assert_eq!(watched, wb.to_string());

        wb.set_video("tt2934286:1:6", true);
        assert!(wb.get_video("tt2934286:1:6"));
    }

    #[test]
    fn construct_from_array() {
        let arr = vec![false; 500];
        let mut video_ids = vec![];
        for i in 1..500 {
            video_ids.push(format!("tt2934286:1:{}", i));
        }
        let mut wb = WatchedBitField::construct_from_array(arr, video_ids.clone());

        // All should be false
        for (i, val) in video_ids.iter().enumerate() {
            assert!(!wb.get(i));
            assert!(!wb.get_video(val));
        }

        // Set half to true
        for (i, _val) in video_ids.iter().enumerate() {
            wb.set(i, i % 2 == 0);
        }

        // Serialize and deserialize to new structure
        let watched = wb.to_string();
        let wb2 = WatchedBitField::construct_and_resize(
            &watched,
            video_ids.iter().map(|v| v.to_string()).collect(),
        )
        .unwrap();

        // Half should still be true
        for (i, val) in video_ids.iter().enumerate() {
            assert_eq!(wb2.get(i), i % 2 == 0);
            assert_eq!(wb2.get_video(val), i % 2 == 0);
        }
    }

    #[test]
    fn to_string_empty() {
        let watched = WatchedBitField::construct_from_array(vec![], vec![]);
        let serialized = watched.to_string();
        assert_eq!(serialized, "undefined:1:eJwDAAAAAAE=");
    }

    #[test]
    #[cfg(feature = "serde")]
    fn test_watched_field_de_serialize() {
        let string = "tt7767422:3:8:24:eJz7//8/AAX9Av4=";
        let json_value = serde_json::json!(string);

        let expected = string.parse::<WatchedField>().expect("Should parse field");

        let actual_from_json = serde_json::from_value::<WatchedField>(json_value.clone())
            .expect("Should deserialize ");
        assert_eq!(expected, actual_from_json);
        assert_eq!("eJz7//8/AAX9Av4=", &actual_from_json.bitfield.to_string());
        assert_eq!(24, actual_from_json.anchor_length);
        assert_eq!("tt7767422:3:8", actual_from_json.anchor_video);

        let actual_to_json = serde_json::to_value(&expected).expect("Should serialize");
        assert_eq!(json_value, actual_to_json);
    }

    #[test]
    fn deserialize_empty() {
        let watched = WatchedBitField::construct_and_resize("undefined:1:eJwDAAAAAAE=", vec![]);
        assert_eq!(
            watched,
            Ok(WatchedBitField {
                bitfield: BitField8::new(0),
                video_ids: vec![]
            })
        );
    }
}