package core

dispose_character :: proc(c: Character) {
	delete(c.character_sheet_urls)
}

dispose_character_owned :: proc(c: Character) {
	delete(c.id)
	delete(c.name)
	delete(c.description)
	delete(c.reference_image_url)
	delete(c.first_appearance_panel)
	delete(c.character_sheet_urls)
}

dispose_script :: proc(script: ^Comic_Script) {
	for c in script.characters {
		dispose_character(c)
	}
	for p in script.pages {
		for pan in p.panels {
			delete(pan.characters_present)
			delete(pan.dialogue)
			delete(pan.sound_effects)
		}
		delete(p.panels)
	}
	delete(script.characters)
	delete(script.pages)
}

dispose_script_owned :: proc(script: ^Comic_Script) {
	delete(script.title)
	delete(script.synopsis)

	for c in script.characters {
		dispose_character_owned(c)
	}
	for p in script.pages {
		for pan in p.panels {
			delete(pan.panel_id)
			delete(pan.description)
			delete(pan.caption)
			for id in pan.characters_present { delete(id) }
			delete(pan.characters_present)
			for d in pan.dialogue {
				delete(d.speaker_id)
				delete(d.text)
			}
			delete(pan.dialogue)
			for s in pan.sound_effects { delete(s) }
			delete(pan.sound_effects)
		}
		delete(p.panels)
	}
	delete(script.characters)
	delete(script.pages)
}

dispose_page_layouts :: proc(layouts: ^[]Page_Layout) {
	for l in layouts^ {
		delete(l.panels)
	}
	delete(layouts^)
}

dispose_speech_bubbles :: proc(bubbles: ^map[string][]Speech_Bubble) {
	for _, s in bubbles^ {
		delete(s)
	}
	delete(bubbles^)
}

dispose_state :: proc(state: ^Comic_State) {
	dispose_script(&state.script)

	for c in state.characters {
		dispose_character(c)
	}
	if raw_data(state.characters) != raw_data(state.script.characters) {
		delete(state.characters)
	}

	delete(state.panel_images)
	delete(state.panel_errors)
	dispose_page_layouts(&state.page_layouts)
	dispose_speech_bubbles(&state.speech_bubbles)
	delete(state.workflow.completed_steps)

	state.panel_images = nil
	state.panel_errors = nil
	state.speech_bubbles = nil
	state.page_layouts = nil
	state.characters = nil
	state.workflow.completed_steps = nil
}

dispose_state_owned :: proc(state: ^Comic_State) {
	delete(state.project.project_id)
	delete(state.project.project_name)
	delete(state.project.created_at_iso)
	delete(state.project.last_modified_iso)
	delete(state.story_idea)
	delete(state.story_genre)
	delete(state.target_audience)
	delete(state.art_style)
	delete(state.workflow.error_message)

	dispose_script_owned(&state.script)

	for c in state.characters {
		dispose_character_owned(c)
	}
	if raw_data(state.characters) != raw_data(state.script.characters) {
		delete(state.characters)
	}

	for _, img in state.panel_images {
		delete(img.url)
		delete(img.prompt)
	}
	delete(state.panel_images)

	for _, msg in state.panel_errors {
		delete(msg)
	}
	delete(state.panel_errors)

	dispose_page_layouts(&state.page_layouts)
	dispose_speech_bubbles(&state.speech_bubbles)
	delete(state.workflow.completed_steps)

	state.panel_images = nil
	state.panel_errors = nil
	state.speech_bubbles = nil
	state.page_layouts = nil
	state.characters = nil
	state.workflow.completed_steps = nil
}
