using Newtonsoft.Json.Linq;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UIKF3SceneSettings : MonoBehaviour
{
    public KF3SceneContainer Container;
    public TMPro.TMP_Dropdown StageDropdown;
    public TMPro.TMP_Dropdown BackgroundDropdown;

    public static UIKF3SceneSettings Create(KF3SceneContainer container)
    {
        var panel = Instantiate(KF3ModelViewerMain.Instance.Resources.Pfb_KF3SceneSettings, KF3ModelViewerInterface.Instance.DynamicPanels);
        panel.Container = container;

        foreach (TMP_Dropdown dd in new TMP_Dropdown[] {panel.StageDropdown, panel.BackgroundDropdown})
        {
            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); });
                }
            }
        }

        return panel;
    }

    public void OnBackgroundDropdownSelected(int value)
    {
        if (value == 0)
        {
            Container.SetBackground("");
        }
        else
        {
            Container.SetBackground(BackgroundDropdown.options[value].text);
        }
        Container.SetKeyframe();
    }

    public void OnStageDropdownSelected(int value)
    {
        if (value == 0)
        {
            Container.SetStage("");
        }
        else
        {
            Container.SetStage(StageDropdown.options[value].text);
        }
        Container.SetKeyframe();
    }
}
