using UnityEngine;
using System;

public class ReusablePrefabFactory : AbstractReusableFactory
{
	#region implemented abstract members of AbstractReusableFactory
	public override IReusable Create (object parameter)
	{
		return Create ((string)parameter);
	}
	#endregion
	
	public ReusablePrefab Create(string name)
	{
		GameObject obj;
		if ((obj = Resources.Load (name) as GameObject) == null)
		{
			throw new ArgumentException("Prefab \"" + name + "\" not found in Resources", "name");
		}
		
		if ((obj = GameObject.Instantiate(obj) as GameObject) == null)
		{
			throw new InvalidOperationException("Prefab \"" + name + "\" could not be instantiated");
		}
		
		obj.name = name;
		
		ReusablePrefab reusable = new ReusablePrefab(obj);
		
		obj.AddComponent<ReusableWrapper>().OnDestroyForwarder = reusable.Destroy;
		
		return reusable;
	}
}