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

public class ProfilePoseMatchUtil
{
    private WorldVertexUtil _worldVertexUtil;

    private MeshClassifier meshClassifier;

    public ProfilePoseMatchUtil()
    {
        this._worldVertexUtil = new WorldVertexUtil();
        this.meshClassifier = new MeshClassifier();
    }

    public void ProfilePoseMatcher(GameObject targetAvatar, IReadOnlyList<SkinnedMeshRenderer> targetBodyMeshes, GameObject targetCloth, ProfileData profileData, Dictionary<HumanBodyBones, HashSet<Transform>> clothHumanBonesMap, Dictionary<Transform, ClothBoneType> clothBoneTypeMap, float neckTargetHeight = 1.5f)
    {
        if (targetAvatar == null)
        {
            throw new AutoMorpherException("Target Avatar is Null", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - targetAvatar is null");
        }
        if (targetAvatar.GetComponent<Animator>() == null)
        {
            throw new AutoMorpherException("Target Avatar Animator is Null", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - targetAvatar has no animator");
        }
        if (targetBodyMeshes == null || targetBodyMeshes.Count == 0)
        {
            throw new AutoMorpherException("Target Body Meshes are Missing", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - targetBodyMeshes is null or empty");
        }
        if (targetCloth == null)
        {
            throw new AutoMorpherException("Target Cloth is Null", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - targetCloth is null");
        }
        if (profileData == null)
        {
            throw new AutoMorpherException("Profile Data is Null", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - profileData is null");
        }
        if (clothHumanBonesMap == null || clothHumanBonesMap.Count == 0 || clothBoneTypeMap == null || clothBoneTypeMap.Count == 0)
        {
            throw new AutoMorpherException("Clothing Bone Match Data is Missing", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - profile Clothe Bone Match Data is null or empty");
        }
        Transform transform = targetAvatar.transform;
        Transform transform2 = targetCloth.transform;
        Transform parent = transform.parent;
        transform.SetParent(null, worldPositionStays: true);
        Transform parent2 = transform2.parent;
        transform2.SetParent(null, worldPositionStays: true);
        transform2.position = transform.position;
        Dictionary<HumanBodyBones, HashSet<Transform>> dictionary = this.meshClassifier.MeshHumanoidBoneMatcher(targetAvatar.GetComponent<Animator>(), targetBodyMeshes);
        if (dictionary == null || dictionary.Count == 0)
        {
            throw new AutoMorpherException("Target Avatar Bone Match Data is Missing", "[ProfilePoseMatchUtil] ProfilePoseMatcher\n - target Avatar Bone Match Data is null or empty");
        }
        BodyPoseMatchSetupUtil bodyPoseMatchSetupUtil = new BodyPoseMatchSetupUtil();
        Vector3 comprehensiveScale = bodyPoseMatchSetupUtil.GetComprehensiveScale(transform2, clothHumanBonesMap, profileData);
        Debug.Log($"ComprehensiveScale: {comprehensiveScale}");
        bodyPoseMatchSetupUtil.AdjustAvatarScaleByNeck(transform, dictionary, neckTargetHeight);
        bodyPoseMatchSetupUtil.AdjustAvatarScaleByNeck(transform2, clothHumanBonesMap, neckTargetHeight);
        List<BakedBodyMesh> list = new List<BakedBodyMesh>();
        foreach (SkinnedMeshRenderer targetBodyMesh in targetBodyMeshes)
        {
            list.Add(new BakedBodyMesh(targetBodyMesh));
        }
        new BodyPoseMatch_Torso().AlignTorsoByNeck(transform, list, dictionary, clothHumanBonesMap, transform2, profileData, comprehensiveScale);
        BodyPoseMatch_Arm bodyPoseMatch_Arm = new BodyPoseMatch_Arm();
        bodyPoseMatch_Arm.AlignUpperArmByArmPcaCenters(list, dictionary, clothHumanBonesMap, transform2, profileData, comprehensiveScale);
        bodyPoseMatch_Arm.ScalingBothArmsLength(list, dictionary, clothHumanBonesMap, profileData, comprehensiveScale);
        BodyPoseMatch_Leg bodyPoseMatch_Leg = new BodyPoseMatch_Leg();
        bodyPoseMatch_Leg.AlignBothUpperLegs(transform, list, dictionary, transform2, clothHumanBonesMap, profileData, comprehensiveScale);
        bodyPoseMatch_Leg.ScalingBothLegsAndFoots(transform, list, dictionary, transform2, clothHumanBonesMap, profileData, comprehensiveScale);
        transform.SetParent(parent, worldPositionStays: true);
        transform2.SetParent(parent2, worldPositionStays: true);
    }
}
