using Sirenix.OdinInspector;
using UnityEngine;

namespace mGame
{
    public class Bubble : MonoBehaviour
    {
        [FoldoutGroup("Component")] public MapSO shareData;
        [FoldoutGroup("Component")] public mPS.FlayerVelUnit velUnit;
        [FoldoutGroup("Component")] public Transform preformBubble;
        [FoldoutGroup("Component")] public MeshRenderer meshRender;
        [FoldoutGroup("Component")] public UnityEngine.VFX.VisualEffect bubbleCrap;
        [FoldoutGroup("Component")] public AudioSource onCrap;

        [FoldoutGroup("Setting")] public AnimationCurve lifeToScale;
        [FoldoutGroup("Setting")] public AnimationCurve jumpHeight;
        [FoldoutGroup("Setting")] public float initLife = 4f;
        [FoldoutGroup("Setting")] public float lifeScaleLerp = .1f;

        [FoldoutGroup("Setting")] public float jumpTime;

        [FoldoutGroup("RunTime")] public bool valid = true;
        [FoldoutGroup("RunTime")] public Block onBlock;
        [FoldoutGroup("RunTime")] public float life;
        [FoldoutGroup("RunTime")] public float curScale = 1f;
        [FoldoutGroup("RunTime")] public float jump;

        #region
        public void DoJump()
        {
            jump = jumpTime;
        }
        #endregion
        private void OnEnable()
        {
            velUnit.VelAwake();
        }
        public void DoInit(float initLife)
        {
            valid = true;
            life = initLife;
            jump = 0;

            meshRender.enabled = true;

            curScale = 1;
            transform.localScale = Vector3.one;
        }

        private void FixedUpdate()
        {
            if (!valid) return;
            if (jump > 0) jump -= Time.deltaTime;

            velUnit.VelFixedUpdate();
            onBlock = shareData.GetBlock(transform.position);
            float onBlockRed = onBlock switch
            {
                Block.Desert => 3,
                Block.Mountain => jump > 0 ? 1 : 100000,
                Block.Tree => 6,
                _=>1
            };
            if (life > 0) life -= onBlockRed * Time.fixedDeltaTime;


            curScale = Mathf.Lerp(curScale, lifeToScale.Evaluate(life), lifeScaleLerp);

            preformBubble.localPosition = new Vector3(0,jumpHeight.Evaluate(jump), 0);
            preformBubble.localScale = curScale * Vector3.one;


            if(life < 0)
            {
                valid = false;
                meshRender.enabled = false;

                transform.localScale = Vector3.one;
                velUnit.VelAwake();

                DoBubbleBoom();
                //bubbleCrap.Play();
                //MGameManager.Instance.DoBubbleBlow(transform.position, Block.Desert, Block.Grass);
                //MGameManager.Instance.DoBubbleBlow(transform.position, Block.Dirt, Block.Tree);
                //MGameManager.Instance.DoBubbleBlow(transform.position, Block.DryTown, Block.GreenTown);
                //MGameManager.Instance.AddRecycle(gameObject);
            }
        }

        public void DoBubbleBoom(int radiu = 1)
        {
            bubbleCrap.Play();
            onCrap.Play();

            MGameManager.Instance.DoBubbleBlow(transform.position, Block.Desert, Block.Grass, radiu);
            MGameManager.Instance.DoBubbleBlow(transform.position, Block.Dirt, Block.Tree, radiu);
            MGameManager.Instance.DoBubbleBlow(transform.position, Block.DryTown, Block.GreenTown, radiu);
            MGameManager.Instance.AddRecycle(gameObject);
        }

    }
}


#region Debug
#if UNITY_EDITOR
        

#endif
#endregion
