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;

        private void Awake()
        {
            Instance = this;
        }

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

            testPlayer = GameObject.Find("biaozhunnv").GetComponent<CustomRoleController>();

            bodyData = new RoleCustomData();
            //初始化
            IDataTable<DRCustomBody> drCB = GameEntry.DataTable.GetDataTable<DRCustomBody>();
            DRCustomBody[] dtCB = drCB.GetAllDataRows();
            for (int i = 0; i < dtCB.Length; i++)
            {
                bodyData.bodyList.Add(i, new PartData(dtCB[i].Id,(EditableBodyPart)dtCB[i].Part, (ModifyType)dtCB[i].ModifyType));
            }

            //初始缩放
            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)
            {
                bodyData.bodyList[crf.Part].floatValue = 1 + (crf.ChangeValue.x - 0.5f) * valueFloating;
                if (testPlayer != null)
                {
                    testPlayer.UpdateBoneScale((EditableBodyPart)crf.Part, bodyData.bodyList[crf.Part].floatValue);
                }
            }
        }

        public float GetBodyBoneValue(int id)
        {
            return 0.5f + (bodyData.bodyList[id].floatValue - 1) / valueFloating;
        }
        /// <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);
            }

            //重置旋转
            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);
            }
        }
    }
}