// 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.
// EdenAutoMorpherScript, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.BoneAlignmentUtil
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class BoneAlignmentUtil
{
    public void AlignClothBonesToAvatar(ClothInstance cloth, Animator targetAvatarAnimator)
    {
        if (cloth == null || targetAvatarAnimator == null)
        {
            Debug.LogWarning("[BoneAlignmentUtil] cloth 또는 targetAvatarAnimator 가 null.");
            return;
        }
        foreach (KeyValuePair<HumanBodyBones, HashSet<Transform>> humanoidMatchedBone in cloth.humanoidMatchedBones)
        {
            HumanBodyBones key = humanoidMatchedBone.Key;
            foreach (Transform item in humanoidMatchedBone.Value)
            {
                if (!(item == null))
                {
                    Transform boneTransform = targetAvatarAnimator.GetBoneTransform(key);
                    if (!(boneTransform == null))
                    {
                        item.position = boneTransform.position;
                        item.localScale = Vector3.one;
                        item.name = boneTransform.name;
                    }
                }
            }
        }
    }

    public void ReparentAccessoryBonesToAvatar(EdenAutoMorpherConfig config)
    {
        if (config.clothBoneTypeMap == null || config.targetAvatarObject == null)
        {
            return;
        }
        Animator component = config.targetAvatarObject.GetComponent<Animator>();
        if (component == null || !component.isHuman)
        {
            return;
        }
        Dictionary<Transform, Transform> dictionary = new Dictionary<Transform, Transform>();
        if (config.clothesHumanoidMatchedBones != null)
        {
            foreach (KeyValuePair<HumanBodyBones, HashSet<Transform>> clothesHumanoidMatchedBone in config.clothesHumanoidMatchedBones)
            {
                HumanBodyBones key = clothesHumanoidMatchedBone.Key;
                foreach (Transform item2 in clothesHumanoidMatchedBone.Value)
                {
                    Transform boneTransform = component.GetBoneTransform(key);
                    if (item2 != null && boneTransform != null)
                    {
                        dictionary[item2] = boneTransform;
                    }
                }
            }
        }
        foreach (KeyValuePair<Transform, ClothBoneType> item3 in config.clothBoneTypeMap)
        {
            if (item3.Value != ClothBoneType.Accessory)
            {
                continue;
            }
            Transform key2 = item3.Key;
            if (key2 == null || !key2.IsChildOf(config.targetClothesObject.transform))
            {
                continue;
            }
            Transform parent = key2.parent;
            Transform transform = null;
            while (parent != null)
            {
                if (config.clothBoneTypeMap.TryGetValue(parent, out var value) && value == ClothBoneType.Body)
                {
                    transform = parent;
                    break;
                }
                parent = parent.parent;
            }
            if (!(transform == null) && dictionary.TryGetValue(transform, out var value2))
            {
                key2.SetParent(value2, worldPositionStays: true);
            }
        }
        foreach (KeyValuePair<Transform, Transform> item4 in dictionary)
        {
            Transform key3 = item4.Key;
            Transform value3 = item4.Value;
            if (key3 == null || value3 == null)
            {
                continue;
            }
            List<Transform> list = new List<Transform>();
            foreach (Transform item5 in key3)
            {
                list.Add(item5);
            }
            foreach (Transform item6 in list)
            {
                if (!(item6 == null) && !config.clothBoneTypeMap.ContainsKey(item6))
                {
                    item6.SetParent(value3, worldPositionStays: true);
                }
            }
        }
    }

    public void CleanupTempBones(Transform root)
    {
        if (root == null)
        {
            return;
        }
        TempBoneMarker[] componentsInChildren = root.GetComponentsInChildren<TempBoneMarker>(includeInactive: true);
        if (componentsInChildren == null || componentsInChildren.Length == 0)
        {
            return;
        }
        TempBoneMarker[] array = (from m in componentsInChildren
                                  where m != null
                                  orderby GetDepth(m.transform) descending
                                  select m).ToArray();
        foreach (TempBoneMarker tempBoneMarker in array)
        {
            if (tempBoneMarker == null)
            {
                continue;
            }
            Transform transform = tempBoneMarker.transform;
            Transform transform2 = tempBoneMarker.originalParent;
            if (transform2 == null)
            {
                transform2 = transform.parent;
            }
            if (transform2 != null)
            {
                while (transform.childCount > 0)
                {
                    transform.GetChild(0).SetParent(transform2, worldPositionStays: true);
                }
            }
            if (!Application.isPlaying)
            {
                Object.DestroyImmediate(transform.gameObject);
            }
            else
            {
                Object.Destroy(transform.gameObject);
            }
        }
        static int GetDepth(Transform t)
        {
            int num2 = 0;
            while (t != null)
            {
                num2++;
                t = t.parent;
            }
            return num2;
        }
    }
}
