using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;

namespace Scripts
{
	public class ShowFastestLap : IScript
	{
		private const int Top_NormalHTicker = 116;
		private const int Top_ExtendedHTicker = 162;
	
		public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
		{
			// Check some conditions unless the button was used to show the widget
			if (value?.ToString() != "Button")
			{					
				Console.WriteLine("Not button");
				
				// Don't show if condition flag is checked
				if (item.Theme.ConditionFlags.CheckState("C_PreventFastLap"))
				{					
					Console.WriteLine("Skipped: prevent fast lap on");
					return null;
				}
	
				// Don't show if grid/results are up
				if (item.Theme.ConditionFlags.CheckState("C_Grid") || item.Theme.ConditionFlags.CheckState("C_Results"))
				{						
					Console.WriteLine("Skipped: grid/results on");
					return null;
				}
			}
			else 
			{				
				Console.WriteLine("Button used");
			}

			// Check state of HTicker
			var top = Top_NormalHTicker;
			if (item.Theme.ConditionFlags.CheckState("C_HTickerLarge"))
			{
				Console.WriteLine("HTIcker large");
				top = Top_ExtendedHTicker;
			}
			
			Console.WriteLine("Top = " + top);

			var widget = item.Theme.Widgets.Find("W_FastLap");
			widget.ForceTop(top);

			// Start storyboard
			var sb = item.Theme.Storyboards.Find("SB_FastLap");
			sb.Play();

			return null;
		}
	}
}