using UnityEngine;

namespace Cryville.Crtr.Skin.Components {
	public class MeshWrapper {
		public GameObject MeshObject {
			get;
			private set;
		}
		public MeshFilter MeshFilter {
			get;
			private set;
		}
		Mesh m_mesh;
		public Mesh Mesh {
			get { return m_mesh; }
			set { MeshFilter.sharedMesh = m_mesh = value; }
		}
		public Transform MeshTransform {
			get;
			private set;
		}
		public Renderer Renderer {
			get;
			private set;
		}
		public bool Initialized {
			get;
			private set;
		}
		public static Material NewMaterial() {
			return Material.Instantiate(BuiltinResources.Materials["-SpriteMat"]);
		}
		public void Init(Transform parent) {
			MeshObject = new GameObject("__mesh__");
			MeshTransform = MeshObject.transform;
			MeshTransform.SetParent(parent, false);
			if (MeshObject.GetComponent<MeshFilter>() == null)
				MeshObject.AddComponent<MeshFilter>();
			if (MeshObject.GetComponent<MeshRenderer>() == null)
				MeshObject.AddComponent<MeshRenderer>();
			MeshFilter = MeshObject.GetComponent<MeshFilter>();
			Renderer = MeshObject.GetComponent<Renderer>();
			Initialized = true;
		}
		public void Destroy() {
			Mesh.Destroy(m_mesh);
			m_mesh = null;
			GameObject.Destroy(MeshObject);
		}
	}
}
