module carousel

import freeflowuniverse.crystallib.core.playbook {PlayBook}

@[params]
pub struct PlayParams{
pub mut:
	args map[string]string
	defaults bool
}

//will have all the results, key is in this case intro1.${name}
//this allows us to feed e.g. markdown to all renderers and then we get the data filled in what is found on page
pub fn play(mut plbook PlayBook, params PlayParams) !map[string]Carousel{
	mut result:=map[string]Carousel{}

	if plbook.actions.len == 0 && params.defaults {
		plbook = playbook.new(text: example_heroscript)!
	}

	actions0 := plbook.find(filter: 'intro1.new')!
	for action0 in actions0{
		mut p0 := action0.params
		name := p0.get_default('name','default')!
		key:="intro1.${name}"
		carousel := play_carousel(mut plbook, name)!
		println('carousel ${name}\n${carousel}')
		result[key]=carousel
	}

    return result
}

pub fn play_carousel(mut plbook PlayBook, name string) !Carousel {
	mut carousel := Carousel{}
	
	// Process add_item actions
	actions := plbook.find(filter: 'intro1.${name}_add')!
	for action in actions {
		mut p := action.params

		item := Item{
			title: p.get('title')!
			subtitle: p.get('subtitle')!
			description: p.get('description')!.replace("\n","<br>")
			background_image: p.get('background_image')!
			button_primary: Button{
				text: p.get('button_primary_text')!
				url: p.get('button_primary_url')!
			}
			button_secondary: Button{
				text: p.get('button_secondary_text')!
				url: p.get('button_secondary_url')!
			}
		}

		carousel.items << item
	}
	return carousel
}