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

public class InputToPress : MonoBehaviour
{
	public KeyCode buttonToPress;
	public bool neverStop = false;
	public float timer = 0.0f;
	public Sprite buttonImage;
	public Sprite buttonImageAlt;

	public GameObject button;
	public GameObject buttonAlt;
	public GameObject bar;
	public GameObject fill;

	LevelManager levelManager;
	Vector3 barPosition;
	Vector2 barSize;
	float initialTimer;

	// Start is called before the first frame update
	void Start()
    {
		levelManager = FindObjectOfType<LevelManager>();
		barPosition = bar.transform.position;
		barSize = bar.GetComponent<RectTransform>().sizeDelta;
	}

    // Update is called once per frame
    void Update()
    {
        if(timer > 0 && !levelManager.QTEbuttonPressed) {
			float fillSizeX = barSize.x * timer / initialTimer - 2;
			fill.GetComponent<RectTransform>().sizeDelta = new Vector2(fillSizeX, barSize.y-2);
			fill.transform.position = new Vector3(barPosition.x - (barSize.x - fillSizeX) / 2 + 1, barPosition.y, 0);
			timer -= Time.deltaTime;
		} else if(!neverStop) {
			button.SetActive(false);
			buttonAlt.SetActive(false);
			bar.SetActive(false);
		}
    }

	public void Activate(KeyCode buttonToPress, float timer = 0.0f) {
		button.SetActive(true);
		//button.GetComponentInChildren<Text>().text = buttonToPress.ToString();
		button.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI Gif/" + buttonToPress.ToString());
		bar.SetActive(true);
		fill.GetComponent<RectTransform>().sizeDelta = bar.GetComponent<RectTransform>().sizeDelta;
		this.buttonToPress = buttonToPress;
		this.timer = timer;
		initialTimer = timer;
	}

	public void Activate(KeyCode buttonToPress) {
		this.buttonToPress = buttonToPress;
		button.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI Gif/" + buttonToPress.ToString());
		button.SetActive(true);
		neverStop = true;
	}

	public void Deactivate() {
		neverStop = false;
	}
}
