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

public class LogPanelScript : MonoBehaviour {
    [SerializeField] Button showHidePanel;
    [SerializeField] GameObject logRowPrefab;
    [SerializeField] RectTransform content;

    private int baseRowHeight = 20;

    void Start() {
        showHidePanel.onClick.AddListener(ShowHidePanelToggle);
    }

    private void ShowHidePanelToggle() {
        RectTransform rectTransform = this.GetComponent<RectTransform>();
        if (rectTransform.sizeDelta.x > 50) { // minimize
            rectTransform.sizeDelta = new Vector2(40, rectTransform.sizeDelta.y);
        } else { // maximize
            rectTransform.sizeDelta = new Vector2(300, rectTransform.sizeDelta.y);
        }
    }

    public void AddText(string text) {
        GameObject newLogRow = GameObject.Instantiate(logRowPrefab, new Vector3(0, 0, 0), Quaternion.identity);
        TMPro.TMP_Text logText = newLogRow.GetComponent<TMPro.TMP_Text>();
        RectTransform rectTransform = GetComponent<RectTransform>();
        rectTransform.localScale = new Vector3(1, 1, 1);
        newLogRow.transform.SetParent(content.transform);
        logText.text = text;
        int lineCount = logText.textInfo.lineCount;

        rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, baseRowHeight * lineCount);

    }
}
