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

namespace GGJ24
{

    public class BalloonC : MonoBehaviour
    {
        [SerializeField] private float coolDown = 0f;
        [SerializeField] private Vector2 boxSize = Vector2.zero;
        [SerializeField] private Transform movable = null;
        [SerializeField] private Transform ropeTarget = null;

        private LineRenderer rope = null;
        private Vector2 movementBox;
        private Animator explosion;
        private float pixelWidth;
        private float direction;
        private float timestamp;

        public bool Hidden => !this.rope.enabled;

        public TriggerPasserC passer;

        private AudioSource audio;

        // Start is called before the first frame update
        void Start()
        {
            this.audio = this.GetComponent<AudioSource>();

            this.pixelWidth = 1f / 16f;
            this.direction = 1;

            this.passer = this.GetComponentInChildren<TriggerPasserC>();
            this.passer.OnTrigger += this.Explode;

            this.explosion = this.GetComponentInChildren<Animator>();
        }

        // Update is called once per frame
        void Update()
        {
            this.Tick();
        }

        public void Tick()
        {
            this.rope.SetPosition(0, this.ropeTarget.position);

            if (Time.timeSinceLevelLoad < this.timestamp)
            {
                return;
            }

            var position = this.movable.localPosition;

            var chanceX = Random.value < 0.2;
            var chanceY = Random.value < 0.2;

            direction = -direction;

            if (chanceX)
            {
                var movement = direction * this.pixelWidth;
                this.movementBox.x = Mathf.Clamp(this.movementBox.x + movement, this.boxSize.x * -this.pixelWidth, this.boxSize.x * this.pixelWidth);
                position.x = this.movementBox.x;
                this.timestamp = Time.timeSinceLevelLoad + this.coolDown;
            }
            else if (chanceY)
            {
                var movement = direction * this.pixelWidth;
                this.movementBox.y = Mathf.Clamp(this.movementBox.y + movement, this.boxSize.y * -this.pixelWidth, this.boxSize.y * this.pixelWidth);
                position.y = this.movementBox.y;
                this.timestamp = Time.timeSinceLevelLoad + this.coolDown;
            }

            this.movable.localPosition = position;
            this.rope.SetPosition(0, this.ropeTarget.position);
        }

        public void SetRope(LineRenderer rope)
        {
            this.rope = rope;
        }

        private void Explode(GameObject gameObject)
        {
            if (gameObject.tag != "Cat")
            {
                return;
            }

            CatC cat = gameObject.GetComponent<CatC>();
            cat.BlowAway(-6f * cat.Velocity / Time.deltaTime / 50f);
            
            this.explosion.enabled = true;
            this.passer.OnAnimationDone += this.HideByEvent;
            
            this.audio.pitch = 0.9f + 0.2f * UnityEngine.Random.value;
            this.audio.Play();
        }

        private void HideByEvent(GameObject source)
        {
            this.passer.OnAnimationDone -= this.HideByEvent;
            this.Hide();
        }

        public void Hide()
        {
            this.gameObject.SetActive(false);
            this.rope.enabled = false;
        }
    }
}

