namespace Carelearning.Prototype.ViewModels
{
	using System.Threading.Tasks;	
    using System.Linq;
    using System.Collections.Generic;
    using Carelearning.Prototype.Models; 

	public class DefaultViewModel : MasterPageViewModel
	{
		public MySubViewModel SubViewModel { get; set; }

		public string Title { get; set; } = "Prototype Cascading Dropdowns";

		public override async Task PreRender()
		{
			if (!Context.IsPostBack)
				await InitializeListAsync();

			await base.PreRender();
		}

		public void OnActivityChanged()
		{
			// HACK no op
		}

		public void OnStatusChanged()
		{
			// Refresh activities based on the new status selection
			switch (SubViewModel.SelectedActivityTypeId)
			{
				case 1: // Course
					SubViewModel.CourseGuiLoad();
					break;
				case 2: // Event
					SubViewModel.EventGuiLoad();
					break;
				case 3: // TrackIt Title
					SubViewModel.TrackItTitleGuiLoad();
					break;
			}
		}

		public void OnTypeChanged()
		{
			// Always set the activity type default when type changes - this was the bug!
			SetActivityTypeDefault();

			switch (SubViewModel.SelectedActivityTypeId)
			{
				case 1: // Course
					SubViewModel.CourseGuiLoad();
					break;
				case 2: // Event
					SubViewModel.EventGuiLoad();
					break;
				case 3: // TrackIt Title
					SubViewModel.TrackItTitleGuiLoad();
					break;
			}
		}

		public void OnSubmit()
		{			
		}

		private void SetActivityTypeDefault()
		{
			// Update the previous value for tracking
			SubViewModel.PreviousSelectedActivityTypeId = SubViewModel.SelectedActivityTypeId;

			// Set default status based on activity type
			var statusId = 1;
			SubViewModel.SelectedStatusId = statusId;

			// Update the statuses list to reflect the new activity type
			switch (SubViewModel.SelectedActivityTypeId)
			{
				case 1: // Course
					SubViewModel.Statuses = SubViewModel.CourseStatuses
						.Select(x => new ReportFormOption
						{
							IsEnabled = x.IsEnabled,
							Text = x.Title,
							Value = x.Id
						})
						.ToList();
					break;
				case 2: // Event
					SubViewModel.Statuses = SubViewModel.EventStatuses
						.Select(x => new ReportFormOption
						{
							IsEnabled = x.IsEnabled,
							Text = x.Title,
							Value = x.Id
						})
						.ToList();
					break;
				case 3: // TrackIt Title
					SubViewModel.Statuses = SubViewModel.TrackItStatuses
						.Select(x => new ReportFormOption
						{
							IsEnabled = x.IsEnabled,
							Text = x.Title,
							Value = x.Id
						})
						.ToList();
					break;
			}
		}

		#region Private Methods
		private async Task InitializeListAsync()
		{
			await Task.CompletedTask;

			SubViewModel = new MySubViewModel();
			SubViewModel.Load();
		}		
		#endregion
		public class MySubViewModel
		{
			public int PreviousSelectedActivityTypeId { get; set; }
			public int Total => Activities.Count;

			public List<ReportFormOption> CourseItems { get; set; }
			public List<ReportFormOption> EventItems { get; set; }
			public List<ReportFormOption> TrackItItems { get; set; }

			public List<ActivityStatusFilterOption> CourseStatuses { get; set; }
			public List<ActivityStatusFilterOption> EventStatuses { get; set; }
			public List<ActivityStatusFilterOption> TrackItStatuses { get; set; }

			// Combobox Options
			public List<ReportFormOption> Activities { get; set; }
			public List<ReportFormOption> ActivityTypes { get; set; }
			public List<ReportFormOption> Statuses { get; set; }

			// Selected Combobox Values
			public int SelectedActivityId { get; set; }
			public int SelectedActivityTypeId { get; set; }
			public int SelectedStatusId { get; set; }

			public MySubViewModel()
			{
				Activities = new List<ReportFormOption>();
				ActivityTypes = new List<ReportFormOption>();
				CourseItems = new List<ReportFormOption>();
				CourseStatuses = new List<ActivityStatusFilterOption>();
				EventItems = new List<ReportFormOption>();
				EventStatuses = new List<ActivityStatusFilterOption>();
				Statuses = new List<ReportFormOption>();
				TrackItItems = new List<ReportFormOption>();
				TrackItStatuses = new List<ActivityStatusFilterOption>();
			}

