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

public class NoticeWindow : MonoBehaviour
{
    // Use this for initialization
    public static List<string> dataList;
    public static List<string> newsList;

    private static NoticeWindow m_Instance;

    public GameObject ItemParent;
    public Text m_NewsLabel;
    public Text m_TitleLabel;
    public GameObject NoticeItemObject;

    public static NoticeWindow Instance()
    {
        return m_Instance;
    }

    private void Awake()
    {
        m_Instance = this;
    }

    private void OnDestroy()
    {
        m_Instance = null;
    }

    private void Start()
    {
        InitNoticeInfo();
    }

    // Update is called once per frame
    public static void addNotice(string news, string data)
    {
        if (dataList == null) dataList = new List<string>();
        dataList.Add(data);
        if (newsList == null) newsList = new List<string>();
        newsList.Add(news);
    }

    public static void ClearNotice()
    {
        if (dataList != null) dataList.Clear();
        if (newsList != null) newsList.Clear();
    }

    private bool isNotice()
    {
        if (dataList == null || newsList == null) return false;
        if (dataList.Count != newsList.Count) return false;
        return true;
    }

    private void InitNoticeInfo()
    {
        if (isNotice() == false) return;

        if (dataList.Count >= 1) m_TitleLabel.text = dataList[0];
        if (newsList.Count >= 1)
        {
            newsList[0] = newsList[0].Replace("\\n", "\n");
            m_NewsLabel.text = newsList[0];
        }

        Vector2 _size = NoticeItemObject.GetComponent<BoxCollider>().size;
        _size.y = m_NewsLabel.rectTransform.sizeDelta.y;
        NoticeItemObject.GetComponent<BoxCollider>().size = _size;

        _size = NoticeItemObject.GetComponent<BoxCollider>().center;
        _size.y = (0 - m_NewsLabel.rectTransform.sizeDelta.y) / 2;
        NoticeItemObject.GetComponent<BoxCollider>().center = _size;
    }
}