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
use std::{
    cell::RefCell,
    collections::{HashMap, HashSet},
    rc::Rc,
};

use serde_json::Map;

use crate::{
    callstack::CallStack,
    json_read, json_write,
    list_definitions_origin::ListDefinitionsOrigin,
    state_patch::StatePatch,
    story_error::StoryError,
    value::Value,
    value_type::{ValueType, VariablePointerValue},
    variable_assigment::VariableAssignment,
};

#[derive(Clone)]
pub(crate) struct VariablesState {
    pub global_variables: HashMap<String, Rc<Value>>,
    pub default_global_variables: HashMap<String, Rc<Value>>,
    pub batch_observing_variable_changes: bool,
    pub callstack: Rc<RefCell<CallStack>>,
    pub changed_variables_for_batch_obs: Option<HashSet<String>>,
    pub patch: Option<StatePatch>,
    list_defs_origin: Rc<ListDefinitionsOrigin>,
}

impl VariablesState {
    pub fn new(
        callstack: Rc<RefCell<CallStack>>,
        list_defs_origin: Rc<ListDefinitionsOrigin>,
    ) -> VariablesState {
        VariablesState {
            global_variables: HashMap::new(),
            default_global_variables: HashMap::new(),
            batch_observing_variable_changes: false,
            callstack,
            changed_variables_for_batch_obs: None,
            patch: None,
            list_defs_origin,
        }
    }

    pub fn start_batch_observing_variable_changes(&mut self) {
        self.batch_observing_variable_changes = true;
        self.changed_variables_for_batch_obs = Some(HashSet::new());
    }

    pub fn stop_batch_observing_variable_changes(&mut self) -> Vec<(String, ValueType)> {
        self.batch_observing_variable_changes = false;

        let mut changed: Vec<(String, ValueType)> = Vec::with_capacity(0);

        // Finished observing variables in a batch - now send
        // notifications for changed variables all in one go.
        if let Some(changed_variables_for_batch_obs) = self.changed_variables_for_batch_obs.take() {
            for variable_name in changed_variables_for_batch_obs {
                let current_value = self.global_variables.get(&variable_name).unwrap();

                changed.push((variable_name, current_value.value.clone()));
            }
        }

        changed
    }

    pub fn snapshot_default_globals(&mut self) {
        for (k, v) in self.global_variables.iter() {
            self.default_global_variables.insert(k.clone(), v.clone());
        }
    }

    pub fn apply_patch(&mut self) {
        for (name, value) in self.patch.as_ref().unwrap().globals.iter() {
            self.global_variables.insert(name.clone(), value.clone());
        }

        if let Some(changed_variables) = &mut self.changed_variables_for_batch_obs {
            for name in self.patch.as_ref().unwrap().changed_variables.iter() {
                changed_variables.insert(name.clone());
            }
        }

        self.patch = None;
    }

    pub fn assign(
        &mut self,
        var_ass: &VariableAssignment,
        value: Rc<Value>,
    ) -> Result<(), StoryError> {
        let mut name = var_ass.variable_name.to_string();
        let mut context_index = -1;
        let mut set_global;

        // Are we assigning to a global variable?
        if var_ass.is_new_declaration {
            set_global = var_ass.is_global;
        } else {
            set_global = self.global_variable_exists_with_name(&name);
        }

        let mut value = value;
        // Constructing new variable pointer reference
        if var_ass.is_new_declaration {
            if let Some(var_pointer) = Value::get_variable_pointer_value(value.as_ref()) {
                value = self.resolve_variable_pointer(var_pointer);
            }
        } else {
            // Assign to an existing variable pointer
            // Then assign to the variable that the pointer is pointing to by name.
            // De-reference variable reference to point to
            loop {
                let existing_pointer = self.get_raw_variable_with_name(&name, context_index);

                match existing_pointer {
                    Some(existing_pointer) => {
                        match Value::get_variable_pointer_value(existing_pointer.as_ref()) {
                            Some(pv) => {
                                name = pv.variable_name.to_string();
                                context_index = pv.context_index;
                                set_global = context_index == 0;
                            }
                            None => break,
                        }
                    }
                    None => break,
                }
            }
        }

        if set_global {
            self.set_global(&name, value);
        } else {
            self.callstack.borrow_mut().set_temporary_variable(
                name,
                value,
                var_ass.is_new_declaration,
                context_index,
            )?;
        }

        Ok(())
    }

