using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework.Event;
using UnityEngine;
using HumanoidEditor;
using GameFramework.DataTable;

namespace MetaClient
{
    public class CustomManager : MonoBehaviour
    {
        public static CustomManager Instance;

        public RoleCustomData bodyData;
        public const float valueFloating = 1f;
        public CustomRoleController testPlayer = null;
        public Dictionary<string, Transform> bones = new Dictionary<string, Transform>();

        private void Awake()
        {
            Instance = this;
        }

        public void Init()
        {
            GameEntry.Event.Subscribe(CustomRoleBodyEventArgs.EventId, UpdateBody);

            CreateCustomBodyData();
        }
        /// <summary>
        /// 生成玩家自定义数据信息
        /// </summary>
        public void CreateCustomBodyData()
        {
            testPlayer = GameObject.Find("标准女").GetComponent<CustomRoleController>();

            bodyData = new RoleCustomData();
            //初始化
            IDataTable<DRCustomBody> drCB = GameEntry.DataTable.GetDataTable<DRCustomBody>();
            DRCustomBody[] dtCB = drCB.GetAllDataRows();
            for (int i = 0; i < dtCB.Length; i++)
            {
                var partData = new PartData(
                    dtCB[i].Id,
                    dtCB[i].BoneName.Split(','),
                    dtCB[i].ScaleRangeMin,
                    dtCB[i].ScaleRangeMax,
                    dtCB[i].ScaleChange,
                    dtCB[i].PositionChange,
                    dtCB[i].RotationChange
                    );
                bodyData.bodyList.Add(dtCB[i].Id, partData);
            }

            bones.Clear();
            foreach (var item in bodyData.bodyList)
            {
                var key = item.Key;
                var value = item.Value;
                for (int i = 0; i < value.bones.Length; i++)
                {
                    var transform = CustomRoleUtility.GetChild(testPlayer.transform, value.bones[i]);
                    if(!transform)
                    {
                        continue;
                    }
                    if(!bones.ContainsKey(value.bones[i]))
                    {
                        bones.Add(value.bones[i], transform);
                    }
                }
            }
            Debug.Log( "一共有"+ bones.Count + "部位的骨骼自定义修改");

            //获取自定义骨骼的初始数据
            foreach (var item in bones)
            {
                var key = item.Key;
                var value = item.Value;
                bodyData.orginLocalScaleList.Add(key, value.localScale);
            }

            ////初始缩放
            //testPlayer.GetNormalAniBonesScale(ref bodyData.orginLocalScaleList);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.Chest);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperArmUp);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperArmDown);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperLegUp);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.UpperLegDown);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.LowerLegUp);
            //testPlayer.GetExtraAniBonesScale(ref bodyData.orginLocalScaleList, EditableBodyPart.LowerLegDown);
            ////初始位置
            //testPlayer.GetExtraAniBonesPosition(ref bodyData.orginLocalPositionList, EditableBodyPart.Chest);
            ////初始旋转
            //testPlayer.GetExtraAniBonesRotate(ref bodyData.orginLocalRotationList, EditableBodyPart.Chest);
        }

        public void Clear()
        {
            GameEntry.Event.Unsubscribe(CustomRoleBodyEventArgs.EventId, UpdateBody);
        }

