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

namespace Plugins.Animate_UI_Materials.Editor
{
  using PropertyType = ShaderUtil.ShaderPropertyType;

  internal class ShaderPropertyInfo
  {
    /// <summary>
    /// The index of the property inside the shader
    /// </summary>
    public int index;
    /// <summary>
    /// The name of the property inside the shader
    /// </summary>
    public string name;
    /// <summary>
    /// The type of the property, as defined by ShaderUtil
    /// </summary>
    public PropertyType type;

    /// <summary>
    ///   Get all properties from a shader, with their name, index, and type
    /// </summary>
    /// <param name="shader">The shader to get the properties from</param>
    /// <param name="allowHidden">Include properties marked as hidden and _MainTex</param>
    /// <returns></returns>
    public static List<ShaderPropertyInfo> GetShaderProperties(Shader shader, bool allowHidden = false)
    {
      List<ShaderPropertyInfo> properties = new List<ShaderPropertyInfo>();
      int propertyCount = ShaderUtil.GetPropertyCount(shader);

      for (int i = 0; i < propertyCount; i++)
        if (!ShaderUtil.IsShaderPropertyHidden(shader, i) || !allowHidden)
          if (!ShaderUtil.IsShaderPropertyNonModifiableTexureProperty(shader, i) || allowHidden)
            properties.Add(new ShaderPropertyInfo
            {
              index = i,
              name = ShaderUtil.GetPropertyName(shader, i),
              type = ShaderUtil.GetPropertyType(shader, i)
            });

      return properties;
    }

    /// <summary>
    ///   Get all properties from a material, with their name, index, and type
    /// </summary>
    /// <param name="material">The material to get the properties from</param>
    /// <param name="allowHidden">Include properties marked as hidden and _MainTex</param>
    /// <returns></returns>
    public static List<ShaderPropertyInfo> GetMaterialProperties(Material material, bool allowHidden = false)
    {
      if (!material) return new List<ShaderPropertyInfo>();
      return GetShaderProperties(material.shader, allowHidden);
    }
  }
}