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

[RequireComponent(typeof(AudioSource))]
public class TutorialController : MonoBehaviour {
    public KeyCode ToggleKey;
    public bool Toggled = true;
    public float AnimationLerpFactor = 10f;
    public AudioClip[] OpenClips;
    public AudioClip[] CloseClips;

    private RectTransform Rect;
    private AudioSource Audio;

    private void Awake() {
        Rect = GetComponent<RectTransform>();
        Audio = GetComponent<AudioSource>();
    }

    private void Start() {
        Rect.anchoredPosition = new Vector2(-100, -100);
    }

    private void Update() {
        if (Input.GetKeyDown(ToggleKey)) {
            Toggled = !Toggled;
            if (Toggled) {
                Audio.PlayOneShot(GetRandomClip(OpenClips));
            } else {
                Audio.PlayOneShot(GetRandomClip(CloseClips));
            }
        }

        Vector2 Pos = Rect.anchoredPosition;
        Pos.y = Mathf.Lerp(Pos.y, Toggled ? -100 : (Rect.rect.height + 10), AnimationLerpFactor * Time.deltaTime);
        Rect.anchoredPosition = Pos;
    }

    private AudioClip GetRandomClip(AudioClip[] clips) {
        if (clips.Length > 0) {
            return clips[Random.Range(0, clips.Length)];
        } else {
            return null;
        }
    }
}
