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

public class UISettingsPanel : MonoBehaviour
{
    public Transform Content;

    private void Start()
    {
        ModelViewerSettings.OnSettingsSaved += UpdateSettings;
    }

    private void OnEnable()
    {
        if(ModelViewerSettings.Instance != null)
        {
            UpdateSettings(ModelViewerSettings.Instance);
        }
    }

    public void UpdateSettings(ModelViewerSettings settings)
    {
        Debug.Log("Updating");

        for(int i = Content.childCount -1; i >= 0; i--)
        {
            Destroy(Content.GetChild(i).gameObject);
        }

        foreach(var entry in settings.Settings)
        {
            var setting = entry.Value;
            var inputMode = setting.Type == ModelViewerSettings.SettingType.FilePath ? UITextInput.InputMode.FilePicker
                : setting.Type == ModelViewerSettings.SettingType.FolderPath ? UITextInput.InputMode.FolderPicker
                : UITextInput.InputMode.Text;
            UITextInput.Create(entry.Key, setting.Value, setting.Editable, Content, inputMode).SetOnTextChanged((value) =>
            {
                ModelViewerSettings.Set(entry.Key, value, true);
            });
        }
    }
}
