Text GetId() {
	return "Nsgr_TwitchPlaysCheckpoint";
}

CUILayer::EUILayerType GetType() {
	return CUILayer::EUILayerType::Normal;
}

Text GetManialink() {
	return """<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
	<manialink version="3" name="{{{GetId()}}}">
	<frame pos="0 38" z-index="-2" id="frame-checkpoint" scale="0">
		<quad z-index="0" size="20 8" valign="center" bgcolor="000000" opacity="0.6" id="quad-background" halign="center" />
		<label z-index="1" valign="center2" id="label-time"  text="01:02.023" textfont="RajdhaniMono" textsize="4" textcolor="FFF" halign="center"/>
	</frame>
	<script><!--
		#Include "TextLib" as TL

		#Const C_SoundPath "file://Media/Manialinks/Nadeo/Trackmania/Ingame/Sound/"
		#Const C_Waypoint_Ahead "-1"
		#Const C_Waypoint_Neutral "0"
		#Const C_Waypoint_Late "1"

		CSmPlayer GetOwner() {
			if (GUIPlayer != Null) return GUIPlayer;
			return InputPlayer;
		}

		main() {
			// Init
			declare CMlFrame Frame_Checkpoint;
			declare CMlLabel Label_Time;
			declare CMlQuad Quad_Background;

			declare Integer CheckpointsCount;
			declare Boolean IsRewind;
			declare Integer CheckpointTime;
			declare Integer RaceStartTime;
			declare Integer DisplayEndTime;
			declare Boolean PrevVisible;
			declare Boolean IsVisible;

			declare CAudioSource Sound_Checkpoint;

			// Start
			Frame_Checkpoint <=> (Page.GetFirstChild("frame-checkpoint") as CMlFrame);
			Label_Time <=> (Frame_Checkpoint.GetFirstChild("label-time") as CMlLabel);
			Quad_Background <=>(Frame_Checkpoint.GetFirstChild("quad-background") as CMlQuad);
			
			if (Sound_Checkpoint == Null) Sound_Checkpoint = Audio.CreateSound(C_SoundPath^"RaceCheckPoint_Experimental.wav", 0.0, False, False, False);

			CheckpointsCount = -123;
			CheckpointTime = -123;
			RaceStartTime = -123;
			DisplayEndTime = -123;
			IsRewind = False;
			IsVisible = False;

			while (True) {
				yield;

				//Loop

				declare Owner <=> GetOwner();

				if (Owner != Null) {
					if (CheckpointsCount != Owner.RaceWaypointTimes.count) {
						IsRewind = CheckpointsCount > Owner.RaceWaypointTimes.count;
						CheckpointsCount = Owner.RaceWaypointTimes.count;
						CheckpointTime = -1;
					}
	
					if (Owner.RaceWaypointTimes.count > 0) {
						if (CheckpointTime < 0 && Owner.RaceWaypointTimes[Owner.RaceWaypointTimes.count - 1] >= 0) {
							CheckpointTime = Owner.RaceWaypointTimes[Owner.RaceWaypointTimes.count - 1];
	
							if (!IsRewind) {
								Label_Time.Value = TL::TimeToText(CheckpointTime, True, True);
								Quad_Background.Size.X = Label_Time.ComputeWidth(Label_Time.Value) + 2.;
								Sound_Checkpoint.Stop();
								Sound_Checkpoint.Play();
								DisplayEndTime = Now + 3000;
								IsVisible = True;
							}
						}
					}
				}
				
				if (Now >= DisplayEndTime) {
					IsVisible = False;
					DisplayEndTime = -1;
				}

				if (PrevVisible != IsVisible) {
					AnimMgr.Flush(Frame_Checkpoint);
					if (IsVisible) {
						AnimMgr.Add(Frame_Checkpoint, "<anim scale=\"1\" />", 80, CAnimManager::EAnimManagerEasing::CircOut);
					} else {
						AnimMgr.Add(Frame_Checkpoint, "<anim scale=\"0\" />", 80, CAnimManager::EAnimManagerEasing::CircIn);
					}
				}

				PrevVisible = IsVisible;
			}
		}
	--></script>
	</manialink>
	""";
}