using System;
using ATVO.ThemesSDK;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Scripting.DotNET;
using ATVO.ThemeEditor.Data;
using System.Collections.Generic;

namespace Scripts
{
	public class ChangeTickerClassFilter : IScript
	{
		public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
		{
			// Which ticker(s) are we changing?
			List<Widget> tickers = new List<Widget>();
			var tickerValue = (string)value;
			if (tickerValue == "H")
			{
				// Horizontal ticker
				tickers.Add(item.Theme.Widgets.Find("W_HTicker"));
			}
			else if (tickerValue == "V")
			{
				// Vertical ticker
				tickers.Add(item.Theme.Widgets.Find("W_VTickerTop"));
				tickers.Add(item.Theme.Widgets.Find("W_VTickerBottom"));
			}
			else if (tickerValue == "Grid")
			{
				// Grid tickers
				tickers.Add(item.Theme.Widgets.Find("W_GridLeft"));
				tickers.Add(item.Theme.Widgets.Find("W_GridRight"));				
			}
			else if (tickerValue == "Results")
			{
				// Results ticker
				tickers.Add(item.Theme.Widgets.Find("W_Results"));
			}
			
			// Default: all classes
			DataClassIndex index = DataClassIndex.AllClasses;

			// Find the dropdown and the index of the selected item
			var dropdown = (Dropdown)item;
			var selectedIndex = dropdown.Items.IndexOf(dropdown.SelectedItem);
			if (selectedIndex >= 0)
			{
				// The index of the dropdown items happens to coincide with the class filter indices, 
				// so this is an easy way to set the index without a long list of "if" or switch statements
				index = (DataClassIndex) selectedIndex;
			}

			// Finally change the class filter
			// Also restart the ticker to have it recalculate which items to show
			foreach (var ticker in tickers)
			{
				ticker.DataClassIndex = index;	
				ticker.Ticker.Restart();
			}
			
			return null;
		}
	}
}