// 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.
// EdenAutoMorpherEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.AutoMorpherValidator
using Eden.AutoMorpher;
using System.Text;
using UnityEngine;

public class AutoMorpherValidator
{
    public bool ValidateAutoModeObjects(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        return this.ValidateAutoModeObjects_Internal(sourceAvatar, sourceClothes, targetAvatar, out errorMessage);
    }

    public bool ValidateManualMode_AutoSetup_Objects(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        return this.ValidateManualMode_AutoSetup_Objects_Internal(sourceAvatar, sourceClothes, targetAvatar, out errorMessage);
    }

    public bool ValidateManualModeObjects(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, GameObject targetClothes, out string errorMessage)
    {
        return this.ValidateManualModeObjects_Internal(sourceAvatar, sourceClothes, targetAvatar, targetClothes, out errorMessage);
    }

    public bool ValidateProfileModeObjects(GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        return this.ValidateProfileModeObjects_Internal(sourceClothes, targetAvatar, out errorMessage);
    }

    public bool ValidateAutoModeObjects_Internal(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        StringBuilder stringBuilder = new StringBuilder();
        this.ObjectNullCheck(stringBuilder, (sourceAvatar, "- Source Avatar Object"), (sourceClothes, "- Source Clothes Object"), (targetAvatar, "- Target Avatar Object"));
        if (sourceClothes != null)
        {
            this.ClothesChildCheck(stringBuilder, sourceAvatar, sourceClothes, LanguageManager.Get("UI.Validator.SourceClothesChildCheck"));
            this.HasSMRInClothes(stringBuilder, sourceClothes, "ClothesObject - There is No SkinnedMeshRenderer");
            this.HasLocalArmature(stringBuilder, sourceClothes, "Source " + LanguageManager.Get("UI.Validator.ClothesArmatureCheck"));
        }
        if (sourceAvatar != null)
        {
            this.IsHumanoid(stringBuilder, sourceAvatar, LanguageManager.Get("UI.Validator.SourceAvatarAnimatorCheck"));
        }
        if (targetAvatar != null)
        {
            this.IsHumanoid(stringBuilder, targetAvatar, LanguageManager.Get("UI.Validator.TargetAvatarAnimatorCheck"));
        }
        if (stringBuilder.Length == 0)
        {
            errorMessage = null;
            return true;
        }
        errorMessage = "Auto Mode: " + LanguageManager.Get("UI.Validator.Can'tFitting") + stringBuilder.ToString();
        return false;
    }

    public bool ValidateProfileModeObjects_Internal(GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        StringBuilder stringBuilder = new StringBuilder();
        this.ObjectNullCheck(stringBuilder, (sourceClothes, "- Source Clothes Object"), (targetAvatar, "- Target Avatar Object"));
        if (sourceClothes != null)
        {
            this.HasSMRInClothes(stringBuilder, sourceClothes, "ClothesObject - There is No SkinnedMeshRenderer");
            this.HasLocalArmature(stringBuilder, sourceClothes, "Source " + LanguageManager.Get("UI.Validator.ClothesArmatureCheck"));
        }
        if (targetAvatar != null)
        {
            this.IsHumanoid(stringBuilder, targetAvatar, LanguageManager.Get("UI.Validator.TargetAvatarAnimatorCheck"));
        }
        if (stringBuilder.Length == 0)
        {
            errorMessage = null;
            return true;
        }
        errorMessage = "Profile Mode: " + LanguageManager.Get("UI.Validator.Can'tFitting") + stringBuilder.ToString();
        return false;
    }

