Lesson.cs
namespace PixelPusher;

public enum LessonStep
{
	Off,
	Paint,
	OpenStage,
	DropIn,
	Play,
	Done
}

public static class Lesson
{
	const string DoneFile = "lesson_done";

	public static bool Active { get; private set; }
	public static bool Finished { get; private set; }
	public static LessonStep Step { get; private set; } = LessonStep.Off;

	public static string Title => Step switch
	{
		LessonStep.Paint => "1 / 4  ·  Draw",
		LessonStep.OpenStage => "2 / 4  ·  Stage",
		LessonStep.DropIn => "3 / 4  ·  Drop in",
		LessonStep.Play => "4 / 4  ·  Play",
		LessonStep.Done => "That's the loop",
		_ => ""
	};

	public static string Body => Step switch
	{
		LessonStep.Paint => "Click the checkerboard and drag. That's PIXEL STUDIO — you just draw.",
		LessonStep.OpenStage => "Open STAGE. Stock the Cooler is already built for you.",
		LessonStep.DropIn => "DROP IN (or Tab) puts you in the stage. You can always come back to edit.",
		LessonStep.Play => "A / D move. Space jumps. Grab the three coins, then the pink exit.",
		LessonStep.Done => "Draw it. Stamp it. Play it. World and Chip can wait.",
		_ => ""
	};

	public static string Highlight => Step switch
	{
		LessonStep.Paint => "canvas",
		LessonStep.OpenStage => "tab-stage",
		LessonStep.DropIn => "drop-in",
		LessonStep.Play => "play",
		_ => ""
	};

	public static void Boot()
	{
		try
		{
			Finished = FileSystem.Data.FileExists( DoneFile );
		}
		catch
		{
			Finished = false;
		}
	}

	public static void MaybeStart()
	{
		if ( Finished || Active )
			return;
		Start();
	}

	public static void Start()
	{
		Active = true;
		Finished = false;
		Go( LessonStep.Paint );
		ChipVoices.Tick();
	}

	public static void Skip()
	{
		Active = false;
		Step = LessonStep.Off;
		MarkDone();
		Coach.Line = "Tour skipped. BUILD when you want, PLAY to run the cooler.";
		GameFlow.ShowToast( "tour skipped" );
		GameFlow.Bump();
	}

	public static void OnPaint()
	{
		if ( Step == LessonStep.Paint )
			Go( LessonStep.OpenStage );
	}

	public static void OnStudio( GameScreen screen )
	{
		if ( !Active )
			return;
		if ( screen == GameScreen.Stage && Step is LessonStep.Paint or LessonStep.OpenStage )
			Go( LessonStep.DropIn );
		else if ( screen == GameScreen.Pixel && Step == LessonStep.DropIn )
			Go( LessonStep.OpenStage );
	}

	public static void OnPlay()
	{
		if ( Step is LessonStep.DropIn or LessonStep.OpenStage or LessonStep.Paint )
			Go( LessonStep.Play );
	}

	public static void OnCleared()
	{
		if ( !Active )
			return;
		Go( LessonStep.Done );
		Active = false;
		MarkDone();
	}

	static void Go( LessonStep step )
	{
		Step = step;
		Coach.Line = Body;
		GameFlow.Bump();
	}

	static void MarkDone()
	{
		Finished = true;
		try
		{
			FileSystem.Data.WriteAllText( DoneFile, "1" );
		}
		catch
		{
			// local flag is enough for this session
		}
	}
}