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

public class UIKF3CharacterPanel : MonoBehaviour
{
    public KF3CharacterContainer Container;

    public TMPro.TMP_Dropdown
        AnimationSetDropdown,
        BodyAnimationDropdown,
        CostumeDropdown,
        EarsAnimationDropdown,
        TailAnimationDropdown;

    public ScrollRect WeaponToggle;

    private static KF3ModelViewerMain Main => KF3ModelViewerMain.Instance;

    public static UIKF3CharacterPanel Create(KF3CharacterContainer container)
    {
        var panel = Instantiate(Main.Resources.Pfb_KF3CharacterPanel, KF3ModelViewerInterface.Instance.DynamicPanels);
        panel.Container = container;
        panel.Init();
        panel.UpdateSelection();

        return panel;
    }

    public void Init()
    {
        if (Container as KF3FriendContainer != null)
        {
            var friend = Container as KF3FriendContainer;
            KF3ModelViewerInterface.SetDropdownData(CostumeDropdown, Main.Assets.GetCostumesFromId(Container.ModelId).Select(c => KF3Names.GetDressName(c.DisplayName.Split('_', '.')[2])).ToList());
            KF3ModelViewerInterface.SetDropdownData(AnimationSetDropdown, Main.Assets.AnimationSets.Select(a => a.DisplayName).ToList());
            KF3ModelViewerInterface.SetDropdownData(BodyAnimationDropdown, Container.BodyAnimation.prefabAnimeList.Where(anim => anim.clip != null).Select(kf => kf.name).ToList(), true, true);
            KF3ModelViewerInterface.SetDropdownData(EarsAnimationDropdown, friend.EarsAnimation == null ? null : friend.EarsAnimation.prefabAnimeList.Where(anim => anim.clip != null).Select(kf => kf.name).ToList(), true, true);
            KF3ModelViewerInterface.SetDropdownData(TailAnimationDropdown, friend.TailAnimation == null ? null : friend.TailAnimation.prefabAnimeList.Where(anim => anim.clip != null).Select(kf => kf.name).ToList(), true, true);

            foreach (Renderer r in friend.Renderers)
            {
                UIKF3MorphSlider go = Instantiate(Main.Resources.Pfb_KF3MorphToggle, WeaponToggle.content);
                go.Text.text = KF3CharacterContainerSerializable.GetRendererName(friend, r);
                go.Toggle.isOn = r.enabled;
                go.Toggle.onValueChanged.AddListener((value) => { friend.ToggleRendererVisible(r, value); friend.SetKeyframe(); });
            }
        }
        else
        {
            var friend = Container as KF3CeruleanContainer;
            KF3ModelViewerInterface.SetDropdownData(CostumeDropdown, Main.Assets.GetCeruleans(KF3Names.GetEnemyName(friend.ModelId)).Select(m => m.FileName).ToList());
            KF3ModelViewerInterface.SetDropdownData(AnimationSetDropdown, null);
            KF3ModelViewerInterface.SetDropdownData(BodyAnimationDropdown, friend.BodyAnimation.prefabAnimeList.Where(anim => anim.clip != null).Select(kf => kf.name).ToList(), true, true);
            KF3ModelViewerInterface.SetDropdownData(EarsAnimationDropdown, null);
            KF3ModelViewerInterface.SetDropdownData(TailAnimationDropdown, null);
        }

        foreach (var dd in new TMPro.TMP_Dropdown[] { AnimationSetDropdown, BodyAnimationDropdown, CostumeDropdown, EarsAnimationDropdown, TailAnimationDropdown })
        {
            foreach (Transform t in dd.transform)
            {
                if (t.name == "LeftButton")
                {
                    t.gameObject.GetComponent<Button>().onClick.AddListener(() => { ModelViewerInterface.DropdownPrevious(dd); });
                }
                else if (t.name == "RightButton")
                {
                    t.gameObject.GetComponent<Button>().onClick.AddListener(() => { ModelViewerInterface.DropdownNext(dd); });
                }
            }
        }
    }

