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


public class acilma : MonoBehaviour
{
    public AudioSource aso;
    public AudioClip music;

    public Image fadeImage; // UI Image (Canvas üzerindeki Image)
    public float fadeDuration = 1f; // Fade süresi (saniye)
    public float mfadeDuration = 1f;

    private void Start()
    {
        aso=GetComponent<AudioSource>();

        StartCoroutine(FadeOut());
        StartCoroutine(FadeIn());
        StartCoroutine(mFadeOutMusic());

        PlaySound();
    }
    void Update()
    {
        if (Input.anyKeyDown)
        {
            SceneManager.LoadScene(1);
        }
    }

    private IEnumerator FadeOut()
    {
        float elapsedTime = 0f;
        Color color = fadeImage.color;

        while (elapsedTime < fadeDuration)
        {
            elapsedTime += Time.deltaTime;
            color.a = Mathf.Clamp01(elapsedTime / fadeDuration); // Alfa değerini artır
            fadeImage.color = color;
            yield return null;
        }

        // Alfa değerini tamamen 1 yap (tamamen görünür hale getir)
        color.a = 1;
        fadeImage.color = color;
    }
    private IEnumerator mFadeOutMusic()
    {
        yield return new WaitForSeconds(28f);
        float startVolume = aso.volume; // Mevcut ses seviyesi
        float elapsedTime = 0f;

        while (elapsedTime < mfadeDuration)
        {
            elapsedTime += Time.deltaTime;
            aso.volume = Mathf.Lerp(startVolume, 0f, elapsedTime / mfadeDuration); // Volume'yi azalt
            yield return null;
        }

        aso.volume = 0f; // Volume'yi tamamen 0 yap
        aso.Stop(); // Sesi durdur (isteğe bağlı)
    }

    private IEnumerator FadeIn()
    {
        yield return new WaitForSeconds(10f);
        float elapsedTime = 0f;
        Color color = fadeImage.color;

        while (elapsedTime < fadeDuration)
        {
            elapsedTime += Time.deltaTime;
            color.a = 1 - Mathf.Clamp01(elapsedTime / fadeDuration); // Alfa değerini düşür
            fadeImage.color = color;
            yield return null;
        }

        // Alfa değerini tamamen sıfır yap (görünmez hale getir)
        color.a = 0;
        fadeImage.color = color;
    }


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