using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace TelerikTestReal
{
	public class TestParEditorItem : INotifyPropertyChanged
	{
		public TestParItem Item { get; private set; }
		public TestParSubItem SubItem { get; private set; }

		public bool IsItem => Item != null;
		public bool IsSubItem => SubItem != null;

		public object DataObject => IsSubItem ? (object)SubItem : (object)Item;

		public TestParEditorItem(TestParItem item, int gridId)
		{
			Item = item;
			GridID = gridId;
		}

		public TestParEditorItem(TestParSubItem subItem, int gridId, int gridParentId, string name)
		{
			SubItem = subItem;
			SubItemName = name;
			GridID = gridId;
			GridParentID = gridParentId;
		}


		public string Code
		{
			get => IsItem ? Item.Code : SubItem.Code;
			set
			{
				if (IsItem)
				{
					if (Item.Code != value)
					{
						Item.Code = value;
						NotifyPropertyChanged();
					}
				}
				else
				{
					if (SubItem.Code != value)
					{
						SubItem.Code = value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public TestType? TestType
		{
			get => IsItem ? (TestType?)Item.TestType : null;
			set
			{
				if (IsItem)
				{
					if (Item.TestType != value.Value)
					{
						Item.TestType = value.Value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public float? R
		{
			get => IsItem ? (float?)Item.R : null;
			set
			{
				if (IsItem)
				{
					if (Item.R != value.Value)
					{
						Item.R = value.Value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public float? Fi
		{
			get => IsItem ? (float?)Item.Fi : null;
			set
			{
				if (IsItem)
				{
					if (Item.Fi != value.Value)
					{
						Item.Fi = value.Value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public float Min
		{
			get => IsItem ? Item.Min : SubItem.Min;
			set
			{
				if (IsItem)
				{
					if (Item.Min != value)
					{
						Item.Min = value;
						NotifyPropertyChanged();
					}
				}
				else
				{
					if (SubItem.Min != value)
					{
						SubItem.Min = value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public float Max
		{
			get => IsItem ? Item.Max : SubItem.Max;
			set
			{
				if (IsItem)
				{
					if (Item.Max != value)
					{
						Item.Max = value;
						NotifyPropertyChanged();
					}
				}
				else
				{
					if (SubItem.Max != value)
					{
						SubItem.Max = value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public string Comment
		{
			get => IsItem ? Item.Comment : null;
			set
			{
				if (IsItem)
				{
					if (Item.Comment != value)
					{
						Item.Comment = value;
						NotifyPropertyChanged();
					}
				}
			}
		}

		public bool Disabled
		{
			get => IsItem ? Item.Disabled : SubItem.Disabled;
			set
			{
				if (IsItem)
				{
					if (Item.Disabled != value)
					{
						Item.Disabled = value;
						NotifyPropertyChanged();
					}
				}
				else
				{
					if (SubItem.Disabled != value)
					{
						SubItem.Disabled = value;
						NotifyPropertyChanged();
					}
				}
			}
		}


		public string SubItemName { get; private set; }

		public int GridID { get; private set; }
		public int GridParentID { get; private set; }

		public List<TestParEditorItem> SubItems { get; } = new List<TestParEditorItem>();

		public event PropertyChangedEventHandler PropertyChanged;

		private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
		{
			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
