// 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.BodyPoseMatchUtil
using System.Collections.Generic;
using UnityEngine;

public class BodyPoseMatchUtil
{
    private WorldVertexUtil _worldVertexUtil;

    private MeshClassifier meshClassifier;

    private BodyPoseMatch_CommonUtil poseMatchCommonUtil;

    private bool doDebug;

    public BodyPoseMatchUtil()
    {
        this._worldVertexUtil = new WorldVertexUtil();
        this.meshClassifier = new MeshClassifier();
        this.poseMatchCommonUtil = new BodyPoseMatch_CommonUtil();
    }

    public GameObject AutoAdjustBodyPose(GameObject sourceAvatar, IReadOnlyList<SkinnedMeshRenderer> sourceBodyMeshes, GameObject targetAvatar, IReadOnlyList<SkinnedMeshRenderer> targetBodyMeshes, out Dictionary<Transform, Transform> sourceToProxy, float neckTargetHeight = 1.5f, bool onlyScaling = false)
    {
        Animator component = sourceAvatar.GetComponent<Animator>();
        Animator component2 = targetAvatar.GetComponent<Animator>();
        if (component == null || component2 == null)
        {
            Debug.LogError("[AvatarBodyMatchUtil] sourceAvatar 또는 targetAvatar가 null입니다.");
            sourceToProxy = new Dictionary<Transform, Transform>();
            return null;
        }
        Dictionary<HumanBodyBones, HashSet<Transform>> humanBoneMap = this.meshClassifier.MeshHumanoidBoneMatcher(component, sourceBodyMeshes);
        Dictionary<HumanBodyBones, HashSet<Transform>> dictionary = this.meshClassifier.MeshHumanoidBoneMatcher(component2, targetBodyMeshes);
        BodyPoseMatchSetupUtil bodyPoseMatchSetupUtil = new BodyPoseMatchSetupUtil();
        bodyPoseMatchSetupUtil.AdjustAvatarScaleByNeck(sourceAvatar.transform, humanBoneMap, neckTargetHeight);
        bodyPoseMatchSetupUtil.AdjustAvatarScaleByNeck(targetAvatar.transform, dictionary, neckTargetHeight);
        List<SkinnedMeshRenderer> proxyBodyMeshes;
        GameObject gameObject = bodyPoseMatchSetupUtil.CreateBodyProxy(component, sourceBodyMeshes, out proxyBodyMeshes, out sourceToProxy);
        Animator component3 = gameObject.GetComponent<Animator>();
        if (onlyScaling)
        {
            gameObject.transform.SetParent(targetAvatar.transform);
            gameObject.transform.localPosition = Vector3.zero;
            return gameObject;
        }
        Dictionary<HumanBodyBones, HashSet<Transform>> dictionary2 = this.meshClassifier.MeshHumanoidBoneMatcher(component3, proxyBodyMeshes);
        if (dictionary2 == null || dictionary2.Count == 0)
        {
            throw new AutoMorpherException("Proxy Bone Map is Missing", "[BodyPoseMatch_Arm] AlignUpperArmByArmPcaCenters\n - proxyBoneMap is null");
        }
        gameObject.transform.SetParent(targetAvatar.transform);
        gameObject.transform.localPosition = Vector3.zero;
        List<BakedBodyMesh> list = new List<BakedBodyMesh>();
        foreach (SkinnedMeshRenderer item in proxyBodyMeshes)
        {
            list.Add(new BakedBodyMesh(item));
        }
        List<BakedBodyMesh> list2 = new List<BakedBodyMesh>();
        foreach (SkinnedMeshRenderer targetBodyMesh in targetBodyMeshes)
        {
            list2.Add(new BakedBodyMesh(targetBodyMesh));
        }
        BodyPoseMatch_Torso bodyPoseMatch_Torso = new BodyPoseMatch_Torso();
        bodyPoseMatch_Torso.AlignTorsoByNeck(gameObject, list, dictionary2, targetAvatar, list2, dictionary);
        if (this.doDebug)
        {
            bodyPoseMatch_Torso.DrawTorsoPcaDebug(gameObject, list, dictionary2, Color.yellow, Color.cyan, 1f, 20f);
            bodyPoseMatch_Torso.DrawTorsoPcaDebug(targetAvatar, list2, dictionary, Color.red, Color.green, 1f, 20f);
        }
        foreach (BakedBodyMesh item2 in list)
        {
            item2.ReBakeMesh();
        }
        BodyPoseMatch_Arm bodyPoseMatch_Arm = new BodyPoseMatch_Arm();
        bodyPoseMatch_Arm.AlignUpperArmByArmPcaCenters(list, dictionary2, list2, dictionary);
        if (this.doDebug)
        {
            bodyPoseMatch_Arm.DrawArmPcaDebug(gameObject, list, dictionary2, isLeft: true, Color.yellow, Color.cyan, 1f, 20f);
            bodyPoseMatch_Arm.DrawArmPcaDebug(targetAvatar, list2, dictionary, isLeft: true, Color.red, Color.green, 1f, 20f);
        }
        foreach (BakedBodyMesh item3 in list)
        {
            item3.ReBakeMesh();
        }
        bodyPoseMatch_Arm.ScalingBothArmsLength(list, dictionary2, list2, dictionary);
        foreach (BakedBodyMesh item4 in list)
        {
            item4.ReBakeMesh();
        }
        BodyPoseMatch_Leg bodyPoseMatch_Leg = new BodyPoseMatch_Leg();
        bodyPoseMatch_Leg.AlignBothUpperLegs(gameObject, list, dictionary2, targetAvatar, list2, dictionary);
        foreach (BakedBodyMesh item5 in list)
        {
            item5.ReBakeMesh();
        }
        bodyPoseMatch_Leg.ScalingBothLegsAndFoots(gameObject, list, dictionary2, targetAvatar, list2, dictionary);
        if (this.doDebug)
        {
            foreach (BakedBodyMesh item6 in list)
            {
                item6.ReBakeMesh();
            }
            bodyPoseMatch_Leg.DrawLegPcaDebug(gameObject, list, dictionary2, isLeft: true, Color.yellow, Color.cyan, 1f, 5f);
            bodyPoseMatch_Leg.DrawLegPcaDebug(gameObject, list, dictionary2, isLeft: false, Color.yellow, Color.cyan, 1f, 5f);
            bodyPoseMatch_Leg.DrawLegPcaDebug(targetAvatar, list2, dictionary, isLeft: true, Color.magenta, Color.green, 1f, 5f);
            bodyPoseMatch_Leg.DrawLegPcaDebug(targetAvatar, list2, dictionary, isLeft: false, Color.magenta, Color.green, 1f, 5f);
            bodyPoseMatch_Arm.DrawForearmExtremeDebugPair(gameObject.gameObject, proxyBodyMeshes, targetAvatar, targetBodyMeshes, isLeft: true, 1f, 5f);
        }
        return gameObject;
    }
}
