// -----------------------------------------------------------
// slide5.c - the assembly confession
//   the body is typed in live, one character per frame, with a
//   white block cursor; the closing line appears when the
//   typing is done and pulses
// -----------------------------------------------------------
#include "defs.h"
#include "common.h"
#include "slides.h"

// ---- data ----

#define LINE_LEN 34                  // all body lines are exactly 34 chars
#define BODY_COL 3                   // justified: both edges at columns 3 and 36

// body lines are justified: extra spaces pad each line to LINE_LEN
static const char * const t_txt[5] = {
	"we speak c/c++, lua, python, java,",
	"go,   ruby,  js,  elixir,  pascal.",
	"but  6502  assembly?  not  a word.",
	"so   this  slideshow  was  written",
	"with  a  little  help  from an ai."
};

static const char t_row[5]   = { 7, 9, 12, 15, 17 };
static const char t_color[5] = {
	VCOL_LT_GREY, VCOL_LT_GREY, VCOL_WHITE, VCOL_LT_GREY, VCOL_LT_GREY
};

// ---- variables ----
static char t_line;                  // body line being typed (0-4)
static char t_chr;                   // next character within the line
static char t_done;                  // 1 = typing finished, closing line pulses

void s5_init(void)
{
	clear_screen();
	print_text(3, 11, VCOL_WHITE, "a quick confession");
	t_line = 0;                      // the body starts empty and gets typed in
	t_chr  = 0;
	t_done = 0;
}

void s5_update(void)
{
	while (vic.raster != RAS_BOTTOM)
		;

	framectr++;

	if (t_done)
	{
		// pulse on the "(the ai says hi.)" line
		char c = pulsetbl[(framectr >> 3) & 7];
		for (char x = 0; x < 17; x++)
			COLRAM[21 * 40 + 11 + x] = c;
		return;
	}

	// ---- type one character per frame ----
	unsigned o = rowoff[t_row[t_line]] + BODY_COL;
	char *sp = SCREEN + o;
	char *cp = COLRAM + o;

	char y = t_chr;
	sp[y] = t_txt[t_line][y];        // the character lands where the cursor was
	cp[y] = t_color[t_line];
	y++;
	if (y == LINE_LEN)
	{
		t_chr = 0;
		t_line++;
		if (t_line == 5)
		{
			// typing done: the closing line appears and starts pulsing
			print_text(21, 11, VCOL_CYAN, "(the ai says hi.)");
			t_done = 1;
		}
	}
	else
	{
		t_chr = y;
		sp[y] = 0xa0;                // block cursor at the next cell
		cp[y] = VCOL_WHITE;
	}
}