    public void UpdateSelection()
    {
        string currentAnimation = Container.CurrentAnimation;

        if (currentAnimation == "")
        {
            BodyAnimationDropdown.SetValueWithoutNotify(0);
        }
        else
        {
            int index = BodyAnimationDropdown.options.IndexOf(BodyAnimationDropdown.options.FirstOrDefault(o => o.text == currentAnimation));
            BodyAnimationDropdown.SetValueWithoutNotify(index);
        }

        if (Container as KF3FriendContainer != null)
        {
            CostumeDropdown.SetValueWithoutNotify(CostumeDropdown.options.IndexOf(CostumeDropdown.options.First(o => o.text == KF3Names.GetDressName(Container.CostumeName.Split('_', '.')[2]))));
            EarsAnimationDropdown.SetValueWithoutNotify(0);
            TailAnimationDropdown.SetValueWithoutNotify(0);
            AnimationSetDropdown.SetValueWithoutNotify(Main.Assets.AnimationSets.IndexOf(Main.Assets.GetAnimationSet(Container.AnimationName)));
        }
        else
        {
            CostumeDropdown.SetValueWithoutNotify(CostumeDropdown.options.IndexOf(CostumeDropdown.options.FirstOrDefault(o => o.text == Container.CostumeName)));
        }
    }

    public void OnAnimationSetDropdownSelected(int index)
    {
        //if (index == 0) return;
        KF3AssetBundle animations = Main.Assets.GetAnimationSet(AnimationSetDropdown.options[index].text);
        List<KF3AssetBundle> costumes = Main.Assets.GetCostumesFromId(Main.SelectedObject.ModelId);

        Main.StartCoroutine(Main.Builder.UpdateCharacter(Container, costumes[CostumeDropdown.value].FileName, animations.FileName));
    }

    public void OnCostumeDropdownSelected(int index)
    {
        if (Container as KF3FriendContainer != null)
        {
            List<KF3AssetBundle> costumes = Main.Assets.GetCostumesFromId(Main.SelectedObject.ModelId);
            KF3AssetBundle animation = Main.Assets.GetAnimationSet(AnimationSetDropdown.options[AnimationSetDropdown.value].text);
            Main.StartCoroutine(Main.Builder.UpdateCharacter(Container, costumes[index].FileName, animation.FileName));
        }
        else
        {
            //var cells = Main.Assets.GetCeruleans(KF3Names.GetEnemyName(Main.SelectedObject.ModelId));
            //var t = Main.SelectedObject.transform;
            //var pos = t.position;
            //var rot = t.rotation;
            //var siz = t.localScale;
            //Destroy(Main.SelectedObject);
            //StartCoroutine(Main.ModelBuilder.BuildCerulean(cells[index].FileName, cells[0].FileName, (go) =>
            //{
            //    t = go.transform;
            //    t.position = pos;
            //    t.rotation = rot;
            //    t.localScale = siz;
            //}));
        }
    }

    public void OnBodyAnimationDropdownSelected(int index)
    {
        Debug.Log("Click");
        if (index == 0)
        {
            Container.CurrentAnimation = "";
        }
        else
        {
            Container.CurrentAnimation = BodyAnimationDropdown.options[index].text;
        }
        Container.SetKeyframe();
        TimelineController.SetCurrentFrame(Container);
        Container.SetKeyframe();
    }

    public void OnEarsAnimationDropdownSelected(int index)
    {
        var friend = Container as KF3FriendContainer;
        if (friend == null) return;
        if (index == 0)
        {
            friend.PlayEarsAnimation("");
            return;
        }
        friend.PlayEarsAnimation(EarsAnimationDropdown.options[index].text);
    }

    public void OnTailAnimationDropdownSelected(int index)
    {
        var friend = Container as KF3FriendContainer;
        if (friend == null) return;
        if (index == 0)
        {
            friend.PlayTailAnimation("");
            return;
        }
        friend.PlayTailAnimation(TailAnimationDropdown.options[index].text);
    }
}
