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

public class UIKF3MorphPanel : MonoBehaviour
{
    public Transform
        EyesPanel,
        EyebrowsPanel,
        MouthPanel;

    public ToggleGroup EyesToggleGroup;

    public static UIKF3MorphPanel Create(KF3FriendContainer container)
    {
        var main = KF3ModelViewerMain.Instance;
        var morphPanel = Instantiate(main.Resources.Pfb_KF3MorphPanel, main.UI.DynamicPanels);
        //morphPanel.Init(container);
        return morphPanel;
    }

    //public void Init(KF3FriendContainer container)
    //{
    //    foreach (Transform child in container.GetComponentsInChildren<Transform>())
    //    {
    //        GameObject go = null;
    //        switch (child.name)
    //        {
    //            case "md_eye_base":
    //                {
    //                    go = Instantiate(KF3ModelViewerInterface.Instance.Pfb_MorphSliderToggle, EyesPanel);
    //                    go.GetChild<Text>().text = "Look At Camera";
    //                    go.Get<Slider>().wholeNumbers = false;
    //                    go.Get<Slider>().minValue = 0;
    //                    go.Get<Slider>().maxValue = 1;
    //                    go.Get<Slider>().value = container.LookAtStrength;
    //                    go.Get<Slider>().onValueChanged.AddListener((value) => { container.UpdateLookAtTarget(value); });
    //                    go.GetChild<Toggle>().isOn = container.EyesLocked;
    //                    go.GetChild<Toggle>().onValueChanged.AddListener((val) => container.EyesLocked = val);

    //                    SkinnedMeshRenderer meshRenderer = child.GetComponent<SkinnedMeshRenderer>();
    //                    if (meshRenderer == null) break;
    //                    if (meshRenderer.sharedMesh == null) break;
    //                    for (int i = 0; i < meshRenderer.sharedMesh.blendShapeCount; i++)
    //                    {
    //                        CreateMorph(meshRenderer, EyesPanel.transform, i, "BS_eye.eye_");
    //                    }

    //                    if (HumanBones.ContainsKey(HumanBodyBones.Head) && HumanBones[HumanBodyBones.Head] != null && !EyeTarget)
    //                    {
    //                        Transform head = HumanBones[HumanBodyBones.Head];
    //                        EyeTarget = (UIHandleEyes)new GameObject().AddComponent<UIHandleEyes>()
    //                            .Init(this)
    //                            .SetScale(2)
    //                            .SetColor(Color.red);
    //                        EyeTarget.name = $"Eyes_{ModelId}";
    //                        EyeTarget.transform.localScale = 0.1f * Vector3.one;
    //                        EyeTarget.transform.SetParent(head);
    //                        EyeTarget.transform.position = head.position + head.forward;
    //                    }
    //                    goto case "md_eye_special_a";
    //                }
    //            case "md_eye_special_a":
    //            case "md_eye_special_b":
    //                {
    //                    go = Instantiate(KF3ModelViewerInterface.EyeTogglePrefab, EyesMorphsPanel.transform);
    //                    go.GetChild<Text>().text = child.name;
    //                    go.Get<Toggle>().isOn = child.gameObject.activeSelf;
    //                    go.Get<Toggle>().onValueChanged.AddListener((value) => { ToggleVisible(child.gameObject, value); });
    //                    InstantiatedObjects.Add(go);
    //                    break;
    //                }
    //            case "md_mouth_base":
    //                {
    //                    SkinnedMeshRenderer meshRenderer = child.GetComponent<SkinnedMeshRenderer>();
    //                    if (meshRenderer == null) break;
    //                    if (meshRenderer.sharedMesh == null) break;
    //                    for (int i = 0; i < meshRenderer.sharedMesh.blendShapeCount; i++)
    //                    {
    //                        CreateMorph(meshRenderer, MouthMorphsPanel.transform, i, "BS_mouth.mouth_");
    //                    }
    //                    break;
    //                }
    //            case "md_eye_l": LeftEye = child; break;
    //            case "md_eye_r": RightEye = child; break;
    //            case "md_brow_base":
    //                {
    //                    SkinnedMeshRenderer meshRenderer = child.GetComponent<SkinnedMeshRenderer>();
    //                    if (meshRenderer == null) break;
    //                    if (meshRenderer.sharedMesh == null) break;

    //                    for (int i = 0; i < meshRenderer.sharedMesh.blendShapeCount; i++)
    //                    {
    //                        CreateMorph(meshRenderer, EyebrowsMorphsPanel.transform, i, "BS_brow.brow_");
    //                    }

    //                    break;
    //                }
    //            case "md_subweapon":
    //                {
    //                    SubWeapon = child;
    //                    if (SubWeaponBone == null) break;
    //                    SubWeapon.SetParent(SubWeaponBone);
    //                    SubWeapon.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
    //                    break;
    //                }
    //            case "subweapon":
    //                {
    //                    SubWeaponBone = child;
    //                    if (SubWeapon == null) break;
    //                    SubWeapon.SetParent(SubWeaponBone);
    //                    SubWeapon.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
    //                    break;
    //                }
    //        }
    //    }
    //}

    public void ResetChildMorphs(Transform self)
    {
        foreach(var helper in self.GetComponentsInChildren<MorphHelper>())
        {
            helper.Slider.value = 0;
        }
    }

    //public void PasteValues(KF3MorphHelper[] helpers)
    //{

    //    foreach (var morph in keyframe.Morphs)
    //    {
    //        var rend = this.Morphs.Keys.FirstOrDefault(k => k.name == morph.ParentName);
    //        if (rend == null) continue;

    //        KF3MorphHelper h = this.Morphs[rend].FirstOrDefault(m => m.Name == morph.Name);
    //        if (h != null) this.UpdateMorph(rend, rend.sharedMesh.GetBlendShapeIndex(morph.Name), morph.Strength, h.Slider);
    //    }
    //}
}