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
use crate::path_reference::CanonicalPathReference;
use crate::value::PropertyValue;
use crate::{
    AssetId, AssetLocation, AssetName, DataSet, DataSetAssetInfo, DataSetResult, HashSet,
    NullOverride, OrderedSet, SchemaSet,
};
use uuid::Uuid;

#[derive(Debug)]
pub struct DynamicArrayEntryDelta {
    key: String,
    // was previous add/remove, but order is important and this didn't maintain order
    entries: OrderedSet<Uuid>,
}

#[derive(Default, Debug)]
pub struct AssetDiff {
    set_name: Option<AssetName>,
    set_location: Option<AssetLocation>,
    set_prototype: Option<Option<AssetId>>,
    set_properties: Vec<(String, PropertyValue)>,
    remove_properties: Vec<String>,
    set_null_overrides: Vec<(String, NullOverride)>,
    remove_null_overrides: Vec<String>,
    add_properties_in_replace_mode: Vec<String>,
    remove_properties_in_replace_mode: Vec<String>,
    dynamic_array_entry_deltas: Vec<DynamicArrayEntryDelta>,
    set_canonical_path_references: Vec<(CanonicalPathReference, AssetId)>,
    remove_canonical_path_references: Vec<CanonicalPathReference>,
}

impl AssetDiff {
    pub fn has_changes(&self) -> bool {
        self.set_name.is_some()
            || self.set_location.is_some()
            || self.set_prototype.is_some()
            || !self.set_properties.is_empty()
            || !self.remove_properties.is_empty()
            || !self.set_null_overrides.is_empty()
            || !self.remove_null_overrides.is_empty()
            || !self.add_properties_in_replace_mode.is_empty()
            || !self.remove_properties_in_replace_mode.is_empty()
            || !self.dynamic_array_entry_deltas.is_empty()
            || !self.set_canonical_path_references.is_empty()
            || !self.remove_canonical_path_references.is_empty()
    }

    pub fn apply(
        &self,
        asset: &mut DataSetAssetInfo,
    ) {
        if let Some(set_name) = &self.set_name {
            asset.asset_name = set_name.clone();
        }

        if let Some(set_location) = &self.set_location {
            asset.asset_location = set_location.clone();
        }

        if let Some(set_prototype) = self.set_prototype {
            asset.prototype = set_prototype;
        }

        for (k, v) in &self.set_properties {
            asset.properties.insert(k.clone(), v.as_value());
        }

        for k in &self.remove_properties {
            asset.properties.remove(k);
        }

        for (k, v) in &self.set_null_overrides {
            asset.property_null_overrides.insert(k.clone(), *v);
        }

        for k in &self.remove_null_overrides {
            asset.property_null_overrides.remove(k);
        }

        for k in &self.add_properties_in_replace_mode {
            asset.properties_in_replace_mode.insert(k.clone());
        }

        for k in &self.remove_properties_in_replace_mode {
            asset.properties_in_replace_mode.remove(k);
        }

        for delta in &self.dynamic_array_entry_deltas {
            if delta.entries.is_empty() {
                // No entries, just remove the key from the dynamic_collection_entries entirely
                asset.dynamic_collection_entries.remove(&delta.key);
            } else {
                // We have entries, get or create the key, then stomp the value
                *asset
                    .dynamic_collection_entries
                    .entry(delta.key.clone())
                    .or_default() = delta.entries.clone();
            }
        }

        for (k, v) in &self.set_canonical_path_references {
            asset
                .build_info
                .path_reference_overrides
                .insert(k.clone(), *v);
        }

        for k in &self.remove_canonical_path_references {
            asset.build_info.path_reference_overrides.remove(k);
        }
    }
}

pub struct AssetDiffSet {
    pub apply_diff: AssetDiff,
    pub revert_diff: AssetDiff,
}

impl AssetDiffSet {
    pub fn has_changes(&self) -> bool {
        // assume if apply has no changes, neither does revert
        self.apply_diff.has_changes()
    }

