using System.Text;
using UnityEngine;
using UnityEngine.UIElements;

namespace RebootKit.Engine.Services.Console {
    public class ConsoleUI : MonoBehaviour {
        private StringBuilder _content = new();

        [SerializeField]
        private UIDocument _document;

        private TextField _textField;
        private Button _submitButton;

        public bool IsVisible { get; private set; }

        private void Awake() {
            
            VisualElement root = _document.rootVisualElement;
            return;
            // _textField = root.Q<TextField>("console-text-field");
            // _submitButton = root.Q<Button>("console-btn-submit");
            // _submitButton.clicked += OnSubmitButtonClicked;
        }

        private void OnSubmit(string input) {
            RR.Console().Execute(input);
            _textField.value = input;
        }

        public void SetVisibility(bool visible) {
            _document.enabled = visible;
        }

        public void Write(string message) {
            _content.Append(message);
            //_textField.value = _content.ToString();
        }

        public void Clear() {
            _content.Clear();

            if (_textField != null) {
                _textField.value = "";
            }
        }

        private void OnSubmitButtonClicked() {
            OnSubmit(_textField.value);
        }
    }
}