using GCGame.Table;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class ChildModelAndEffectCtr : MonoBehaviour
{

    public UICameraTexture _ModelCamera;
    public GameObject _EffectPanel;
    public RemainTimeCoroutine _RemainTimeCoroutine;
    public Transform _EmbEffectParent;

    [Serializable]
    public class RemainTimeCoroutineCountOverCallBack : UnityEvent<int>
    {
        public RemainTimeCoroutineCountOverCallBack() { }
    }
    [SerializeField]
    public RemainTimeCoroutineCountOverCallBack _CallBack;


    private const int _BundleId = 4113;
    private int _CurSelectChildIndex = 0;
    private int _CurCharModelId = -1;
    public void ShowChild(int index)
    {
        if (_CurSelectChildIndex != index)
            _CurCharModelId = -1;

        _CurSelectChildIndex = index;

        if (ChildData.GetChildByIndex(_CurSelectChildIndex).basic.embryoEndtime > GlobalData.ServerAnsiTime)
        {
            //孕育状态
            _ModelCamera.gameObject.SetActive(false);
            _EffectPanel.SetActive(true);

            InitEmbEffectModel();
        }
        else
        {
            //正常状态
            _ModelCamera.gameObject.SetActive(true);
            _EffectPanel.SetActive(false);

            InitCharModel();
        }
    }

    private Dictionary<string, GameObject> _EffectPoolDic = new Dictionary<string, GameObject>();
    void InitEmbEffectModel()
    {
        var bundlePathTab = TableManager.GetBundlePathByID(_BundleId, 0);
        if (bundlePathTab == null)
        {
            Debug.LogError(" BundlePath is null, id is : " + _BundleId);
            return;
        }
        var effectName = bundlePathTab.Name;
        var _EffectName = bundlePathTab.Name;

        if (_EffectPoolDic.ContainsKey(effectName))
        {
            var _EffectObj = _EffectPoolDic[effectName];
            _EffectObj.gameObject.SetActive(true);
            _EffectObj.transform.SetParent(_EmbEffectParent);
            _EffectObj.transform.localPosition = Vector3.zero;
            _EffectObj.transform.localRotation = Quaternion.Euler(Vector3.zero);
            _EffectObj.transform.localScale = Vector3.one * bundlePathTab.Scale;

            _EffectObj.EnsureComponent<UIParticleSystem>();

//            WindowSign wnd = gameObject.GetComponentInParent<WindowSign>();
//            if (wnd != null)
//                wnd.SetWndDirty();
//            else
//            {
//#if UNITY_EDITOR
//                Debug.LogError("wnd is null");
//#endif
//            }
        }
        else
        {
            LoadAssetBundle.Instance.LoadGameObject(LoadAssetBundle.BUNDLE_PATH_EFFECT, effectName,
               delegate (string assetName, GameObject resObj, Hashtable hashParam)
               {
                   if (resObj != null)
                   {
                       var effect = Instantiate(resObj);

                       effect.gameObject.SetActive(true);
                       effect.transform.SetParent(_EmbEffectParent);
                       effect.transform.localPosition = Vector3.zero;
                       effect.transform.localRotation = Quaternion.Euler(Vector3.zero);
                       effect.transform.localScale = Vector3.one;
                       SetChildScale(effect.transform, bundlePathTab.Scale);
                       if (!_EffectPoolDic.ContainsKey(_EffectName))
                           _EffectPoolDic.Add(_EffectName, effect);

                       effect.EnsureComponent<UIParticleSystem>();
                       //var wnd = gameObject.EnsureComponent<WindowSign>();
                       //if (wnd != null)
                       //    wnd.SetWndDirty();
                   }
               }, new Hashtable());
        }

        _RemainTimeCoroutine.gameObject.SetActive(true);
        _RemainTimeCoroutine.StartCountTime(ChildData.GetChildByIndex(_CurSelectChildIndex).basic.embryoEndtime + 60, () =>
        {
            if (_CallBack != null)
                _CallBack.Invoke(_CurSelectChildIndex);
            else
                Debug.LogError("_CallBack is null");
        });
    }

    private void SetChildScale(Transform obj, float scale)
    {
        var allChilsTransform = obj.GetComponentsInChildren<Transform>();
        for (int index = 0; index < allChilsTransform.Length; index++)
            allChilsTransform[index].transform.localScale *= scale;

        var allParticles = obj.GetComponentsInChildren<ParticleSystem>();
        foreach (var particle in allParticles)
        {
            var render = particle.GetComponent<ParticleSystemRenderer>();
            if (render)
            {
                render.lengthScale = scale;
                render.velocityScale = scale;
            }
        }
    }

    void InitCharModel()
    {
        if(ChildData.GetChildCurDressedFashionIdByIndex(_CurSelectChildIndex) != -1)
        {
            ShowChildFashion(_CurSelectChildIndex);
        }else
        {
            //模型
            var charModelTab = GameManager.gameManager.PlayerDataPool.ChildData.GetCharModelTab(ChildData.GetChildByIndex(_CurSelectChildIndex));
            if (_CurCharModelId == charModelTab.Id)
                return;

            _CurCharModelId = charModelTab.Id;
            _ModelCamera.InitModelPath(charModelTab.ResPath, charModelTab);
        }
    }

    public void PlayChildAnimation(int operationIndex)
    {
        //互动动作
        var childParamTab = TableManager.GetChildrenParaByID(0, 0);
        if (childParamTab != null)
        {
            var animationId = childParamTab.GetInteractAnimationIdbyIndex(operationIndex);
            var animationTab = TableManager.GetAnimationByID(animationId, 0);
            if (animationTab == null)
            {
                Debug.LogError("animationTab is null : " + animationId);
                return;
            }

            if (_ModelCamera._FakeObj != null &&
                _ModelCamera._FakeObj.FakeObj != null &&
                _ModelCamera._FakeObj.FakeObj.AnimLogic != null)
            {
                var animationLogic = _ModelCamera._FakeObj.FakeObj.AnimLogic;
                animationLogic.Play(animationId);
            }
        }
    }

    public void ShowChildFashion(int _CurSelctIndex, int _CurSelectFashionItemId = -1)
    {
        if (_CurSelectChildIndex != _CurSelctIndex)
            _CurCharModelId = -1;

        _CurSelectChildIndex = _CurSelctIndex;

        if (ChildData.GetChildByIndex(_CurSelectChildIndex) == null)
        {
            return;
        }

        if (ChildData.GetChildByIndex(_CurSelectChildIndex).basic.embryoEndtime > GlobalData.ServerAnsiTime)
        {
            //孕育状态
            _ModelCamera.gameObject.SetActive(false);
            _EffectPanel.SetActive(true);

            InitEmbEffectModel();
            return;
        }
        _ModelCamera.gameObject.SetActive(true);
        _EffectPanel.SetActive(false);

        if (_CurSelectFashionItemId != -1
        || ChildData.GetChildCurDressedFashionIdByIndex(_CurSelectChildIndex) != -1)
        {
            var childFashionTab = TableManager.GetChildrenFashionByID(_CurSelectFashionItemId != -1 
                ? _CurSelectFashionItemId : ChildData.GetChildCurDressedFashionIdByIndex(_CurSelectChildIndex), 0);
            if (childFashionTab == null)
            {
                Debug.LogError("childFashionTab is null : " + _CurSelectFashionItemId);
                return;
            }

            var charModelId = ChildData.GetChildByIndex(_CurSelectChildIndex).basic.gender == (int)ChildHeadIcon.ChildSex.Boy ?
                childFashionTab.BoyCharModelId : childFashionTab.GirlCharModelId;

            if (_CurCharModelId == charModelId)
                return;

            _CurCharModelId = charModelId;

            var charModelTab = TableManager.GetCharModelByID(charModelId, 0);
            if (charModelTab == null)
            {
                Debug.LogError("CharModelTab is null : " + charModelId);
                return;
            }

            _ModelCamera.InitModelPath(charModelTab.ResPath, charModelTab);
        }
        else
        {
            var childLevelTab = TableManager.GetChildrenLevelUpByID(ChildData.GetChildByIndex(_CurSelectChildIndex).basic.curLevel, 0);
            if (childLevelTab == null)
            {
                Debug.LogError("childLevelTab is null : " + ChildData.GetChildByIndex(_CurSelectChildIndex).basic.curLevel);
                return;
            }
            var childTypeTab = TableManager.GetChildrenStateTypeByID(childLevelTab.CurStateType, 0);
            if (childTypeTab == null)
            {
                Debug.LogError("childStateTypeTab is null : " + childLevelTab.CurStateType);
                return;
            }
            var childModelId = ChildData.GetChildByIndex(_CurSelectChildIndex).basic.gender == (int)ChildHeadIcon.ChildSex.Boy ? childTypeTab.BoyModelId : childTypeTab.GirlModelId;

            if (_CurCharModelId == childModelId)
                return;
            _CurCharModelId = childModelId;

            var charModelTab = TableManager.GetCharModelByID(childModelId, 0);
            if (charModelTab == null)
            {
                Debug.LogError("charModelTab is null : " + childModelId);
                return;
            }
            _ModelCamera.InitModelPath(charModelTab.ResPath, charModelTab);
        }
    }

    private void OnDisable()
    {
        _CurCharModelId = -1;
    }
}