import json

from components.sud import Sud, SudState

# Console-only walkthrough of Sud.save()/load() (no plot - there's no
# numeric data here, just JSON plumbing). load()/save() are purely
# in-memory - Sud itself never reads or writes anything under sude/.

if __name__ == '__main__':
	sud = Sud()
	print(f"Starts out empty: '{sud.name}', {len(sud.schedule)} steps")

	with open("sude/sud_0010.json") as f:
		doc = json.load(f)
	assert sud.load(doc) is True
	print(f"Loaded '{sud.name}', {len(sud.schedule)} steps")

	saved = sud.save()
	assert saved['Name'] == sud.name
	print("save() returned the running document")

	sud.state = SudState.RAMPING
	assert sud.load(saved) is False
	print("load() refused while RAMPING")

	sud.state = SudState.IDLE
	saved['Name'] = 'Sud-Renamed'
	saved['steps'][0]['descr'] = 'Edited step'
	assert sud.load(saved) is True
	assert sud.name == 'Sud-Renamed'
	assert sud.schedule[0]['descr'] == 'Edited step'
	assert sud.state == SudState.IDLE
	print("load() applied the edit, purely in memory")

	with open("sude/sud_0010.json") as f:
		on_disk = json.load(f)
	assert on_disk['Name'] != 'Sud-Renamed'
	print("sude/sud_0010.json on disk is untouched")

	assert sud.load({'Name': 'broken'}) is False
	assert sud.name == 'Sud-Renamed'
	print("load() rejected malformed data, left schedule untouched")

	print("All sud save/load checks passed")
