using Cinemachine;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class BubbleGumController : MonoBehaviour
{

    public CinemachineVirtualCamera cam;
    public GameObject agiz;

    public GameObject Yuz;
    SpriteRenderer frr;

    SpriteRenderer sRR;


    public AudioSource aso;
    public AudioClip clipSound;
    public AudioClip clipSound2;


    public float growSpeed = 5f; // Büyüme hızı
    public float shrinkSpeed = 5f; // Küçülme hızı
    private Vector3 originalScale; // Başlangıç boyutu
    private Vector3 originalPosition; // Başlangıç pozisyonu

    void Start()
    {
        // Nesnenin başlangıç boyutunu ve pozisyonunu kaydet
        originalScale = transform.localScale;
        originalPosition = transform.position;

        frr = Yuz.gameObject.GetComponent<SpriteRenderer>();
        sRR=this.gameObject.GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PlaySound2();
        }
        // Space tuşuna basılı tutuluyorsa büyüt
        if (Input.GetKey(KeyCode.Space))
        {
            // Yatay ve dikey eksende büyüt
            transform.localScale += new Vector3(growSpeed * Time.deltaTime, growSpeed * Time.deltaTime, 0);

            // Sol taraf sabit kalacak şekilde pozisyonu güncelle
            transform.position = new Vector3(
                originalPosition.x + (transform.localScale.x - originalScale.x) / 2,
                originalPosition.y,
                originalPosition.z
            );
        }
        else
        {
            // Space bırakıldığında küçült
            if (transform.localScale.x > originalScale.x)
            {
                transform.localScale -= new Vector3(shrinkSpeed * Time.deltaTime, shrinkSpeed * Time.deltaTime, 0);

                // Sol taraf sabit kalacak şekilde pozisyonu güncelle
                if (transform.localScale.x < originalScale.x)
                {
                    transform.localScale = originalScale;
                }

                transform.position = new Vector3(
                    originalPosition.x + (transform.localScale.x - originalScale.x) / 2,
                    originalPosition.y,
                    originalPosition.z
                );
            }
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        

        if (other.gameObject.CompareTag("igne") || other.gameObject.CompareTag("parmak"))
        {
            sRR.enabled=false;  
            PlaySound();
            StartCoroutine(Gecis());
        }

    }
    public void PlaySound()
    {
        if (clipSound != null)
        {
            aso.clip = clipSound;
            aso.Play();
        }
    }
    public void PlaySound2()
    {
        if (clipSound2 != null)
        {
            aso.clip = clipSound2;
            aso.Play();
        }
    }

    IEnumerator Gecis()
    {
        ChangeLookAt(agiz.transform);
        frr.enabled = false;
        Debug.Log("kame");

        yield return new WaitForSeconds(2f);
        
        Debug.Log("sahne değişti");
        SceneManager.LoadScene(3);
    }

    public void ChangeLookAt(Transform newTarget)
    {
        cam.LookAt = newTarget;
        cam.Follow = newTarget;
    }
}
