using Sirenix.OdinInspector;
using UnityEngine;

namespace mGame
{
    public class BubbleSpawner : MonoBehaviour
    {
        [FoldoutGroup("Components")] public Transform genPos;
        [FoldoutGroup("Components")] public Rotater genDir;
        [FoldoutGroup("Components")] public AudioSource mAudio;

        [FoldoutGroup("Setting")] public GameObject bubblePrefab;
        [FoldoutGroup("Setting")] public float genGap = 1f;

        [FoldoutGroup("Setting")] public Vector3 initBubbleDir;
        [FoldoutGroup("Setting")] public float initBubblePower = 10;
        [FoldoutGroup("Setting")] public float initBubble;

        [FoldoutGroup("RunTime")] public float cooling;
        public void OnEnable()
        {
            cooling = genGap;
        }

        private void OnMouseDrag()
        {
            initBubbleDir = MGameManager.Instance.mousePos - transform.position;
            initBubbleDir.y = 0;

            genDir.DoRotate(initBubbleDir);
        }

        private void FixedUpdate()
        {
            cooling -= Time.fixedDeltaTime;

            if(cooling <= 0)
            {
                cooling = genGap;
                Bubble bb = ObjectPool.Instance.GetObject(bubblePrefab, genPos.position).GetComponent<Bubble>();
                bb.DoInit(initBubble);
                bb.velUnit.SetImpulseVelocityOverWrite(initBubbleDir.normalized * initBubblePower);

                mAudio.Play();
            }
        }
    }
}

