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

using JumpTower;

namespace JumpTower
{
    public class NewCylinderCreator : MonoBehaviour
    {
        // Start is called before the first frame update
        [SerializeField] private GameObject objParent;
        private GameObject cylender;
        private GameObject cylenderParent;
        private BallMovment ballMovment;
        private CylinderResorce cylenderParentResorce;
        private TextMeshProUGUI scoreText;

        void Start()
        {
            try
            {
                cylenderParent = GameObject.Find("CylenderParent");
                scoreText = GameObject.Find("ScoreText").GetComponent<TextMeshProUGUI>();
                cylenderParentResorce = cylenderParent.GetComponent<CylinderResorce>();
                ballMovment = GameObject.Find("Player").GetComponent<BallMovment>();
                cylender = cylenderParentResorce.cylender;
            }
            catch
            {
                Debug.Log("Somthing Not Find");
            }

        }

        private void OnTriggerEnter(Collider colider)
        {
            if (cylender != null && colider.tag == "Player")
            {
                GameObject newObject = Instantiate(cylender);
                newObject.transform.parent = cylenderParentResorce.transform;
                int newCylenderIndex = objParent.GetComponent<MeshCreator>().cylenderIndex + 6;
                newObject.transform.localPosition = new Vector3(0.0f, newCylenderIndex - 4.0f, 0.0f);
                newObject.GetComponent<MeshCreator>().cylenderIndex = newCylenderIndex;
                ballMovment.score = newCylenderIndex - 6;
                scoreText.text = (newCylenderIndex - 6).ToString();
                Destroy(gameObject);
            }
        }

    }
}