﻿using System.Collections;
using DarkTonic.PoolBoss;
using UnityEngine;
using ORKFramework;

// ReSharper disable once CheckNamespace
public static class ORKPoolBossWrapper
{
	// instantiate
	public static GameObject Instantiate(GameObject prefab)
	{
		return Instantiate(prefab, Vector3.zero, Quaternion.identity);
	}

	public static GameObject Instantiate(GameObject prefab, Vector3 position, Quaternion rotation)
	{
		if(prefab != null)
		{
			Transform spawned = PoolBoss.SpawnInPool(prefab.transform, position, rotation);
			if(spawned != null)
			{
				spawned.parent = null;
				return spawned.gameObject;
			}
		}
		return null;
	}

	// destroy
	public static void Destroy(GameObject gameObject)
	{
		if(gameObject != null)
		{
			PoolBoss.Despawn(gameObject.transform);
			ORK.StartCoroutine(CheckDestroyed(gameObject));
		}
	}

	public static void Destroy(GameObject gameObject, float time)
	{
		if(gameObject != null)
		{
			ORK.StartCoroutine(DestroyAfterWait(gameObject, time));
		}
	}

	private static IEnumerator DestroyAfterWait(GameObject gameObject, float time)
	{
		yield return new WaitForSeconds(time);
		if(gameObject != null)
		{
			PoolBoss.Despawn(gameObject.transform);
			ORK.StartCoroutine(CheckDestroyed(gameObject));
		}
	}

	private static IEnumerator CheckDestroyed(GameObject gameObject)
	{
		yield return null;
		if(gameObject != null &&
			gameObject.activeInHierarchy)
		{
			GameObject.Destroy(gameObject);
		}
	}
}