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
use expresso::expression::Expression;
use expresso::machine::Machine;
use oml_audio::SoundBank;
use regex::Regex;
use tracing::*;

use crate::file_cache::FileCache;

#[derive(Debug)]
pub struct Context {
	time_step:         f64,
	soundbank:         SoundBank,
	machine:           Machine,
	selected_variable: String,
	file_cache:        std::sync::Arc<std::sync::Mutex<FileCache>>,
}

impl Context {
	pub fn new() -> Self {
		Self {
			time_step:         1.0 / 60.0,
			soundbank:         SoundBank::new(),
			machine:           Machine::new(),
			selected_variable: String::new(),
			file_cache:        std::sync::Arc::new(std::sync::Mutex::new(FileCache::new())),
		}
	}

	pub fn file_cache(&mut self) -> &mut std::sync::Arc<std::sync::Mutex<FileCache>> {
		&mut self.file_cache
	}

	pub fn set_file_cache(&mut self, file_cache: std::sync::Arc<std::sync::Mutex<FileCache>>) {
		self.file_cache = file_cache;
	}

	pub fn play_sound(&mut self, id: &str) {
		debug!("play_sound: {:?}", id);
		self.soundbank.enable_debug();
		self.soundbank.play(id);
	}

	pub fn get_soundbank_mut(&mut self) -> &mut SoundBank {
		&mut self.soundbank
	}
	pub fn get_mut_machine(&mut self) -> &mut Machine {
		&mut self.machine
	}

	pub fn set_time_step(&mut self, time_step: f64) {
		self.time_step = time_step;
	}

	pub fn time_step(&self) -> f64 {
		self.time_step
	}

	pub fn selected_variable(&self) -> &str {
		&self.selected_variable
	}

	pub fn select_next_variable(&mut self, prefix: Option<&str>) -> &str {
		let vs = self.machine.get_variable_storage();

		let mut names: Vec<&str> = if let Some(prefix) = prefix {
			vs.names()
				.filter_map(|v| {
					if v.starts_with(prefix) {
						Some(v.as_ref())
					} else {
						None
					}
				})
				.collect()
		} else {
			vs.names().map(|v| v.as_ref()).collect()
		};

		match names.len() {
			0 => {},
			1 => {
				self.selected_variable = names[0].to_string();
			},
			_ => {
				names.sort();
				match names.binary_search(&self.selected_variable.as_ref()) {
					Ok(i) => {
						//						dbg!(&i);
						let i = (i + 1).wrapping_rem(names.len());
						//						println!("Found using [{}]", i);
						self.selected_variable = names[i].to_string();
					},
					Err(_) => {
						//						println!("Not found using [0]" );
						self.selected_variable = names[0].to_string();
					},
				};
			},
		};

		debug!("selected variable: {:?}", &self.selected_variable);
		&self.selected_variable
	}

	pub fn set_string(&mut self, name: &str, value: &str) {
		//		dbg!(&name, &value);
		self.machine.get_mut_variable_storage().set(
			name,
			expresso::variables::Variable::String(value.to_string()),
		);
	}

	pub fn set_f32(&mut self, name: &str, value: f32) {
		//		dbg!(&name, &value);
		self.machine
			.get_mut_variable_storage()
			.set(name, expresso::variables::Variable::F32(value));
	}

	pub fn get_string(&self, name: &str) -> Option<&str> {
		match self.machine.get_variable_storage().get(name) {
			Some(expresso::variables::Variable::String(s)) => Some(s),
			o => todo!("{:?}", &o),
		}
	}

	pub fn get_f32(&self, name: &str) -> Option<f32> {
		match self.machine.get_variable_storage().get(name) {
			Some(expresso::variables::Variable::F32(f)) => Some(*f),
			None => None, // Why not???
			o => {
				//				todo!("{:?}", &o)
				println!("Error: Can not get as f32: {:?} using 0.0", &o);
				Some(0.0)
			},
		}
	}

	pub fn get_expanded_string(&self, name: &str) -> Option<&str> {
		match self.get_string(name) {
			None => None,
			Some(s) => Some(s),
		}
	}

