using System;
using System.Collections.Generic;
using Shared.IO;
using UIKit;

namespace Shared
{
    
    /// <summary>
    /// ViewGroup 按键
    /// </summary>
    public class ViewGroup : View
	{
        /// <summary>
        /// 当前真实的控件
        /// </summary>
        internal UIView realViewGroup;
		/// <summary>
		/// 当前控件
		/// </summary>
		/// <value>The view group.</value>
		internal UIView viewGroup
		{
			get
			{
				return uiView as UIView;
			}
			set
			{
				uiView = value;

				uiView.AddSubview(backgroundImage);
			}
		}

		/// <summary>
		/// 当前背景图
		/// </summary>
		UIKit.UIImageView backgroundImage = new UIImageView() { Tag = int.MinValue};

		/// <summary>
		/// 控件宽度
		/// </summary>
		public override int Width
		{
			get
			{
				return base.Width;
			}
			set
			{
				base.Width = value;

				if (!IsCanRefresh)
					return;
				var frame = backgroundImage.Frame;
				frame.Width = base.Width;
				backgroundImage.Frame = frame;

				var frame2 = realViewGroup.Frame;
				frame2.Width = base.Width;
				realViewGroup.Frame = frame2;
			}
		}

		/// <summary>
		/// 控件的高度
		/// </summary>
		/// <value>The height.</value>
		public override int Height
		{
			get
			{
				return base.Height;
			}
			set
			{
				base.Height = value;

				if (!IsCanRefresh)
					return;
				
				var frame = backgroundImage.Frame;
				frame.Height = base.Height;
				backgroundImage.Frame = frame;

				var frame2 = realViewGroup.Frame;
				frame2.Height = base.Height;
				realViewGroup.Frame = frame2;
			}
		}


		/// <summary>
		/// 增加子控件
		/// </summary>
		/// <param name="view">View.</param>
		public virtual void AddChidren(View view)
		{
			view.Parent = this;
			viewList.Add(view);
			realViewGroup.AddSubview(view.RealView);
			if (!IsCanRefresh)
			{
				return;
			}
			view.Refresh();

            addParentToAllSubViews(view);

            if (view is ViewGroup)
			{
				var tempViewGroup = (ViewGroup)view;
				for (int i = 0; i < tempViewGroup.ChildrenCount; i++)
				{
					tempViewGroup.GetChildren(i).Refresh();
				}
			}
        }

        internal void addParentToAllSubViews(View view)
        {
            if (view is ViewGroup)
            {
                var viewgroup = view as ViewGroup;
                for (int i = 0; i < viewgroup.viewList.Count; i++)
                {
                    var _view = viewgroup.viewList[i];
                    if (_view == null) continue;
                    _view.Parent = viewgroup;
                    
                    addParentToAllSubViews(_view);//递归,将子控件所包含的控件也一并移除;
                }
            }
        }

        /// <summary>
        /// 控件数量
        /// </summary>
        /// <value>The children count.</value>
        public virtual int ChildrenCount { get { return viewList.Count; } }

		/// <summary>
		/// 控件列表
		/// </summary>
		internal System.Collections.Generic.List<View> viewList = new System.Collections.Generic.List<View>();

		string backgroundImagePath;
		/// <summary>
		/// 背景图片
		/// </summary>
		/// <value>The background image path.</value>
		public virtual string BackgroundImagePath
		{
			get
			{
				return backgroundImagePath;
			}
			set
			{
				backgroundImagePath = value;

				if (!IsCanRefresh || Radius != 0)
				{
					return;
				}

                backgroundImage.Image = UIImage.FromFile(FileUtils.GetImageFilePath(backgroundImagePath));

			}
		}

		/// <summary>
		/// 刷新界面
		/// </summary>
		public override void Refresh()
		{
			base.Refresh();
			BackgroundImagePath = backgroundImagePath;
		}

		/// <summary>
		/// 移除控件
		/// </summary>
		/// <param name="view">View.</param>
		internal virtual void Remove(View view)
		{
			if (view == null)
			{
				return;
			}

			viewList.Remove(view);
            if (view.RealView.Superview != null)
            {
                view.RealView.RemoveFromSuperview();
            }
            view.Parent = null;
		}

        internal virtual void removeChildParent()
        {
            for (int i = 0; i < viewList.Count; i++)
            {
                var view = viewList[i];
                if (view == null) continue;
                if (view is ViewGroup)
                {
                    (view as ViewGroup).removeChildParent();
                }
                view.Parent = null;
            }
        }

        public override void RemoveFromParent()
        {
            removeChildParent();
            base.RemoveFromParent();
        }



        /// <summary>
        /// 清空所有的控件
        /// </summary>
        public virtual void RemoveAll()
		{
			while (0 < viewList.Count)
			{
                GetChildren(0)?.RemoveFromParent();
            }
		}

		/// <summary>
		/// 移除指定索引对象
		/// </summary>
		/// <param name="index">Index.</param>
		public virtual void RemoveAt(int index)
		{
            //Remove(GetChildren(index));
            GetChildren(index)?.RemoveFromParent();

        }

		/// <summary>
		/// 获取指定索引对象
		/// </summary>
		/// <returns>The children.</returns>
		/// <param name="index">Index.</param>
		public View GetChildren(int index)
		{
			if (index < 0 || viewList.Count - 1 < index)
			{
				return null;
			}
			return viewList[index];
		}

		/// <summary>
		/// 根据类型移除控件
		/// </summary>
		/// <param name="type">Type.</param>
		public void RemoveViewByType(System.Type type)
		{
			for (int i = 0; i < viewList.Count; i++)
			{
				if (viewList[i].GetType() == type)
				{
                    viewList[i].RemoveFromParent();
					i--;
				}
			}
		}

		/// <summary>
		/// 根据Tag移除控件
		/// </summary>
		/// <param name="type">Type.</param>
		public void RemoveViewByTag(object tag)
		{
			for (int i = 0; i < viewList.Count; i++)
			{
				if (viewList[i].Tag == tag)
				{
                    viewList[i].RemoveFromParent();
					i--;
				}
			}
		}

	}
}