using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomRoleController : MonoBehaviour
{
    private Animator ani;
    private SkinnedMeshRenderer body;

    public Transform[] Chest;
    public Transform[] UpperArmUp;
    public Transform[] UpperArmDown;
    public Transform[] UpperLegUp;
    public Transform[] UpperLegDown;
    public Transform[] LowerLegUp;
    public Transform[] LowerLegDown;

    private void Start()
    {
        ani = GetComponent<Animator>();
        body = GetComponentInChildren<SkinnedMeshRenderer>();
        var t1 = ani.GetBoneTransform(HumanBodyBones.LeftHand);
        var t2 = ani.GetBoneTransform(HumanBodyBones.LeftFoot);
    }

    private void Update()
    {
        UpdatePos();
    }

    private void UpdatePos()
    {
        var t = body.bounds.min;
        var offset = t.y - (-1.36f);
        var y = transform.position.y - offset;
        transform.SetPositionY(y * 0.2f + transform.position.y * 0.8f);
    }

    public void UpdateBoneScale(EditableBodyPart part,float value)
    {
        CustomRoleUtility.UpdateBoneScale(ani, part, value);
    }

    public List<Transform> GetBonesTrans()
    {
        var bonesTrans = new List<Transform>();
        for (int i = 0; i < Enum.GetValues(typeof(HumanBodyBones)).Length - 1; i++)
        {
            var trans = ani.GetBoneTransform((HumanBodyBones)i);
            if (trans)
            {
                bonesTrans.Add(trans);
            }
        }

        return bonesTrans;
    }

    public void GetNormalAniBonesScale(ref Dictionary<string, Vector3> boneDic)
    {
        CustomRoleUtility.GetNormalAniBonesScale(ani,ref boneDic);
    }

    private Transform[] GetExtraAniBonesTrans(EditableBodyPart extraPart)
    {
        Transform[] targetTrans = null;
        switch (extraPart)
        {
            case EditableBodyPart.Chest:
                targetTrans = Chest;
                break;
            case EditableBodyPart.UpperArmUp:
                targetTrans = UpperArmUp;
                break;
            case EditableBodyPart.UpperArmDown:
                targetTrans = UpperArmDown;
                break;
            case EditableBodyPart.UpperLegUp:
                targetTrans = UpperLegUp;
                break;
            case EditableBodyPart.UpperLegDown:
                targetTrans = UpperLegDown;
                break;
            case EditableBodyPart.LowerLegUp:
                targetTrans = LowerLegUp;
                break;
            case EditableBodyPart.LowerLegDown:
                targetTrans = LowerLegDown;
                break;
            default:
                break;
        }
        return targetTrans;
    }

    public void GetExtraAniBonesScale(ref Dictionary<string, Vector3> boneDic,EditableBodyPart extraPart)
    {
        Transform[] targetTrans = GetExtraAniBonesTrans(extraPart);

        if(targetTrans != null)
        {
            for (int i = 0; i < targetTrans.Length; i++)
            {
                boneDic.Add(targetTrans[i].name, targetTrans[i].localScale);
            }
        }
    }

    public void GetExtraAniBonesRotate(ref Dictionary<string, Quaternion> boneDic, EditableBodyPart extraPart)
    {
        Transform[] targetTrans = GetExtraAniBonesTrans(extraPart);

        if (targetTrans != null)
        {
            for (int i = 0; i < targetTrans.Length; i++)
            {
                boneDic.Add(targetTrans[i].name, targetTrans[i].localRotation);
            }
        }
    }

    public void GetExtraAniBonesPosition(ref Dictionary<string, Vector3> boneDic, EditableBodyPart extraPart)
    {
        Transform[] targetTrans = GetExtraAniBonesTrans(extraPart);

        if (targetTrans != null)
        {
            for (int i = 0; i < targetTrans.Length; i++)
            {
                boneDic.Add(targetTrans[i].name, targetTrans[i].localPosition);
            }
        }
    }

    public void SetBonePos(string name,Vector3 value)
    {
        var bone = CustomRoleUtility.GetChild(transform,name);
        if(bone)
        {
            bone.transform.localPosition = value;
        }
    }

    public void SetBoneRot(string name, Quaternion value)
    {
        var bone = CustomRoleUtility.GetChild(transform, name);
        if (bone)
        {
            bone.transform.localRotation = value;
        }
    }

    public void SetBoneScale(string name, Vector3 value)
    {
        var bone = CustomRoleUtility.GetChild(transform, name);
        if (bone)
        {
            bone.transform.localScale = value;
        }
    }
}
