using System;
using System.Collections;
using System.Collections.Generic;
using GGJ24;
using UnityEditor;
using UnityEngine;

public class BalloonControllerC : MonoBehaviour
{
    [SerializeField] private Transform ropeTarget = null;
    [SerializeField] private Transform ropeContainer = null;
    [SerializeField] private Vector2 speed = Vector2.one;

    [SerializeField] private ScoreC score = null;

    private List<LineRenderer> ropes = null;
    private List<BalloonC> balloons = null;
    private float verticalDistance = 0f;

    // Start is called before the first frame update
    void Start()
    {
        this.ropes = new List<LineRenderer>(this.ropeContainer.GetComponentsInChildren<LineRenderer>());
        this.balloons = new List<BalloonC>(this.GetComponentsInChildren<BalloonC>());
        this.verticalDistance = this.transform.position.y - this.ropeTarget.position.y;
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 position = this.transform.position;
        position.x = Mathf.Lerp(position.x, this.ropeTarget.transform.position.x, this.speed.x * Time.deltaTime);
        position.y = Mathf.Lerp(position.y, this.verticalDistance + this.ropeTarget.transform.position.y, this.speed.y * Time.deltaTime);
        this.transform.position = position;

        foreach (LineRenderer r in this.ropes)
        {
            r.SetPosition(1, this.ropeTarget.position);
        }

        if (this.ropes.Find(r => r.enabled))
        {
            return;
        }

        this.score.StopScoring();
    }

    public void UseRandomBalloon()
    {
        BalloonC balloon = this.balloons.Find(b => !b.Hidden);
        balloon.Hide();

        BalloonC next = this.balloons.Find(b => !b.Hidden);
        if (next != null)
        {
            return;
        }

        this.score.StopScoring();
    }
}
