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

public class Game_ending : MonoBehaviour
{
    Enemy_AI m_enemy;
    Pelaajan_hp m_pelaajan_hp;
    Pelaajan_liike m_pelaaja;

    Text m_text;

    Music_handler soundhandler;
    AudioSource mainmusic;

    // Start is called before the first frame update
    void Start() {
        m_enemy = GameObject.Find("enemy").GetComponent<Enemy_AI>();
        m_pelaajan_hp = GetComponent<Pelaajan_hp>();
        m_pelaaja = GetComponent<Pelaajan_liike>();

        m_text = GameObject.Find("Canvas").GetComponent<Text>();
        m_text.enabled = false;

        soundhandler = GetComponent<Music_handler>();
        mainmusic = GameObject.Find("Camera").GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update() {
        if (m_enemy.getEnemiesReaped() >= 1) {
            m_text.enabled = true;
            m_text.text = "YOU WIN";
            m_pelaaja.setPosition(-400,-400,-400);
            mainmusic.mute = true;
            soundhandler.loppuääni();
            StartCoroutine(alusta());
        }
        if (m_pelaajan_hp.isReaped()) {
            m_text.enabled = true;
            m_text.text = "YOU LOSE";
            m_pelaaja.setPosition(-400,-400,-400);
            mainmusic.mute = true;
            soundhandler.loppuääni();
            StartCoroutine(alusta());
        }
    }

    IEnumerator alusta() {
        yield return new WaitForSeconds(5);
        SceneManager.LoadScene("SampleScene");
    }
}