    pub fn global_variable_exists_with_name(&self, name: &str) -> bool {
        self.global_variables.contains_key(name) || self.default_global_variables.contains_key(name)
    }

    // Given a variable pointer with just the name of the target known, resolve
    // to a variable
    // pointer that more specifically points to the exact instance: whether it's
    // global,
    // or the exact position of a temporary on the callstack.
    fn resolve_variable_pointer(&self, var_pointer: &VariablePointerValue) -> Rc<Value> {
        let mut context_index = var_pointer.context_index;
        if context_index == -1 {
            context_index = self.get_context_index_of_variable_named(&var_pointer.variable_name);
        }

        let value_of_variable_pointed_to =
            self.get_raw_variable_with_name(&var_pointer.variable_name, context_index);
        // Extra layer of indirection:
        // When accessing a pointer to a pointer (e.g. when calling nested or
        // recursive functions that take a variable references, ensure we don't
        // create
        // a chain of indirection by just returning the final target.
        if let Some(value_of_variable_pointed_to) = value_of_variable_pointed_to {
            if Value::get_variable_pointer_value(value_of_variable_pointed_to.as_ref()).is_some() {
                return value_of_variable_pointed_to;
            }
        }

        Rc::new(Value::new_variable_pointer(
            &var_pointer.variable_name,
            context_index,
        ))
    }

    // returns true if the value changed and we should notify variable observers
    pub fn set(&mut self, variable_name: &str, value_type: ValueType) -> Result<bool, StoryError> {
        if !self.default_global_variables.contains_key(variable_name) {
            return Err(StoryError::BadArgument(format!(
                "Cannot assign to a variable {} that hasn't been declared in the story",
                variable_name
            )));
        }

        let val = Value::from_value_type(value_type);

        let notify = self.set_global(variable_name, Rc::new(val));

        Ok(notify)
    }

    pub fn get(&self, variable_name: &str) -> Option<ValueType> {
        if self.patch.is_some() {
            if let Some(var) = self.patch.as_ref().unwrap().get_global(variable_name) {
                return Some(var.value.clone());
            }
        }

        // Search main dictionary first.
        // If it's not found, it might be because the story content has changed,
        // and the original default value hasn't be instantiated.
        // Should really warn somehow, but it's difficult to see how...!
        if let Some(var_contents) = self.global_variables.get(variable_name) {
            return Some(var_contents.value.clone());
        } else if let Some(var_contents) = self.default_global_variables.get(variable_name) {
            return Some(var_contents.value.clone());
        }

        None
    }

    // Make copy of the variable pointer so we're not using the value direct
    // from
    // the runtime. Temporary must be local to the current scope.
    // 0 if named variable is global
    // 1+ if named variable is a temporary in a particular call stack element
    fn get_context_index_of_variable_named(&self, var_name: &str) -> i32 {
        if self.global_variable_exists_with_name(var_name) {
            return 0;
        }

        return self.callstack.borrow().get_current_element_index();
    }

    fn get_raw_variable_with_name(&self, name: &str, context_index: i32) -> Option<Rc<Value>> {
        // 0 context = global
        if context_index == 0 || context_index == -1 {
            if let Some(patch) = &self.patch {
                if let Some(global) = patch.get_global(name) {
                    return Some(global);
                }
            }

            if let Some(global) = self.global_variables.get(name) {
                return Some(global.clone());
            }

            // Getting variables can actually happen during globals set up since you can do
            // VAR x = A_LIST_ITEM
            // So _default_global_variables may be None.
            // We need to do this check though in case a new global is added, so we need to
            // revert to the default globals dictionary since an initial value hasn't yet
            // been set.

            if let Some(default_global) = self.default_global_variables.get(name) {
                return Some(default_global.clone());
            }

            if let Some(list_item_value) =
                self.list_defs_origin.find_single_item_list_with_name(name)
            {
                return Some(list_item_value.clone());
            }
        }

        // Temporary
        let var_value = self
            .callstack
            .borrow()
            .get_temporary_variable_with_name(name, context_index);

        var_value
    }

