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

public class InGameMessages : MonoBehaviour
{
    public GameObject MessageBox; // scrollView contents
    public GameObject TextPrefab; // TextPrefab
    public int maxMassages = 25;

    public Color infoColor = Color.white;
    public Color successColor = Color.green;
    public Color errorColor = Color.red;
    public Color warningColor = Color.yellow;

    [System.NonSerialized]List<Message> messageList = new List<Message>();

    public void SendMessagetoBox(string text,Message.MessageType messageType = Message.MessageType.info)
    {
        if (messageList.Count > maxMassages)
        {
            // keep mesages under maxMassages
            Destroy(messageList[0].textOBJ.gameObject);
            messageList.Remove(messageList[0]);
        }
        // add timestamp
        string date = "[" + DateTime.Now.ToString("MMdd_hh:mm:ss") + "] ";
        text = date + text;

        Message newMessage = new Message();
        newMessage.text = text;

        GameObject newText = Instantiate(TextPrefab, MessageBox.transform);
        newMessage.textOBJ = newText.GetComponent<Text>();
        newMessage.textOBJ.text = newMessage.text;
        newMessage.textOBJ.color = MessageTypeColor(messageType);

        messageList.Add(newMessage);
    }

    // parse messageType to Color
    Color MessageTypeColor(Message.MessageType messageType)
    {
        Color thisColor = infoColor;
        switch (messageType)
        {
            case Message.MessageType.info:
                thisColor = infoColor;
                break;
            case Message.MessageType.success:
                thisColor = successColor;
                break;
            case Message.MessageType.error:
                thisColor = errorColor;
                break;
            case Message.MessageType.warnning:
                thisColor = warningColor;
                break;
        }
        return thisColor;
    }
}

// Message Class
[System.Serializable]
public class Message
{
    public string text;
    public Text textOBJ;
    public MessageType messageType;

    public enum MessageType
    {
        info,
        success,
        error,
        warnning
    }
}
