using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace JumpTower
{
    public class BallMovment : MonoBehaviour
    {
        [SerializeField] private float bounceSpeed;
        [SerializeField] private GameObject hit;
        [SerializeField] private GameObject cylinder;
        [SerializeField] private Image flashBang;
        [SerializeField] private float duration = 1.0f;
        [SerializeField] private bool _isEnemy = false;

        private SaveSystem saveSystem;
        private float verticalForce;
        private float HorizontalForce;
        private Rigidbody rb;
        private Rigidbody cylinderRb;
        private Coroutine flashCoroutine;
        private bool _lastBreak;
        internal int score;

        // Start is called before the first frame update
        void Start()
        {
            score = 0;
            rb = GetComponent<Rigidbody>();
            cylinderRb = cylinder.GetComponent<Rigidbody>();
            _lastBreak = false;
            Time.timeScale = 1f;
            try
            {
                saveSystem = GameObject.Find("Canvas").GetComponent<SaveSystem>();
            }
            catch { }
        }



        private void OnCollisionEnter(Collision collision)
        {
            if (_isEnemy)
            {
                cylinderRb.velocity = new Vector3(0.0f, bounceSpeed, 0.0f);
            }
            else if (rb != null && collision.gameObject.tag == "Bounce")
            {
                hit = collision.gameObject;
                _lastBreak = false;
                //Debug.Log(collision);
                cylinderRb.velocity = new Vector3(0.0f, bounceSpeed, 0.0f);
            }
            else if (rb != null && collision.gameObject.tag == "Break")
            {
                if (_lastBreak)
                {
                    Destroy(collision.gameObject);
                }
                else
                {
                    _lastBreak = true;
                    hit = collision.gameObject;
                    //Debug.Log("Break");
                    cylinderRb.velocity = new Vector3(0.0f, bounceSpeed, 0.0f);
                    Destroy(collision.gameObject);
                }
            }
            else if (rb != null && collision.gameObject.tag == "Slow")
            {
                _lastBreak = false;
                hit = collision.gameObject;
                cylinderRb.velocity = new Vector3(0.0f, bounceSpeed / 2, 0.0f);
                //Debug.Log("Slow");
            }
            else if (rb != null && collision.gameObject.tag == "Flash")
            {
                hit = collision.gameObject;
                if (flashCoroutine != null)
                {
                    _lastBreak = false;
                    StopCoroutine(flashCoroutine);
                }
                flashBang.color = new Color(flashBang.color.r, flashBang.color.g, flashBang.color.b, 1);
                cylinderRb.velocity = new Vector3(0.0f, bounceSpeed, 0.0f);
                flashCoroutine = StartCoroutine(FlashBang(flashBang, 0.05f, duration));
                //Debug.Log("Flash");
            }
            else
            {
                hit = collision.gameObject;
                saveSystem.SaveTry(score);
                Time.timeScale = 0f;
                SceneManager.LoadScene("Menu");
            }
        }


        IEnumerator FlashBang(Image image, float targetAlpha, float duration)
        {
            Color color = image.color;
            float startAlpha = color.a;
            float time = 0f;

            while (time < duration)
            {
                time += Time.deltaTime;
                // Manually interpolate the alpha value
                float alpha = startAlpha + (targetAlpha - startAlpha) * (time / duration);
                color.a = alpha;
                image.color = color;
                yield return null;
            }

            // Ensure the final value is set
            color.a = targetAlpha;
            image.color = color;
        }


    }

}
