using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
using Unity.Linq;
public class Spawner : MonoBehaviour
{
    public List<Human> HumansPrefabs = new List<Human>()
    {


    };
    public List<Soldier> SoldierPrefabs = new List<Soldier>()
    {

    };

    public HumanPool Humans;
    public SoldierPool Soldiers;

    void Start()
    {
        Humans = new HumanPool(HumansPrefabs, "Humans");
        Soldiers = new SoldierPool(SoldierPrefabs, "Soldier");
    }

    public void Spawn(MapChunk chunk)
    {
        foreach (var item in chunk.spawnPoints)
        {
            var i = SpawnType();
            if (i == 0)
            {

            }
            else if (i == 1)
            {

                var obj = Humans.Rent();
                obj.transform.parent = chunk.PeopleParent.transform;
                obj.transform.position = item.transform.position;
            }
            else if (i == 2)
            {

            }
        }
        CompositeDisposable disposables = new CompositeDisposable();
        chunk.MapEnded += Chunk_MapEnded;

    }

    private void Chunk_MapEnded(MapChunk chunk)
    {
        var childs = chunk.PeopleParent.gameObject.Children();
        foreach (var item in childs)
        {
            if (item.HasComponent<Human>())
            {
                var human = item.GetComponent<Human>();
                human.Reborn();
                Humans.Return(human);
            }
            else if (item.HasComponent<Soldier>())
            {
                var soldier = item.GetComponent<Soldier>();
                soldier.Reborn();
                Soldiers.Return(soldier);
            }
        }
    }

    int SpawnType()
    {
        return 1;
    }



}
public class HumanPool : SimpleMultiPool<Human>
{
    public HumanPool(IEnumerable<Human> prefab, string name) : base(prefab, name)
    {

    }
}

public class SoldierPool : SimpleMultiPool<Soldier>
{
    public SoldierPool(IEnumerable<Soldier> prefab, string name) : base(prefab, name)
    {

    }
}
