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

public class UIMessage : MonoBehaviour
{
    private float screenWidth = 1300.0f;
    private float time = 0.0f;
    public string title = "";
    public string text = "Empty message";
    public CanvasGroup background;
    public Text titleRenderer;
    public Text textRenderer;
    public bool randomHeight = false;

    public void Start() {
        time = 0.0f;
        float rand = Random.Range(3.0f, 5.0f);
        if (randomHeight) {
            transform.localPosition = new Vector3(screenWidth, Random.Range(20f, 170f), rand);
        }
        else {
            transform.localPosition = new Vector3(screenWidth, transform.localPosition.y, rand);
        }
        titleRenderer.text = title;
        textRenderer.text = text;
    }

    public void Update() {
        if (transform.localPosition.x > 0.01f) {
            transform.localPosition = new Vector3(Mathf.Lerp(transform.localPosition.x, screenWidth / 2.0f, Time.unscaledDeltaTime * 5), transform.localPosition.y, transform.localPosition.z);
        }
        if (time > 2.7 && time < 6) {
            float val = (time - 2.7f) * 30.0f;
            transform.position = new Vector3(transform.position.x - val * val, transform.position.y, transform.position.z);
            textRenderer.color = new Color(textRenderer.color.r, textRenderer.color.g, textRenderer.color.b, 0.5f - (time - 2.7f) * 2);
            background.alpha = 0.5f - (time - 2.7f);
        }
        else if (time >= 6) {
            this.enabled = false;
            GameObject.Destroy(this.gameObject);
        }
        time += Time.unscaledDeltaTime * 0.6f;
    }
}
