using System;
using System.Collections;
using UnityEngine;

namespace TriLib
{
	public static class TransformExtensions
	{
		public static void LoadMatrix(this Transform transform, Matrix4x4 matrix, bool local = true)
		{
			if (local)
			{
				transform.localScale = matrix.ExtractScale();
				transform.localRotation = matrix.ExtractRotation();
				transform.localPosition = matrix.ExtractPosition();
			}
			else
			{
				transform.rotation = matrix.ExtractRotation();
				transform.position = matrix.ExtractPosition();
			}
		}

		public static Bounds EncapsulateBounds(this Transform transform)
		{
			Bounds result = default(Bounds);
			Renderer[] componentsInChildren = transform.GetComponentsInChildren<Renderer>();
			if (componentsInChildren != null)
			{
				foreach (Renderer renderer in componentsInChildren)
				{
					result.Encapsulate(renderer.bounds);
				}
			}
			return result;
		}

		public static Transform FindDeepChild(this Transform transform, string name, bool endsWith = false)
		{
			if ((!endsWith) ? transform.name.EndsWith(name) : (transform.name == name))
			{
				return transform;
			}
			IEnumerator enumerator = transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					Transform transform2 = (Transform)obj;
					Transform transform3 = transform2.FindDeepChild(name, false);
					if (transform3 != null)
					{
						return transform3;
					}
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
			return null;
		}

		public static void DestroyChildren(this Transform transform, bool destroyImmediate = false)
		{
			for (int i = transform.childCount - 1; i >= 0; i--)
			{
				Transform child = transform.GetChild(i);
				if (destroyImmediate)
				{
					UnityEngine.Object.DestroyImmediate(child.gameObject);
				}
				else
				{
					UnityEngine.Object.Destroy(child.gameObject);
				}
			}
		}
	}
}
