using System.Collections.Generic;
using gay.lilyy.MenuIconRemover;
using nadena.dev.ndmf;
using nadena.dev.ndmf.vrchat;
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.ScriptableObjects;
using static VRC.SDK3.Avatars.ScriptableObjects.VRCExpressionsMenu;

[assembly: ExportsPlugin(typeof(MenuIconRemoverPlugin))]

namespace gay.lilyy.MenuIconRemover
{
    public class MenuIconRemoverPlugin : Plugin<MenuIconRemoverPlugin>
    {
        public override string DisplayName => "MenuIconRemover";
        public override string QualifiedName => "gay.lilyy.MenuIconRemover";

        protected override void Configure()
        {
            InPhase(BuildPhase.Optimizing).BeforePlugin("gay.lilyy.MenuStyling").Run("RemoveMenuIcons", ctx =>
            {
                var obj = ctx.AvatarRootObject.GetComponent<MenuIconRemoverConfig>();
                if (obj != null)
                {
                    var shouldRemove = false;
                    if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android)
                    {
                        shouldRemove = obj.removeOnQuest;
                    }
                    else if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows ||
                             EditorUserBuildSettings.activeBuildTarget == BuildTarget.StandaloneWindows64)
                    {
                        shouldRemove = obj.removeOnPc;
                    }
                    if (shouldRemove)
                    {
                        var descriptor = ctx.VRChatAvatarDescriptor();
                        descriptor.expressionsMenu = DuplicateMenu(obj, descriptor.expressionsMenu);
                        Object.DestroyImmediate(obj);
                    }
                }
            });
        }

        private VRCExpressionsMenu DuplicateMenu(MenuIconRemoverConfig cfg, VRCExpressionsMenu menu)
        {
            if (menu == null) return null;

            var newMenu = ScriptableObject.CreateInstance<VRCExpressionsMenu>();
            newMenu.controls = new List<Control>();

            foreach (var control in menu.controls)
            {
                var shouldEdit = true;
                foreach (var exclude in cfg.exclude)
                {
                    if (control.name == exclude)
                    {
                        shouldEdit = false;
                        break;
                    }
                }
                var newControl = new Control
                {
                    name = control.name,
                    type = control.type,
                    icon = shouldEdit ? null : control.icon,
                    parameter = control.parameter,
                    subMenu = shouldEdit ? DuplicateMenu(cfg, control.subMenu) : control.subMenu,
                    value = control.value,
                    style = control.style,
                    labels = control.labels,
                    subParameters = control.subParameters
                };
                newMenu.controls.Add(newControl);
            }

            return newMenu;
        }
    }
}