    pub fn diff_assets(
        before_data_set: &DataSet,
        before_asset_id: AssetId,
        after_data_set: &DataSet,
        after_asset_id: AssetId,
        modified_locations: &mut HashSet<AssetLocation>,
    ) -> Self {
        let before_obj = before_data_set.assets().get(&before_asset_id).unwrap();
        let after_obj = after_data_set.assets().get(&after_asset_id).unwrap();

        assert_eq!(
            before_obj.schema().fingerprint(),
            after_obj.schema().fingerprint()
        );

        let mut apply_diff = AssetDiff::default();
        let mut revert_diff = AssetDiff::default();

        if before_obj.asset_name != after_obj.asset_name {
            apply_diff.set_name = Some(after_obj.asset_name.clone());
            revert_diff.set_name = Some(before_obj.asset_name.clone());
        }

        if before_obj.asset_location != after_obj.asset_location {
            apply_diff.set_location = Some(after_obj.asset_location.clone());
            revert_diff.set_location = Some(before_obj.asset_location.clone());
        }

        //
        // Prototype
        //
        if before_obj.prototype != after_obj.prototype {
            apply_diff.set_prototype = Some(after_obj.prototype);
            revert_diff.set_prototype = Some(before_obj.prototype);
        }

        //
        // Properties
        //
        for (key, before_value) in &before_obj.properties {
            if let Some(after_value) = after_obj.properties.get(key) {
                if !PropertyValue::are_matching_property_values(before_value, after_value) {
                    // Value was changed
                    apply_diff
                        .set_properties
                        .push((key.clone(), after_value.as_property_value().unwrap()));
                    revert_diff
                        .set_properties
                        .push((key.clone(), before_value.as_property_value().unwrap()));
                } else {
                    // No change
                }
            } else {
                // Property was removed
                apply_diff.remove_properties.push(key.clone());
                revert_diff
                    .set_properties
                    .push((key.clone(), before_value.as_property_value().unwrap()));
            }
        }

        for (key, after_value) in &after_obj.properties {
            if !before_obj.properties.contains_key(key) {
                // Property was added
                apply_diff
                    .set_properties
                    .push((key.clone(), after_value.as_property_value().unwrap()));
                revert_diff.remove_properties.push(key.clone());
            }
        }

        //
        // Null Overrides
        //
        for (key, &before_value) in &before_obj.property_null_overrides {
            if let Some(after_value) = after_obj.property_null_overrides.get(key).copied() {
                if before_value != after_value {
                    // Value was changed
                    apply_diff
                        .set_null_overrides
                        .push((key.clone(), after_value));
                    revert_diff
                        .set_null_overrides
                        .push((key.clone(), before_value));
                } else {
                    // No change
                }
            } else {
                // Property was removed
                apply_diff.remove_null_overrides.push(key.clone());
                revert_diff
                    .set_null_overrides
                    .push((key.clone(), before_value));
            }
        }

        for (key, &after_value) in &after_obj.property_null_overrides {
            if !before_obj.property_null_overrides.contains_key(key) {
                // Property was added
                apply_diff
                    .set_null_overrides
                    .push((key.clone(), after_value));
                revert_diff.remove_null_overrides.push(key.clone());
            }
        }

        //
        // Properties in replace mode
        //
        for replace_mode_property in &before_obj.properties_in_replace_mode {
            if !after_obj
                .properties_in_replace_mode
                .contains(replace_mode_property)
            {
                // Replace mode disabled
                apply_diff
                    .remove_properties_in_replace_mode
                    .push(replace_mode_property.clone());
                revert_diff
                    .add_properties_in_replace_mode
                    .push(replace_mode_property.clone());
            }
        }

        for replace_mode_property in &after_obj.properties_in_replace_mode {
            if !before_obj
                .properties_in_replace_mode
                .contains(replace_mode_property)
            {
                // Replace mode enabled
                apply_diff
                    .add_properties_in_replace_mode
                    .push(replace_mode_property.clone());
                revert_diff
                    .remove_properties_in_replace_mode
                    .push(replace_mode_property.clone());
            }
        }

        //
        // Dynamic Array Entries. THe "key" in this context is a property path.
        // We do a heavyweight clone so that we can maintain ordering of elements
        //
        let mut all_dynamic_entry_keys = HashSet::default();
        for key in before_obj.dynamic_collection_entries().keys() {
            all_dynamic_entry_keys.insert(key);
        }
        for key in after_obj.dynamic_collection_entries().keys() {
            all_dynamic_entry_keys.insert(key);
        }
        for key in all_dynamic_entry_keys {
            let empty_set = OrderedSet::<Uuid>::default();
            let old = before_obj
                .dynamic_collection_entries
                .get(key)
                .unwrap_or(&empty_set);
            let new = after_obj
                .dynamic_collection_entries
                .get(key)
                .unwrap_or(&empty_set);

            if old != new {
                apply_diff
                    .dynamic_array_entry_deltas
                    .push(DynamicArrayEntryDelta {
                        key: key.clone(),
                        entries: new.clone(),
                    });
                revert_diff
                    .dynamic_array_entry_deltas
                    .push(DynamicArrayEntryDelta {
                        key: key.clone(),
                        entries: old.clone(),
                    });
            }
        }

        //
        // File References
        //
        for (key, &before_value) in &before_obj.build_info.path_reference_overrides {
            if let Some(&after_value) = after_obj.build_info.path_reference_overrides.get(key) {
                if before_value != after_value {
                    // Value was changed
                    apply_diff
                        .set_canonical_path_references
                        .push((key.clone(), after_value));
                    revert_diff
                        .set_canonical_path_references
                        .push((key.clone(), before_value));
                } else {
                    // No change
                }
            } else {
                // Property was removed
                apply_diff
                    .remove_canonical_path_references
                    .push(key.clone());
                revert_diff
                    .set_canonical_path_references
                    .push((key.clone(), before_value));
            }
        }

        for (key, &after_value) in &after_obj.build_info.path_reference_overrides {
            if !before_obj
                .build_info
                .path_reference_overrides
                .contains_key(key)
            {
                // Property was added
                apply_diff
                    .set_canonical_path_references
                    .push((key.clone(), after_value));
                revert_diff
                    .remove_canonical_path_references
                    .push(key.clone());
            }
        }

        // we only flag the location as modified if we make an edit
        // (if apply_diff doesn't have changes, before_diff doesn't either)
        if apply_diff.has_changes() {
            if !modified_locations.contains(&after_obj.asset_location) {
                modified_locations.insert(after_obj.asset_location.clone());
            }

            // Also save the old location so that in moves, the "from" location is marked as changed too
            if before_obj.asset_location != after_obj.asset_location {
                if !modified_locations.contains(&before_obj.asset_location) {
                    modified_locations.insert(before_obj.asset_location.clone());
                }
            }
        }

        AssetDiffSet {
            apply_diff,
            revert_diff,
        }
    }
}