    // Returns true if global var has changed and we need to notify observers
    fn set_global(&mut self, name: &str, value: Rc<Value>) -> bool {
        let mut old_value: Option<Rc<Value>> = None;

        if let Some(patch) = &self.patch {
            old_value = patch.get_global(name);
        }

        if old_value.is_none() {
            old_value = self.global_variables.get(name).cloned();
        }

        if let Some(old_value) = &old_value {
            Value::retain_list_origins_for_assignment(old_value.as_ref(), value.as_ref());
        }

        if let Some(patch) = &mut self.patch {
            patch.set_global(name, value.clone());
        } else {
            self.global_variables
                .insert(name.to_string(), value.clone());
        }

        if old_value.is_none() || !Rc::ptr_eq(old_value.as_ref().unwrap(), &value) {
            if self.batch_observing_variable_changes {
                if let Some(patch) = &mut self.patch {
                    patch.add_changed_variable(name);
                } else if let Some(changed_variables) = &mut self.changed_variables_for_batch_obs {
                    changed_variables.insert(name.to_string());
                }
            } else {
                return true;
            }
        }

        false
    }

    pub fn get_variable_with_name(&self, name: &str, context_index: i32) -> Option<Rc<Value>> {
        let var_value = self.get_raw_variable_with_name(name, context_index);
        // Get value from pointer?
        if let Some(vv) = var_value.clone() {
            if let Some(var_pointer) = Value::get_variable_pointer_value(vv.as_ref()) {
                return self.value_at_variable_pointer(var_pointer);
            }
        }

        var_value
    }

    fn value_at_variable_pointer(&self, pointer: &VariablePointerValue) -> Option<Rc<Value>> {
        self.get_variable_with_name(&pointer.variable_name, pointer.context_index)
    }

    pub fn set_callstack(&mut self, callstack: Rc<RefCell<CallStack>>) {
        self.callstack = callstack;
    }

    pub(crate) fn write_json(&self) -> Result<serde_json::Value, StoryError> {
        let mut jobj: Map<String, serde_json::Value> = Map::new();

        for (name, val) in self.global_variables.iter() {
            // Don't write out values that are the same as the default global values
            let default_val = self.default_global_variables.get(name);
            if let Some(default_val) = default_val {
                if self.val_equal(val, default_val) {
                    continue;
                }
            }

            jobj.insert(name.clone(), json_write::write_rtobject(val.clone())?);
        }

        Ok(serde_json::Value::Object(jobj))
    }

    fn val_equal(&self, val: &Value, default_val: &Value) -> bool {
        match &val.value {
            ValueType::Bool(val) => match default_val.value {
                ValueType::Bool(default_val) => *val == default_val,
                _ => false,
            },
            ValueType::Int(val) => match default_val.value {
                ValueType::Int(default_val) => *val == default_val,
                _ => false,
            },
            ValueType::Float(val) => match default_val.value {
                ValueType::Float(default_val) => *val == default_val,
                _ => false,
            },
            ValueType::List(val) => match &default_val.value {
                ValueType::List(default_val) => *val == *default_val,
                _ => false,
            },
            ValueType::String(val) => match &default_val.value {
                ValueType::String(default_val) => val.string.eq(&default_val.string),
                _ => false,
            },
            ValueType::DivertTarget(val) => match &default_val.value {
                ValueType::DivertTarget(default_val) => *val == *default_val,
                _ => false,
            },
            ValueType::VariablePointer(val) => match &default_val.value {
                ValueType::VariablePointer(default_val) => *val == *default_val,
                _ => false,
            },
        }
    }

    pub(crate) fn load_json(
        &mut self,
        jobj: &Map<String, serde_json::Value>,
    ) -> Result<(), StoryError> {
        self.global_variables.clear();

        for (k, v) in self.default_global_variables.iter() {
            let loaded_token = jobj.get(k);

            if let Some(loaded_token) = loaded_token {
                self.global_variables.insert(
                    k.to_string(),
                    json_read::jtoken_to_runtime_object(loaded_token, None)?
                        .into_any()
                        .downcast::<Value>()
                        .unwrap(),
                );
            } else {
                self.global_variables.insert(k.clone(), v.clone());
            }
        }

        Ok(())
    }
}