			public void Load()
			{
				InitializeTypes();
				InitializeStatuses();
				InitializeActivities();

				CourseGuiLoad();
			}

			public static class Options
			{
				public const string AllStatus = "All";
				public const string BothStatus = "Both";
				public const string ClosedStatus = "Closed";
				public const string CompleteStatus = "Complete";
				public const string CompliantStatus = "Compliant";
				public const string CourseType = "Course";
				public const string CurrentClassroomStatus = "Current Classroom";
				public const string EventType = "Event";
				public const string ExpiredStatus = "Expired";
				public const string ExpiringSoonStatus = "Expiring Soon";
				public const string IncompleteStatus = "Incomplete";
				public const string NoDateStatus = "No Date";
				public const string OpenStatus = "Open";
				public const string PriorClassroomStatus = "Prior Classroom";
				public const string TrackItType = "TrackIt Title";
			}

			private string GetActivityTypeName(int activityTypeId)
			{
				switch (activityTypeId)
				{
					case 1:
						return nameof(ActivityType.Course);
					case 2:
						return nameof(ActivityType.Event);
					case 3:
						return nameof(ActivityType.TrackIt);
					default:
						return nameof(ActivityType.Course);
				}
			}

			private void InitializeActivities()
			{
				CourseItems = new List<ReportFormOption>
			{
				new ReportFormOption
				{
					Text = "Course 1",
					Value = 1,
					IsEnabled = true
				},
				new ReportFormOption
				{
					Text = "Course 2",
					Value = 2,
					IsEnabled = false
				},
				new ReportFormOption
				{
					Text = "Course 3",
					Value = 3,
					IsEnabled = true
				}
			};

				EventItems = new List<ReportFormOption>
			{
				new ReportFormOption
				{
					Text = "Event 1",
					Value = 4,
					IsEnabled = true
				},
				new ReportFormOption
				{
					Text = "Event 2",
					Value = 5,
					IsEnabled = false
				},
				new ReportFormOption
				{
					Text = "Event 3",
					Value = 6,
					IsEnabled = true
				}
			};

				TrackItItems = new List<ReportFormOption>
			{
				new ReportFormOption
				{
					Text = "TrackIt 1",
					Value = 7
				},
				new ReportFormOption
				{
					Text = "TrackIt 2",
					Value = 8
				},
				new ReportFormOption
				{
					Text = "TrackIt 3",
					Value = 9
				}
			};

				// Default to Courses;
				Activities = CourseItems.ToList();
				// Default to first course;
				SelectedActivityId = CourseItems.First().Value;
			}

			private void InitializeStatuses()
			{
				CourseStatuses = InitializeCourseStatuses();
				EventStatuses = InitializeEventStatuses();
				TrackItStatuses = InitializeTrackItStatuses();

				// Default to Courses
				Statuses = CourseStatuses
					.Select(x => new ReportFormOption
					{
						IsEnabled = x.IsEnabled,
						Text = x.Title,
						Value = x.Id
					})
					.ToList();

				// Default to Course Current Classroom
				SelectedStatusId = CourseStatuses.Single(x => x.Title == Options.CurrentClassroomStatus).Id;
			}

			private List<ActivityStatusFilterOption> InitializeCourseStatuses()
			{
				return new List<ActivityStatusFilterOption>
			{
				new ActivityStatusFilterOption
				{
					IsEnabled = true,
					Id = 1,
					Title = Options.CurrentClassroomStatus,
					Type = nameof(ActivityType.Course)
				},
				new ActivityStatusFilterOption
				{
					IsEnabled = false,
					Id = 2,
					Title = Options.PriorClassroomStatus,
					Type = nameof(ActivityType.Course)
				}
			};
			}

			private List<ActivityStatusFilterOption> InitializeEventStatuses()
			{
				return new List<ActivityStatusFilterOption>
			{
				new ActivityStatusFilterOption
				{
					IsEnabled = true,
					Id = 1,
					Title = Options.OpenStatus,
					Type = nameof(ActivityType.Event)
				},
				new ActivityStatusFilterOption
				{
					IsEnabled = false,
					Id = 2,
					Title = Options.ClosedStatus,
					Type = nameof(ActivityType.Event)
				}
			};
			}

