// Warning: Some assembly references could not be resolved automatically. This might lead to incorrect decompilation of some parts,
// for ex. property getter/setter access. To get optimal decompilation results, please manually add the missing references to the list of loaded assemblies.
// EdenAutoMorpher_ProfileSaver_Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// EdenAutoMorpher_ProfileSaverEditor
using Eden.AutoMorpher;
using System;
using System.IO;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(EdenAutoMorpher_ProfileSaver))]
public class EdenAutoMorpher_ProfileSaverEditor : Editor
{
    private EdenAutoMorpher_ProfileSaver saver;

    private ProfileUtils profileUtils;

    private string errorMessage;

    private bool hasError;

    private void OnEnable()
    {
        this.saver = (EdenAutoMorpher_ProfileSaver)base.target;
        this.profileUtils = new ProfileUtils();
    }

    public override void OnInspectorGUI()
    {
        base.serializedObject.Update();
        this.DrawProfileVersion();
        EditorGUILayout.Space(8f);
        this.DrawGuideMessage();
        EditorGUILayout.Space(10f);
        this.DrawRequiredInputFields();
        EditorGUILayout.Space(10f);
        this.DrawSavePath();
        EditorGUILayout.Space(8f);
        this.ValidateInputs();
        this.DrawErrorMessage();
        EditorGUILayout.Space(12f);
        this.DrawSaveButton();
        base.serializedObject.ApplyModifiedProperties();
    }

    private void DrawProfileVersion()
    {
        //IL_0030: Unknown result type (might be due to invalid IL or missing references)
        //IL_0035: Unknown result type (might be due to invalid IL or missing references)
        //IL_0036: Unknown result type (might be due to invalid IL or missing references)
        //IL_0037: Unknown result type (might be due to invalid IL or missing references)
        //IL_0038: Unknown result type (might be due to invalid IL or missing references)
        //IL_0056: Unknown result type (might be due to invalid IL or missing references)
        //IL_005b: Unknown result type (might be due to invalid IL or missing references)
        //IL_005c: Unknown result type (might be due to invalid IL or missing references)
        //IL_005d: Unknown result type (might be due to invalid IL or missing references)
        //IL_0060: Unknown result type (might be due to invalid IL or missing references)
        int profileVersion = this.profileUtils.GetProfileVersion();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("EDEN Auto Morpher - Profile Saver", EditorStyles.boldLabel);
        GUILayout.FlexibleSpace();
        Language currentLanguage = LanguageManager.CurrentLanguage;
        Language val = currentLanguage;
        val = (Language)(object)EditorGUILayout.EnumPopup((Enum)(object)currentLanguage, GUILayout.Width(80f));
        if (val != currentLanguage)
        {
            LanguageManager.SetLanguage(val);
            GUI.FocusControl(null);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.LabelField($"Profile Version : {profileVersion}", EditorStyles.boldLabel);
        EditorGUILayout.Space(6f);
    }

    private void DrawGuideMessage()
    {
        EditorGUILayout.HelpBox("\n" + LanguageManager.Get("UI.ProfileSaver.Guide") + "\n", MessageType.Info);
    }

    private void DrawRequiredInputFields()
    {
        EditorGUILayout.LabelField("Required Input Fields", EditorStyles.boldLabel);
        using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.Space(4f);
            EditorGUILayout.PropertyField(base.serializedObject.FindProperty("profileName"), new GUIContent("Profile Name", LanguageManager.Get("UI.ProfileSaver.ProfileName.Tooltip")));
            EditorGUILayout.Space(6f);
            EditorGUILayout.PropertyField(base.serializedObject.FindProperty("sourceAvatar"), new GUIContent("Source Avatar", LanguageManager.Get("UI.ProfileSaver.SourceAvatar.Tooltip")));
            EditorGUILayout.Space(6f);
            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(base.serializedObject.FindProperty("sourceBodyMeshes"), new GUIContent("Source Body Meshes", LanguageManager.Get("UI.ProfileSaver.SourceBodyMeshes.Tooltip")), true);
            EditorGUILayout.Space(4f);
            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
        }
    }

    private void DrawSavePath()
    {
        EditorGUILayout.LabelField("Save Path", EditorStyles.boldLabel);
        string text = Path.Combine(this.profileUtils.GetProfileBasePath(), this.saver.profileName ?? string.Empty);
        using (new EditorGUI.DisabledScope(disabled: true))
        {
            EditorGUILayout.TextField(text);
        }
    }

    private void ValidateInputs()
    {
        this.errorMessage = "\n";
        this.hasError = false;
        if (string.IsNullOrEmpty(this.saver.profileName))
        {
            this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.ProfileNameEmpty") + "\n";
            this.hasError = true;
        }
        if (this.saver.sourceAvatar == null)
        {
            this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.SourceAvatarNull") + "\n";
            this.hasError = true;
        }
        else if (this.saver.sourceAvatar.GetComponent<Animator>() == null)
        {
            this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.SourceAvatarAnimatorMissing") + "\n";
            this.hasError = true;
        }
        if (this.saver.sourceBodyMeshes == null || this.saver.sourceBodyMeshes.Count == 0)
        {
            this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.SourceBodyMeshesEmpty") + "\n";
            this.hasError = true;
        }
        else
        {
            if (this.IsBodyMeshesContainNull())
            {
                this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.SourceBodyMeshesContainNull") + "\n";
                this.hasError = true;
            }
            if (this.saver.sourceAvatar != null && this.IsBodyMeshNotChildOfAvatar())
            {
                this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.BodyMeshNotChildOfAvatar") + "\n";
                this.hasError = true;
            }
        }
        if (!this.saver.IsExistBaseData())
        {
            this.errorMessage = this.errorMessage + LanguageManager.Get("UI.ProfileSaver.Error.BaseDataMissing") + "\n";
            this.hasError = true;
        }
    }

    private bool IsBodyMeshesContainNull()
    {
        foreach (SkinnedMeshRenderer sourceBodyMesh in this.saver.sourceBodyMeshes)
        {
            if (sourceBodyMesh == null)
            {
                return true;
            }
        }
        return false;
    }

    private bool IsBodyMeshNotChildOfAvatar()
    {
        foreach (SkinnedMeshRenderer sourceBodyMesh in this.saver.sourceBodyMeshes)
        {
            if (sourceBodyMesh != null && !sourceBodyMesh.transform.IsChildOf(this.saver.sourceAvatar.transform))
            {
                return true;
            }
        }
        return false;
    }

    private void DrawErrorMessage()
    {
        if (this.hasError)
        {
            EditorGUILayout.HelpBox(this.errorMessage, MessageType.Error);
        }
    }

    private void DrawSaveButton()
    {
        using (new EditorGUI.DisabledScope(this.hasError))
        {
            if (GUILayout.Button("Save Profile", GUILayout.Height(34f)))
            {
                this.saver.SaveProfile();
            }
        }
    }
}
