using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class Pool
{
    [SerializeField]
    GameObject p_myPrefab;
    [SerializeField]
    int p_myNumber;

    public GameObject getPrefab()
    {
        return p_myPrefab;
    }
    public int getNumber()
    {
        return p_myNumber;
    }
}

public class ListPool
{
    string p_myTag;
    List<GameObject> p_myList;

    public ListPool()
    {
        p_myTag = "";
        p_myList = new List<GameObject>();
    }

    public ListPool(string tag)
    {
        p_myTag = tag;
        p_myList = new List<GameObject>();
    }

    public ListPool(string tag, List<GameObject> list)
    {
        p_myTag = tag;
        p_myList = list;
    }

    public string getTag()
    {
        return p_myTag;
    }
    public List<GameObject> getList()
    {
        return p_myList;
    }
}

public class PoolManager : MonoBehaviour
{
    [SerializeField]
    List<Pool> listPool;
    static List<Pool> p_myListPool;
    static GameObject p_myPool;
    static List<ListPool> p_myObjPool;
    static GameObject p_myEarth;

    void Awake()
    {
        p_myEarth = GameObject.FindGameObjectWithTag("Earth");
        p_myListPool = listPool;
        p_myObjPool = new List<ListPool>();
        p_myPool = GameObject.FindGameObjectWithTag("Pool");
        foreach (Pool p in p_myListPool)
        {
            List<GameObject> temp = new List<GameObject>();
            for (int i = 0; i < p.getNumber(); i++)
            {
                GameObject newItem = Instantiate(p.getPrefab(), p_myPool.transform.position, Quaternion.identity) as GameObject;
                temp.Add(newItem);
                newItem.transform.parent = p_myPool.transform;
                newItem.SetActive(false);
            }
            ListPool tempList = new ListPool(p.getPrefab().tag, temp);
            p_myObjPool.Add(tempList);
        }
    }

    public static GameObject Instantiation(string tag, Vector3 position, Quaternion rotation)
    {
        foreach (ListPool l in p_myObjPool)
        {
            if (l.getTag() == tag)
            {
                List<GameObject> temp = l.getList();
                if (temp.Count != 0)
                {
                    GameObject obj = temp[0];
                    obj.transform.position = position;
                    obj.transform.rotation = rotation;
                    obj.SetActive(true);
                    if (obj.tag == "Human")
                    {
                        obj.GetComponent<HumanScript>().Initialize(p_myEarth);
                    }
                    else
                    {
                        obj.GetComponent<BuildingScript>().Initialize(p_myEarth);
                    }
                    temp.RemoveAt(0);
                    return obj;
                }
                else
                {
                    foreach (Pool p in p_myListPool)
                    {
                        if (p.getPrefab().tag == tag)
                        {
                            Debug.Log(tag);
                            GameObject newItem = Instantiate(p.getPrefab(), position, rotation) as GameObject;
                            if (newItem.tag == "Human")
                            {
                                newItem.GetComponent<HumanScript>().Initialize(p_myEarth);
                            }
                            else
                            {
                                newItem.GetComponent<BuildingScript>().Initialize(p_myEarth);
                            }
                            return newItem;
                        }
                    }
                }
            }
        }
        return null;
    }

    public static void Death(GameObject obj)
    {
        foreach (ListPool lp in p_myObjPool)
        {
            if (lp.getTag() == obj.tag)
            {
                if (obj.tag == "Human")
                {
                    HumanManager.removeHuman();
                }
                obj.transform.parent = p_myPool.transform;
                lp.getList().Add(obj);
                obj.transform.position = p_myPool.transform.position;
                obj.SetActive(false);
            }
        }
    }
}