﻿using System;
using JLGames.RocketDriver.Actions.Utils;
using JLGames.RocketDriver.CSharp.Pool;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Pool
{
    public class MetaGameObjectPool : MetaObjectPool<GameObject>
    {
        private Transform m_Container;
        private bool m_AutoActive;

        public MetaGameObjectPool(GameObject original, int size = 0, int capacity = 0) : base(original, size, capacity)
        {
        }

        public MetaGameObjectPool(OriginGenFunc originGenFuncGen, int size = 0, int capacity = 0) : base(
            originGenFuncGen, size,
            capacity)
        {
        }

        public MetaGameObjectPool(GameObject original, int size) : base(original, size)
        {
        }

        public MetaGameObjectPool(OriginGenFunc originGenFuncGen, int size) : base(originGenFuncGen, size)
        {
        }

        public void SetContainer(Transform container, bool autoActive = true)
        {
            m_Container = container;
            m_AutoActive = autoActive;
        }

        /// <summary>
        /// Add objects by size
        /// 增加对象
        /// </summary>
        /// <param name="addSize">>增加的数量</param>
        /// <returns>addSize少于0: null</returns>
        public override GameObject[] Add(int addSize)
        {
            var rs = base.Add(addSize);

            if (null == m_Container)
            {
                return rs;
            }

            foreach (var o in rs)
            {
                TransformUtil.AddChildTo(o.transform, m_Container);
                if (m_AutoActive)
                {
                    o.SetActive(true);
                }
            }

            return rs;
        }

        /// <summary>
        /// Remove objects by size
        /// 删除对象
        /// </summary>
        /// <param name="removeSize">删除的数量</param>
        /// <returns>removeSize少于0: null</returns>
        public override GameObject[] Remove(int removeSize)
        {
            var rs = base.Remove(removeSize);
            if (null == m_Container)
            {
                return rs;
            }

            for (var index = rs.Length - 1; index >= 0; index--)
            {
                TransformUtil.RemoveAndDestroy(rs[index]);
            }

            return rs;
        }

        public override GameObject RemoveFirst(Predicate<GameObject> match)
        {
            var rs = base.RemoveFirst(match);
            if (null == m_Container)
            {
                return rs;
            }

            if (null != rs)
                TransformUtil.RemoveAndDestroy(rs);
            return rs;
        }

        public override GameObject RemoveLast(Predicate<GameObject> match)
        {
            var rs = base.RemoveLast(match);
            if (null == m_Container)
            {
                return rs;
            }

            if (null != rs)
                TransformUtil.RemoveAndDestroy(rs);
            return rs;
        }

        protected override GameObject NewObject()
        {
            return UnityEngine.Object.Instantiate(m_Original);
        }
    }
}