using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameStartManager : MonoBehaviour
{
    public Vector3 babyFocusPos = new Vector3(0, 4.5f, -5);
    public Vector3 babyFace = new Vector3(0, 4.0f, -5);

    public GameObject mainMenu;

    public float cameraUpSpeed = 4f;

    public AudioSource background;

    public float babyFocusWait = 0.5f;

    public GameObject howToPlay;

    public List<KeyCode> startKeys = new List<KeyCode>()
    {
        KeyCode.LeftArrow,
        KeyCode.RightArrow,
        KeyCode.Space,
        KeyCode.A,
        KeyCode.W
    };

    // Start is called before the first frame update
    void Start()
    {
        GameState.instance.isEndGame = true; //Stop for now
        mainMenu.SetActive(false);
    }

    private bool isDone = false;
    private bool gameStarted = false;

    // Update is called once per frame
    void Update()
    {
        if (gameStarted)
        {
            //The game is on
            return;
        }

        if (isDone)
        {
            if (startKeys.Any(s => (Input.GetKeyUp(s))))
            {
                gameStarted = true;
                mainMenu.SetActive(false);
                GameState.instance.isEndGame = false;

                SoundManager.instance.StartMainSong();
                howToPlay.SetActive(true);
            }

            return;
        }

        //Gently move to focus on the baby
        //First y - then X
        if ((babyFace.y - Camera.main.transform.position.y) > 0.1f)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y + (cameraUpSpeed * Time.deltaTime), Camera.main.transform.position.z);
            return;
        }

        babyFocusWait -= Time.deltaTime;

        while (babyFocusWait > 0)
        {
            return;
        }

        //THen Z
        if ((Camera.main.transform.position.z - babyFocusPos.z) > 0.1f)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z + (cameraUpSpeed * -1 * Time.deltaTime));

            if ((babyFocusPos.y - Camera.main.transform.position.y) > 0.1f)
            {
                Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y + (cameraUpSpeed * Time.deltaTime), Camera.main.transform.position.z);
                return;
            }
            return;
        }

        //THen menu appears!
        mainMenu.SetActive(true);
        //And done!
        isDone = true;
    }
}
