using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Events;

public class SliderPanel : MonoBehaviour
{
    public static SliderPanel Prefab;

    public Slider Slider;
    //public UICallbacks ToggleCallbacks;
    public TMPro.TMP_Text Text;
    public Toggle Toggle;
    public float DefaultValue;
    public float value {
        get { return Slider.value; }
        set { Slider.value = value; }
    }
    public float maxValue
    {
        get { return Slider.maxValue; }
        set { Slider.maxValue = value; }
    }
    public float minValue
    {
        get { return Slider.minValue; }
        set { Slider.minValue = value; }
    }
    [Header("private")]
    [SerializeField] private SliderDisplay _display;
    [SerializeField] private SliderCallbackExtension _sliderCallbacks;

    private void Awake()
    {
        _display.UpdateDisplay(Slider.value);
    }

    public static SliderPanel CreateMorph(ObjectContainer container, SkinnedMeshRenderer meshRenderer, int index, string displayName, Transform parent)
    {
        SliderPanel panel = Instantiate(SharedResources.Instance.SliderPanel, parent);

        var helper = new MorphHelper(meshRenderer, meshRenderer.sharedMesh.GetBlendShapeName(index), panel.Slider);
        if (container.Morphs.ContainsKey(meshRenderer))
            container.Morphs[meshRenderer].Add(helper);
        else
            container.Morphs.Add(meshRenderer, new List<MorphHelper>() { helper });

        panel.Text.text = displayName;
        panel.Slider.value = meshRenderer.GetBlendShapeWeight(index);
        panel.SetOnValueChanged((value) => {helper.UpdateMorph(value);});
        panel.SetOnRelease((value) => { container.SetKeyframe(); });
        panel.SetOnRightClick(null);
        return panel;
    }

    public static SliderPanel CreateToggle(string label, bool isOn, Transform parent, UnityAction<bool> callback)
    {
        SliderPanel panel = Instantiate(SharedResources.Instance.TogglePanel, parent);
        panel.Slider.gameObject.SetActive(false);
        panel.Toggle.gameObject.SetActive(true);
        panel.Toggle.SetIsOnWithoutNotify(isOn);
        panel.Text.text = label;
        panel.Toggle.onValueChanged.AddListener(callback);
        return panel;
    }

    public SliderPanel SetOnValueChanged(UnityAction<float> action)
    {
        _sliderCallbacks.ValueChangedAction.RemoveAllListeners();
        if (action != null)
        {
            _sliderCallbacks.ValueChangedAction.AddListener(action);
        }
        return this;
    }

    public SliderPanel SetOnRelease(UnityAction<float> action)
    {
        _sliderCallbacks.PointerUpAction.RemoveAllListeners();
        if (action != null)
        {
            _sliderCallbacks.PointerUpAction.AddListener(action);
        }
        return this;
    }

    public SliderPanel SetOnRightClick(UnityAction action)
    {
        _sliderCallbacks.RightClickAction.RemoveAllListeners();
        _sliderCallbacks.RightClickAction.AddListener(ResetSlider);
        if(action != null)
        {
            _sliderCallbacks.RightClickAction.AddListener(action);
        }
        return this;
    }

    public void ResetSlider()
    {
        Slider.value = DefaultValue;
        _sliderCallbacks.PointerUpAction.Invoke(DefaultValue);
    }
}