        private void UpdateBody(object sender, GameEventArgs e)
        {
            CustomRoleBodyEventArgs crf = (CustomRoleBodyEventArgs)e;
            if (crf != null)
            {
                
                //更新数据
                var partData = bodyData.bodyList[crf.Part];
                if(partData == null)
                {
                    Debug.LogError("ID :" + crf.Part + "   无数据");
                    return;
                }
                if(crf.ChangeValue.x == 0.5f)
                {
                    partData.floatValue = 1;
                }
                if(crf.ChangeValue.x < 0.5f)
                {
                    partData.floatValue = partData.minChangeValue + (1 - partData.minChangeValue) * (crf.ChangeValue.x/0.5f);
                }
                if(crf.ChangeValue.x > 0.5f)
                {
                    partData.floatValue = 1 + (partData.maxChangeValue - 1) * ((crf.ChangeValue.x - 0.5f)/0.5f);
                }
                Debug.Log(crf.Part + " : " + crf.ChangeValue + " 转换后 :" + partData.floatValue);
                if (partData.scaleChange != Vector3.zero)
                {
                    for (int i = 0; i < partData.bones.Length; i++)
                    {
                        var orginBoneScale = bodyData.orginLocalScaleList[partData.bones[i]];
                        var bone = bones[partData.bones[i]];

                        var nowBone = bones[partData.bones[i]];
                        var nowScale = new Vector3(nowBone.localScale.x/orginBoneScale.x, nowBone.localScale.y / orginBoneScale.y, nowBone.localScale.z / orginBoneScale.z);
                        //var newBoneScale = new Vector3(orginBoneScale.x * (partData.scaleChange.x == 1 ? partData.floatValue : 1) * nowScale.x, orginBoneScale.y * (partData.scaleChange.y == 1 ? partData.floatValue : 1) * nowScale.y, orginBoneScale.z * (partData.scaleChange.z == 1 ? partData.floatValue : 1) * nowScale.y);

                        var newBoneScale = new Vector3(
                            partData.scaleChange.x == 1 ? partData.floatValue * orginBoneScale.x : nowBone.localScale.x,
                            partData.scaleChange.y == 1 ? partData.floatValue * orginBoneScale.y : nowBone.localScale.y,
                            partData.scaleChange.z == 1 ? partData.floatValue * orginBoneScale.z : nowBone.localScale.z
                            );

                        Debug.Log("newBoneScale : " + newBoneScale);
                        bodyData.changedScaleList[partData.bones[i]] = newBoneScale;
                    }
                }

                RefreshRole();
            }
        }

        public float GetBodyBoneValue(int id)
        {
            Debug.Log(id +":  GetBodyBoneValue + " + bodyData.bodyList[id].scaleChange + "   " + bodyData.bodyList[id].floatValue);
            if (bodyData.bodyList[id].scaleChange.x == 1 || bodyData.bodyList[id].scaleChange.y == 1 || bodyData.bodyList[id].scaleChange.z == 1)
            {
                if (bodyData.bodyList[id].floatValue == 1)
                {
                    return 0.5f;
                }
                if (bodyData.bodyList[id].floatValue < 1)
                {
                    return (bodyData.bodyList[id].floatValue - bodyData.bodyList[id].minChangeValue) / (1 - bodyData.bodyList[id].minChangeValue)/2;
                }
                if (bodyData.bodyList[id].floatValue > 1)
                {
                    return 0.5f + (bodyData.bodyList[id].floatValue - 1) / (bodyData.bodyList[id].maxChangeValue - 1)/2;
                }
            }
            
            return 0.5f;
        }
        /// <summary>
        /// 重置角色数据
        /// </summary>
        public void ResetRole()
        {
            if(testPlayer == null)
            {
                Debug.LogError("没有模型存在");
                return;
            }
            Debug.Log("重置模型");

            foreach (var item in bodyData.bodyList)
            {
                var key = item.Key;
                var value = item.Value;
                value.floatValue = 1;
            }

            //重置缩放
            foreach (var item in bodyData.orginLocalScaleList)
            {
                var key = item.Key;
                var value = item.Value;
                //testPlayer.SetBoneScale(key, value);
                testPlayer.SetBoneWorldScale(key, value);
            }

            //重置旋转
            foreach (var item in bodyData.orginLocalRotationList)
            {
                var key = item.Key;
                var value = item.Value;
                testPlayer.SetBoneRot(key, value);
            }

            //重置位置
            foreach (var item in bodyData.orginLocalPositionList)
            {
                var key = item.Key;
                var value = item.Value;
                testPlayer.SetBonePos(key, value);
            }
        }

        public void RefreshRole()
        {
            if (testPlayer == null)
            {
                Debug.LogError("没有模型存在");
                return;
            }
            Debug.Log("刷新模型");

            //刷新缩放
            //foreach (var item in bodyData.orginLocalScaleList)
            //{
            //    var key = item.Key;
            //    var value = item.Value;

            //    if(bodyData.changedScaleList.ContainsKey(key))
            //    {
            //        testPlayer.SetBoneScale(key, bodyData.changedScaleList[key]);
            //    }
            //    else
            //    {
            //        testPlayer.SetBoneScale(key, value);
            //    }
            //}

            foreach (var item in bodyData.changedScaleList)
            {
                var key = item.Key;
                var value = item.Value;

                testPlayer.SetBoneScale(key, value);
            }
        }
    }
}