    public bool ValidateManualMode_AutoSetup_Objects_Internal(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, out string errorMessage)
    {
        StringBuilder stringBuilder = new StringBuilder();
        this.ObjectNullCheck(stringBuilder, (sourceAvatar, "- Source Avatar Object"), (sourceClothes, "- Source Clothes Object"), (targetAvatar, "- Target Avatar Object"));
        if (sourceClothes != null)
        {
            this.ClothesChildCheck(stringBuilder, sourceAvatar, sourceClothes, LanguageManager.Get("UI.Validator.SourceClothesChildCheck"));
            this.HasSMRInClothes(stringBuilder, sourceClothes, "ClothesObject - There is No SkinnedMeshRenderer");
            this.HasLocalArmature(stringBuilder, sourceClothes, "Source " + LanguageManager.Get("UI.Validator.ClothesArmatureCheck"));
        }
        if (sourceAvatar != null)
        {
            this.IsHumanoid(stringBuilder, sourceAvatar, LanguageManager.Get("UI.Validator.SourceAvatarAnimatorCheck"));
        }
        if (targetAvatar != null)
        {
            this.IsHumanoid(stringBuilder, targetAvatar, LanguageManager.Get("UI.Validator.TargetAvatarAnimatorCheck"));
        }
        if (stringBuilder.Length == 0)
        {
            errorMessage = null;
            return true;
        }
        errorMessage = "Manual Mode - Auto Setup: " + LanguageManager.Get("UI.Validator.Can'tFitting") + stringBuilder.ToString();
        return false;
    }

    private bool ValidateManualModeObjects_Internal(GameObject sourceAvatar, GameObject sourceClothes, GameObject targetAvatar, GameObject targetClothes, out string errorMessage)
    {
        StringBuilder stringBuilder = new StringBuilder();
        this.ObjectNullCheck(stringBuilder, (sourceAvatar, "- Source Avatar Object"), (sourceClothes, "- Source Clothes Object"), (targetAvatar, "- Target Avatar Object"), (targetClothes, "- Target Clothes Object"));
        if (sourceClothes != null)
        {
            this.ClothesChildCheck(stringBuilder, sourceAvatar, sourceClothes, LanguageManager.Get("UI.Validator.SourceClothesChildCheck"));
            this.HasSMRInClothes(stringBuilder, sourceClothes, "ClothesObject - There is No SkinnedMeshRenderer");
            this.HasLocalArmature(stringBuilder, sourceClothes, "Source " + LanguageManager.Get("UI.Validator.ClothesArmatureCheck"));
        }
        if (targetClothes != null)
        {
            this.ClothesChildCheck(stringBuilder, targetAvatar, targetClothes, LanguageManager.Get("UI.Validator.TargetClothesChildCheck"));
            this.HasSMRInClothes(stringBuilder, targetClothes, "ClothesObject - There is No SkinnedMeshRenderer");
            this.HasLocalArmature(stringBuilder, targetClothes, "Target " + LanguageManager.Get("UI.Validator.ClothesArmatureCheck"));
        }
        if (sourceAvatar != null)
        {
            this.IsHumanoid(stringBuilder, sourceAvatar, LanguageManager.Get("UI.Validator.SourceAvatarAnimatorCheck"));
        }
        if (targetAvatar != null)
        {
            this.IsHumanoid(stringBuilder, targetAvatar, LanguageManager.Get("UI.Validator.TargetAvatarAnimatorCheck"));
        }
        if (sourceClothes != null && targetClothes != null)
        {
            this.AppendSmrAndVertexCheck(stringBuilder, sourceClothes, targetClothes);
        }
        if (stringBuilder.Length == 0)
        {
            errorMessage = null;
            return true;
        }
        errorMessage = "Manual Mode: " + LanguageManager.Get("UI.Validator.Can'tFitting") + stringBuilder.ToString();
        return false;
    }

    private void ObjectNullCheck(StringBuilder sb, params (GameObject obj, string label)[] refs)
    {
        bool flag = false;
        for (int i = 0; i < refs.Length; i++)
        {
            if (refs[i].obj == null)
            {
                flag = true;
                break;
            }
        }
        if (!flag)
        {
            return;
        }
        sb.AppendLine(LanguageManager.Get("UI.Validator.ReferenceCheck"));
        for (int j = 0; j < refs.Length; j++)
        {
            if (refs[j].obj == null)
            {
                sb.AppendLine(refs[j].label);
            }
        }
    }

    private void ClothesChildCheck(StringBuilder sb, GameObject parentObject, GameObject childObject, string messageOnInvalid)
    {
        if (!(childObject == null) && (parentObject == null || !childObject.transform.IsChildOf(parentObject.transform)))
        {
            if (sb.Length > 0)
            {
                sb.AppendLine();
            }
            sb.AppendLine(messageOnInvalid);
        }
    }

