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

public class ConsolePuzzle : MonoBehaviour
{
    [SerializeField]
    private Canvas canvas;

    [SerializeField] private string unlockSequence;

    private string enteredSequence = "";

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void TestClick(string val)
    {
        Debug.Log("Clicked");
        enteredSequence = enteredSequence + val;

        if (unlockSequence.Substring(0, enteredSequence.Length) != enteredSequence)
        {
            // failure event
            enteredSequence = "";
            return;
        }

        if (enteredSequence.Length == unlockSequence.Length)
        {
            // success;
            Debug.Log("success");
        }
    }

    public void TriggerConsoleOpen()
    {
        StartCoroutine(ScaleCanvas(1, 0, 2));
    }

    public void TriggerConsoleClose()
    {
        StartCoroutine(ScaleCanvas(0, 1, 2));
    }

    public IEnumerator ScaleCanvas(float targetScale, float startScale, float time)
    {
        float elapsedTime = 0;
        canvas.transform.localScale = new Vector3(canvas.transform.localScale.x, startScale, canvas.transform.localScale.z);

        while (elapsedTime < time)
        {
            canvas.transform.localScale = new Vector3(canvas.transform.localScale.x, Mathf.SmoothStep(startScale, targetScale, elapsedTime), canvas.transform.localScale.z);
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }
    }
}
