using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace BR
{
	[SelectionBase]
	[ExecuteAlways]
	public class AssetGroup : MonoBehaviour
	{
#if UNITY_EDITOR
		#region DEFAULT METHODS
        void OnEnable()
        {
			foreach (Transform child in transform)
			{
				if (child.hideFlags != HideFlags.HideInHierarchy)
					child.hideFlags = HideFlags.HideInHierarchy;
			}
		}
        void OnDisable()
        {
			foreach (Transform child in transform)
			{
				if (child.hideFlags != HideFlags.None)
					child.hideFlags = HideFlags.None;
			}
		}
        void OnDrawGizmos()
        {
			if (isActiveAndEnabled)
				foreach(Transform _child in GetComponentsInChildren<Transform>())
					if (_child != transform && UnityEditor.Selection.Contains(_child.gameObject))
					{
						UnityEditor.Selection.objects = UnityEditor.Selection.objects.ToList().FindAll(x => !(x is GameObject) || (x as GameObject).transform.parent != transform).ToArray();

						if (!UnityEditor.Selection.objects.Contains(gameObject))
						{
							List<Object> _objects = new List<Object>();

							_objects.AddRange(UnityEditor.Selection.objects);

							_objects.Add(gameObject);

							UnityEditor.Selection.objects = _objects.ToArray();
						}
					}
        }
        void OnDestroy()
		{
			foreach (Transform child in transform)
			{
				if (!child)
					continue;

				child.gameObject.hideFlags = HideFlags.None;
				child.hideFlags = HideFlags.None;
			}
		}
		#endregion
#endif
	}
}