    private void HasSMRInClothes(StringBuilder sb, GameObject clothesRoot, string messageOnInvalid)
    {
        if (clothesRoot == null)
        {
            return;
        }
        SkinnedMeshRenderer[] componentsInChildren = clothesRoot.GetComponentsInChildren<SkinnedMeshRenderer>(includeInactive: true);
        if (componentsInChildren == null || componentsInChildren.Length == 0)
        {
            if (sb.Length > 0)
            {
                sb.AppendLine();
            }
            sb.AppendLine(messageOnInvalid);
        }
    }

    private void HasLocalArmature(StringBuilder sb, GameObject clothesRoot, string messageOnInvalid)
    {
        if (clothesRoot == null)
        {
            return;
        }
        SkinnedMeshRenderer[] componentsInChildren = clothesRoot.GetComponentsInChildren<SkinnedMeshRenderer>(includeInactive: true);
        if (componentsInChildren == null || componentsInChildren.Length == 0)
        {
            return;
        }
        Transform transform = clothesRoot.transform;
        bool flag = true;
        SkinnedMeshRenderer[] array = componentsInChildren;
        for (int i = 0; i < array.Length; i++)
        {
            Transform[] bones = array[i].bones;
            if (bones == null || bones.Length == 0)
            {
                continue;
            }
            Transform[] array2 = bones;
            foreach (Transform transform2 in array2)
            {
                if (!(transform2 == null) && !transform2.transform.IsChildOf(transform))
                {
                    flag = false;
                }
            }
        }
        if (!flag)
        {
            if (sb.Length > 0)
            {
                sb.AppendLine();
            }
            sb.AppendLine(messageOnInvalid);
        }
    }

    private bool IsHumanoid(StringBuilder sb, GameObject avatarObject, string messageOnInvalid)
    {
        if (avatarObject == null)
        {
            return false;
        }
        Animator component = avatarObject.GetComponent<Animator>();
        if (component == null)
        {
            return false;
        }
        if (component.avatar == null)
        {
            return false;
        }
        if (!component.avatar.isHuman)
        {
            return false;
        }
        return true;
    }

    private void AppendSmrAndVertexCheck(StringBuilder sb, GameObject sourceClothes, GameObject targetClothes)
    {
        if (sourceClothes == null || targetClothes == null)
        {
            return;
        }
        SkinnedMeshRenderer[] componentsInChildren = sourceClothes.GetComponentsInChildren<SkinnedMeshRenderer>(includeInactive: true);
        SkinnedMeshRenderer[] componentsInChildren2 = targetClothes.GetComponentsInChildren<SkinnedMeshRenderer>(includeInactive: true);
        int num = ((componentsInChildren != null) ? componentsInChildren.Length : 0);
        int num2 = ((componentsInChildren2 != null) ? componentsInChildren2.Length : 0);
        if (num != num2)
        {
            if (sb.Length > 0)
            {
                sb.AppendLine();
            }
            sb.AppendLine(LanguageManager.Get("UI.Validator.CheckRendererCountCheck"));
            sb.AppendLine($"- Source Clothes SMR : {num}");
            sb.AppendLine($"- Target Clothes SMR : {num2}");
            return;
        }
        for (int i = 0; i < num; i++)
        {
            SkinnedMeshRenderer skinnedMeshRenderer = componentsInChildren[i];
            SkinnedMeshRenderer skinnedMeshRenderer2 = componentsInChildren2[i];
            if (skinnedMeshRenderer == null || skinnedMeshRenderer2 == null)
            {
                continue;
            }
            Mesh sharedMesh = skinnedMeshRenderer.sharedMesh;
            Mesh sharedMesh2 = skinnedMeshRenderer2.sharedMesh;
            if (!(sharedMesh == null) && !(sharedMesh2 == null) && sharedMesh.vertexCount != sharedMesh2.vertexCount)
            {
                if (sb.Length > 0)
                {
                    sb.AppendLine();
                }
                sb.AppendLine(LanguageManager.Get("UI.Validator.VertexCountCheck"));
                sb.AppendLine($"- Source: {skinnedMeshRenderer.name} (vertices: {sharedMesh.vertexCount})");
                sb.AppendLine($"- Target: {skinnedMeshRenderer2.name} (vertices: {sharedMesh2.vertexCount})");
                break;
            }
        }
    }
}
