using System;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

[Serializable]
public class MorphHelper
{
    public string Name;
    [DefaultValue("")]
    public string ParentName;
    public float Strength;
    public bool Value;
    [NonSerialized]
    public SkinnedMeshRenderer Renderer;
    [NonSerialized]
    public Slider Slider;

    public MorphHelper() { }

    public MorphHelper(SkinnedMeshRenderer parent, string name, Slider slider = null)
    {
        this.ParentName = parent.name;
        this.Name = name;
        this.Renderer = parent;
        this.Slider = slider;
    }

    /// <summary> Used for serialization! </summary>
    public MorphHelper(string parent, string name, float strength, bool value, Slider slider = null)
    {
        this.ParentName = parent;
        this.Name = name;
        this.Strength = strength;
        this.Value = value;
        this.Slider = slider;
    }

    public void SetSlider(Slider slider)
    {
        this.Slider = slider;
    }

    public void UpdateMorph(float value)
    {
        var id = Renderer.sharedMesh.GetBlendShapeIndex(Name);
        Renderer.SetBlendShapeWeight(id, value);
        Strength = value;
        if (Slider)
            Slider.SetValueWithoutNotify(value);
    }
}
