.{Vec2} := @use("../../../../libraries/stn/src/lib.hb").math;
.{random} := @use("../../../../libraries/stn/src/lib.hb")
render := @use("../../../../libraries/render/src/lib.hb")

/* expected result:
   bouncing gradient square inside coloured bouncing box inside black screen */

example := fn(): void {
	screen := render.init(true)

	image := render.new_surface(screen.width / 3, screen.height / 3)
	vel := Vec2(int).(-1, -1)
	pos := Vec2(uint).(100, 100)
	side := image.width / 8
	vel_inner := Vec2(int).(1, 1)
	pos_inner := Vec2(uint).((image.width - side) / 2, (image.height - side) / 2)
	color := random.any(render.Color)
	target_color := random.any(render.Color)
	loop {
		render.clear(screen, render.black)
		render.put_filled_rect(image, pos_inner, .(side, side), color)
		render.put_rect(image, pos_inner, .(side, side), render.black)
		render.put_rect(image, .(0, 0), .(image.width - 1, image.height - 1), color)

		render.put_surface(screen, image, pos, false)
		render.put_rect(image, pos_inner, .(side, side), color)
		render.sync(screen)

		if pos_inner.x == 0 | pos_inner.x == image.width - side {
			vel_inner.x = -vel_inner.x
			target_color = random.any(render.Color)
		}
		if pos_inner.y == 0 | pos_inner.y == image.height - side {
			vel_inner.y = -vel_inner.y
			target_color = random.any(render.Color)
		}

		if pos.x == 0 | pos.x == screen.width - image.width {
			vel.x = -vel.x
		}
		if pos.y == 0 | pos.y == screen.height - image.height {
			vel.y = -vel.y
		}

		color += .(
			@bitcast(color.b < target_color.b) - @bitcast(color.b > target_color.b),
			@bitcast(color.g < target_color.g) - @bitcast(color.g > target_color.g),
			@bitcast(color.r < target_color.r) - @bitcast(color.r > target_color.r),
			0,
		)
		pos += @bitcast(vel)
		pos_inner += @bitcast(vel_inner)
	}
	return
}