#[derive(Default, Debug)]
pub struct DataSetDiff {
    creates: Vec<(AssetId, DataSetAssetInfo)>,
    deletes: Vec<AssetId>,
    changes: Vec<(AssetId, AssetDiff)>,
}

impl DataSetDiff {
    pub fn has_changes(&self) -> bool {
        !self.creates.is_empty() || !self.deletes.is_empty() || !self.changes.is_empty()
    }

    pub fn apply(
        &self,
        data_set: &mut DataSet,
        schema_set: &SchemaSet,
    ) -> DataSetResult<()> {
        for delete in &self.deletes {
            data_set.delete_asset(*delete)?;
        }

        for (id, create) in &self.creates {
            data_set.restore_asset(
                *id,
                create.asset_name.clone(),
                create.asset_location.clone(),
                create.import_info.clone(),
                create.build_info.clone(),
                schema_set,
                create.prototype,
                create.schema().fingerprint(),
                create.properties.clone(),
                create.property_null_overrides.clone(),
                create.properties_in_replace_mode.clone(),
                create.dynamic_collection_entries.clone(),
            )?;
        }

        for (asset_id, v) in &self.changes {
            if let Some(asset) = data_set.assets_mut().get_mut(asset_id) {
                v.apply(asset);
            }
        }

        Ok(())
    }

    pub fn get_modified_assets(
        &self,
        modified_assets: &mut HashSet<AssetId>,
    ) {
        for (id, _) in &self.creates {
            modified_assets.insert(*id);
        }

        for id in &self.deletes {
            modified_assets.insert(*id);
        }

        for (id, _) in &self.changes {
            modified_assets.insert(*id);
        }
    }
}

#[derive(Debug)]
pub struct DataSetDiffSet {
    pub apply_diff: DataSetDiff,
    pub revert_diff: DataSetDiff,
    pub modified_assets: HashSet<AssetId>,
    pub modified_locations: HashSet<AssetLocation>,
}

impl DataSetDiffSet {
    pub fn has_changes(&self) -> bool {
        // assume if apply has no changes, neither does revert
        self.apply_diff.has_changes()
    }

    pub fn diff_data_set(
        before: &DataSet,
        after: &DataSet,
        tracked_assets: &HashSet<AssetId>,
    ) -> Self {
        let mut apply_diff = DataSetDiff::default();
        let mut revert_diff = DataSetDiff::default();
        let mut modified_assets: HashSet<AssetId> = Default::default();
        let mut modified_locations: HashSet<AssetLocation> = Default::default();

        // Check for created assets
        for &asset_id in tracked_assets {
            let existed_before = before.assets().contains_key(&asset_id);
            let existed_after = after.assets().contains_key(&asset_id);
            if existed_before {
                if existed_after {
                    // Asset was modified
                    let diff = AssetDiffSet::diff_assets(
                        before,
                        asset_id,
                        &after,
                        asset_id,
                        &mut modified_locations,
                    );
                    if diff.has_changes() {
                        modified_assets.insert(asset_id);
                        apply_diff.changes.push((asset_id, diff.apply_diff));
                        revert_diff.changes.push((asset_id, diff.revert_diff));
                    }
                } else {
                    // Asset was deleted
                    let before_asset_info = before.assets().get(&asset_id).unwrap().clone();
                    modified_assets.insert(asset_id);
                    if !modified_locations.contains(&before_asset_info.asset_location) {
                        modified_locations.insert(before_asset_info.asset_location.clone());
                    }

                    // deleted
                    apply_diff.deletes.push(asset_id);
                    revert_diff
                        .creates
                        .push((asset_id, before_asset_info.clone()));
                }
            } else if existed_after {
                // Asset was created
                let after_asset_info = after.assets().get(&asset_id).unwrap();
                if !modified_locations.contains(&after_asset_info.asset_location) {
                    modified_locations.insert(after_asset_info.asset_location.clone());
                }

                // created
                apply_diff
                    .creates
                    .push((asset_id, after_asset_info.clone()));
                revert_diff.deletes.push(asset_id);
            }
        }

        DataSetDiffSet {
            apply_diff,
            revert_diff,
            modified_locations,
            modified_assets,
        }
    }
}