			private List<ActivityStatusFilterOption> InitializeTrackItStatuses()
			{
				return new List<ActivityStatusFilterOption>
			{
				new ActivityStatusFilterOption
				{
					Id = 1,
					IsEnabled = true,
					Title = Options.AllStatus,
					Type = nameof(ActivityType.TrackIt)
				},
				new ActivityStatusFilterOption
				{
					Id = 2,
					IsEnabled = true,
					Title = Options.CompliantStatus,
					Type = nameof(ActivityType.TrackIt)
				},
				new ActivityStatusFilterOption
				{
					Id = 3,
					IsEnabled = false,
					Title = Options.ExpiredStatus,
					Type = nameof(ActivityType.TrackIt)
				},
				new ActivityStatusFilterOption
				{
					Id = 4,
					IsEnabled = true,
					Title = Options.ExpiringSoonStatus,
					Type = nameof(ActivityType.TrackIt)
				},
				new ActivityStatusFilterOption
				{
					Id = 5,
					IsEnabled = true,
					Title = Options.NoDateStatus,
					Type = nameof(ActivityType.TrackIt)
				}
			};
			}

			private void InitializeTypes()
			{
				ActivityTypes = new List<ReportFormOption>
			{
				new ReportFormOption
				{
					Text = Options.CourseType,
					Value = 1
				},
				new ReportFormOption
				{
					Text = Options.EventType,
					Value = 2
				},
				new ReportFormOption
				{
					Text = Options.TrackItType,
					Value = 3
				}
			};

				// On Load Default Course Type
				var activityTypeId = ActivityTypes
					.Single(x => x.Text == Options.CourseType)
					.Value;

				SelectedActivityTypeId = activityTypeId;
				PreviousSelectedActivityTypeId = activityTypeId;
			}

			public void SetSelectedActivityTypeId(int newValue)
			{
				if (SelectedActivityTypeId != newValue)
				{
					PreviousSelectedActivityTypeId = SelectedActivityTypeId;
					SelectedActivityTypeId = newValue;
				}
			}

			public void CourseGuiLoad()
			{
				var query = CourseItems
					.ToList()
					.AsQueryable();

				if (SelectedStatusId == 1) // Current Classroom
					query = query.Where(x => x.IsEnabled);

				if (SelectedStatusId == 2) // Prior Classroom
					query = query.Where(x => !x.IsEnabled);

				Statuses = CourseStatuses
					.Select(x => new ReportFormOption
					{
						IsEnabled = x.IsEnabled,
						Text = x.Title,
						Value = x.Id
					})
					.ToList();

				Activities = query.ToList()
					.Select(x => new ReportFormOption
					{
						Text = x.Text,
						Value = x.Value
					})
					.ToList();

				if (Activities.Any())
					SelectedActivityId = Activities.First().Value;
			}

			public void EventGuiLoad()
			{
				var query = EventItems
					.ToList()
					.AsQueryable();

				if (SelectedStatusId == 1) // Open
					query = query.Where(x => x.IsEnabled);

				if (SelectedStatusId == 2) // Closed
					query = query.Where(x => !x.IsEnabled);

				Statuses = EventStatuses
					.Select(x => new ReportFormOption
					{
						IsEnabled = x.IsEnabled,
						Text = x.Title,
						Value = x.Id
					})
					.ToList();


				Activities = query.ToList()
					.Select(x => new ReportFormOption
					{
						Text = x.Text,
						Value = x.Value
					})
					.ToList();

				if (Activities.Any())
					SelectedActivityId = Activities.First().Value;
			}

			public void TrackItTitleGuiLoad()
			{
				var query = TrackItItems
					.ToList()
					.AsQueryable();

				Statuses = TrackItStatuses
					.Select(x => new ReportFormOption
					{
						IsEnabled = x.IsEnabled,
						Text = x.Title,
						Value = x.Id
					})
					.ToList();

				Activities = query.ToList()
					.Select(x => new ReportFormOption
					{
						Text = x.Text,
						Value = x.Value
					})
					.ToList();

				if (Activities.Any())
					SelectedActivityId = Activities.First().Value;
			}
		}
	}
}