using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts
{
    class LogWriter : MonoBehaviour
    {
        GameObject img;
        GameObject txt;
        List<string> messages;
        bool isDisplaying = false;
        DateTime call;

        void Start()
        {
            img = GameObject.Find("MessageImage").gameObject;
            txt = GameObject.Find("MessageText").gameObject;
            messages = new List<string>();
        }

        void Update()
        {
            if (messages.Count > 0 && GameObject.Find("Fight").transform.localScale.Equals(new Vector3(0, 0, 0)))
            {
                if (!isDisplaying)
                {
                    writeMessage();
                }
                else
                {
                    hideMessage();
                }
            }
        }

        public void addMessage(string message)
        {
            messages.Add(message);
        }

        public void writeMessage()
        {
            switch (messages[0].Split(';')[0])
            {
                case "ERROR":
                    img.GetComponent<RawImage>().color = Color.red;
                    break;
                case "INFORMATION":
                    img.GetComponent<RawImage>().color = Color.black;
                    break;
                case "SUCCESS":
                    img.GetComponent<RawImage>().color = Color.green;
                    break;
                case "WARNING":
                    img.GetComponent<RawImage>().color = Color.yellow;
                    break;
            }

            txt.GetComponent<Text>().text = messages[0].Split(';')[1];
            gameObject.transform.localScale = new Vector3(1, 1, 1);
            isDisplaying = true;
            call = DateTime.Now;
        }

        private void hideMessage()
        {
            if (call.AddSeconds(2).CompareTo(DateTime.Now) <= 0)
            {
                gameObject.transform.localScale = new Vector3(0, 0, 0);
                isDisplaying = false;
                messages.Remove(messages[0]);
            }
        }
    }
}