	// :TODO: maybe return str instead String to avoid potentially unneeded copies
	pub fn expand_string_or(&mut self, s: &str, default: &str) -> String {
		let re = Regex::new(r"^\$\{([^:]+)(:(.+))?\}$").unwrap(); // :TODO: we could use non greedy matching here
		let re2 = Regex::new(r"^\$\[([^:]+)(:(.+))?\]$").unwrap(); // :TODO: we could use non greedy matching here
		if let Some(caps) = re.captures(&s) {
			//			dbg!(&caps);
			let name = &caps[1];
			//			dbg!(&name);
			if let Some(value) = self.get_string(&name) {
				value.to_string()
			} else {
				//				dbg!("Variable not found", &name);
				//				dbg!("Returning default for", &s, &default);
				match caps.get(3) {
					Some(c) => {
						self.set_string(&name, c.as_str());
						c.as_str().to_string()
					},
					None => default.to_string(),
				}
			}
		} else if let Some(caps) = re2.captures(&s) {
			let mut expression = Expression::new();
			match expression.from_str(&caps[1]) {
				// :TODO: error reporting
				_ => {},
			};
			//			println!("{}", expression);
			let mut r = expression.run(&mut self.machine);
			match r.pop() {
				Some(expresso::variables::Variable::I32(i)) => {
					format!("{}", i)
				},
				Some(expresso::variables::Variable::F32(f)) => {
					format!("{}", f)
				},
				None => "No result".to_string(),
				r => todo!("Result is not printable {:?}", r),
			}

		//			format!("Expression: {}", &caps[ 1 ])
		} else {
			s.to_string()
		}
	}

	pub fn expand_u32_or(&mut self, s: &str, default: u32) -> u32 {
		let s = self.expand_string_or(s, "");
		if let Ok(u) = s.parse::<u32>() {
			u
		} else {
			default
		}
	}
	/*
		pub fn 	expand_var_to_u32_or( &mut self, v: &Variable, default: u32 ) -> u32 {
			match v.original() {
				Original::U32( u ) => {
					u
				},
				Original::STRING( s ) => {
					let s = self.expand_string_or( &s, "" );
					if let Ok( u ) = s.parse::<u32>() {
						u
					} else if let Ok( f ) = s.parse::<f32>() {
						f as u32
					} else {
						default
					}
				},
				_ => default,
			}
		}

		pub fn 	expand_var_to_f32_or( &mut self, v: &Variable, default: f32 ) -> f32 {
			match v.original() {
				Original::F32( u ) => {
					u
				},
				Original::U32( u ) => {
					u as f32
				},
				Original::STRING( s ) => {
					let s = self.expand_string_or( &s, "" );
					if let Ok( u ) = s.parse::<u32>() {
						u as f32
					} else if let Ok( f ) = s.parse::<f32>() {
						f
					} else {
						default
					}
				},
				_ => default,
			}
		}
	*/
}

#[cfg(test)]
mod tests {
	use crate::context::Context;

	#[test]
	fn select_next_variable_works_without_variables() {
		let mut context = Context::new();
		context.select_next_variable(None);
		context.select_next_variable(Some("not_found_anyway"));
	}

	#[test]
	fn select_next_variable_without_prefix() {
		let mut context = Context::new();
		context.set_string("a", "one");
		context.set_string("b", "two");
		assert_eq!("", context.selected_variable());
		context.select_next_variable(None);
		assert_eq!("a", context.selected_variable());
		context.select_next_variable(None);
		assert_eq!("b", context.selected_variable());
		context.select_next_variable(None);
		assert_eq!("a", context.selected_variable());
		context.select_next_variable(None);
		assert_eq!("b", context.selected_variable());
		context.select_next_variable(None);
		assert_eq!("a", context.selected_variable());
	}

	#[test]
	fn select_next_variable_with_prefix() {
		let mut context = Context::new();
		context.set_string("a_one", "a one");
		context.set_string("a_two", "a two");
		context.set_string("b_one", "b one");
		context.set_string("b_two", "b two");
		context.set_string("c", "doesn't matter at all");
		context.set_string("d", "doesn't matter at all");
		context.set_string("e", "doesn't matter at all");
		context.set_string("f", "doesn't matter at all");
		context.set_string("g", "doesn't matter at all");
		context.set_string("h", "doesn't matter at all");
		//    	dbg!(&context);
		assert_eq!("", context.selected_variable());
		context.select_next_variable(Some("b"));
		assert_eq!("b_one", context.selected_variable());
		context.select_next_variable(Some("b"));
		assert_eq!("b_two", context.selected_variable());
		context.select_next_variable(Some("b"));
		assert_eq!("b_one", context.selected_variable());
		context.select_next_variable(Some("a"));
		assert_eq!("a_one", context.selected_variable());
		context.select_next_variable(Some("b"));
		assert_eq!("b_one", context.selected_variable());
		context.select_next_variable(Some("c"));
		assert_eq!("c", context.selected_variable());
	}
}