using UnityEngine;
using UnityEngine.Events;
using TMPro;
using Shared.ScriptableVariables;

namespace Shared.SEUI {
  // UI Editor for a StringVariable
  // Depends on ScriptableVariable package
  public class StringVariableInput : ScriptableVariableInput<string> {
    [Tooltip("The variable we are editing")]
    public StringVariable variable;

    [Tooltip("The input UI to edit the variable's value with")]
    public TMP_InputField input;

    //---------------------------------------------------------------------------
    void Start() {
      // Make sure the input handles the right content type
      input.contentType = TMP_InputField.ContentType.Standard;
    }

    //---------------------------------------------------------------------------
    protected override void AddUIListener(UnityAction<string> callback) {
      input.onValueChanged.AddListener(callback);
    }

    //---------------------------------------------------------------------------
    protected override void RemoveUIListener(UnityAction<string> callback) {
      input.onValueChanged.RemoveListener(callback);
    }

    //---------------------------------------------------------------------------
    protected override bool DoValuesMatch() {
      return input.text == variable.Value;
    }

    //---------------------------------------------------------------------------
    protected override void UpdateUIValue() {
      input.text = variable.Value;
    }

    //---------------------------------------------------------------------------
    protected override void UpdateVariableValue(string value) {
      variable.Value = value;
    }
  }
}