using UnityEngine;
using System.Collections.Generic;

namespace mgaia.Utils
{
    /// <summary>
    /// Extensões para Game Object
    /// </summary>
    public static class GameObjectExtensions
    {
        /// <summary>
        /// Retorna o tamanho total do objeto, considerando seus filhos
        /// </summary>
		/// <owner>Felipe</owner>
        public static Vector3 GetSize(this GameObject obj, bool recursive, bool includeInactive)
        {
            Assert.AssertTrue(obj != null, "'obj' must be non-null");

            IEnumerable<Transform> transforms;
            if (recursive)
            {
                transforms = obj.GetComponentsInChildren<Transform>(includeInactive);
            }
            else
            {
                transforms = IEnumerableExtensions.AsEnumerable(obj.transform);
            }

            if (transforms != null && transforms.Count() > 0)
            {
                Bounds[] allBounds = transforms.GetBounds().ToArray();

                if (allBounds.Length > 0)
                {
                    Bounds current = allBounds[0];
                    for (int i = 1; i < allBounds.Length; ++i) { current.Encapsulate(allBounds[i]); }

                    return current.size;
                }
            }

            return Vector2.zero;
        }

		/// <summary>
		/// Retorna a câmera pai mais próxima
		/// </summary>
		/// <owner>Victor</owner>
		public static Camera GetParentCamera(this GameObject obj)
		{
			Camera[] cameras = obj.GetComponentsInParent<Camera>();

			if (cameras.Length > 0)
			{
				return cameras[0];
			}
			else
			{
				return Camera.main;
			}
		}
    }
}