using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace mGame
{
    public class Wind_Height : MonoBehaviour
    {
        public GameObject windSlot;
        public Transform vfxRoot;

        public List<Vector3> lowPressure = new();
        public List<WindSlot> winds = new();

        public void DoInit(List<Vector3> lp)
        {
            lowPressure.Clear();
            for(int i = 0; i < lp.Count; i++)
            {
                lowPressure.Add(lp[i]);
                WindSlot ws = ObjectPool.Instance.GetObject(windSlot).GetComponent<WindSlot>();

                ws.transform.SetParent(vfxRoot);
                winds.Add(ws);
            }
        }

       
        private void OnDisable()
        {
            for (int i = winds.Count - 1; i >= 0; i--)
                ObjectPool.Instance.PushObject(winds[i].gameObject);
            winds.Clear();
            lowPressure.Clear();
        }
        private void OnTriggerEnter(Collider other)
        {
            if(other.TryGetComponent(out Bubble bb))
            {

            }
        }

        private void FixedUpdate()
        {
            
            for (int i = 0; i < winds.Count; i++)
            {
                WindSlot ws = winds[i];

                Vector3 startPos = transform.position;
                Vector3 endPos = lowPressure[i];
                //endPos.z = -endPos.z;
                Vector3 rotDir = endPos - startPos;
                rotDir.y = 0;

                bool isHit = Physics.Raycast(startPos, rotDir, out RaycastHit hit, rotDir.magnitude, LayerMask.GetMask("Wind"));
                Vector3 end = isHit? hit.point : endPos;
                rotDir = end - startPos;

                ws.wind = rotDir.normalized * ws.windPower;
                ws.rotater.DoRotate(rotDir);
                ws.transform.position = (transform.position + end) / 2;
                ws.transform.localScale = new Vector3(rotDir.magnitude, 5, 1);

            }
        }
    }
}

