module intro

import freeflowuniverse.crystallib.core.playbook

pub struct Item {
pub mut:
    title            string
    subtitle         string
    description      string
    background_image string
    button_primary   Button
    button_secondary Button
}

pub struct Button {
pub mut:
    text string
    url  string
}

pub struct Data {
pub mut:
    items []Item
}


const example_heroscript="

	!!intro1.new name:'myintro' 

	!!intro1.myintro_add
		title: 'Sepia'
		description: '
					Made for photographers, photo studios, design agencies.
					Create your own unique and beautiful photography website!
				'
		subtitle: 'Photography Portfolio Theme'
		background_image: 'assets/img/intro/intro-10.jpg'
		button_primary_text: 'Buy It Now!'
		button_primary_url: 'https://example.com/learn-more'
		button_secondary_text: 'Discover More'
		button_secondary_url: 'albums-grid-fluid-2.html'

	!!intro1.myintro_add
		title: 'Sepia 2'
		description: 'Made for photographers 2, photo studios, design agencies.<br>Create your own unique and beautiful photography website!'
		subtitle: 'Photography Portfolio Theme'
		background_image: 'assets/img/intro/intro-10.jpg'
		button_primary_text: 'Buy It Now!'
		button_primary_url: 'https://example.com/learn-more'
		button_secondary_text: 'Discover More'
		button_secondary_url: 'albums-grid-fluid-2.html'
"

@[params]
pub struct RenderArgs{
pub mut:
	text string
	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 render(args_ RenderArgs) !map[string]string{
	mut args:=args_

	mut result:=map[string]string{}

	if args.text =="" && args.defaults{
		args.text = example_heroscript
	}

 	mut plbook := playbook.new(text: args.text)!
    mut data := Data{}

	actions0 := plbook.find(filter: 'intro1.new')!
	for action0 in actions0{
		mut p0 := action0.params
		name := p0.get_default('name','default')!
		key:="intro1.${name}"
		
		// 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')!
				}
			}

			data.items << item
		}

		result[key]=$tmpl("templates/main.html")

	}

    return result	


}