//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
using DG.Tweening;
using GameFramework.DataTable;

namespace MetaClient
{
    public class CustomRoleForm : UGuiForm
    {
        public Text curShow;
        public RectTransform Scroll;
        public Slider curSlider;
        public Button[] btnList;
        public GameObject point;

        public RectTransform listContent;
        public GameObject listgo;

        private List<Transform> bonesTrans = new List<Transform>();
        private List<GameObject> pointList = new List<GameObject>();

        private DRCustomBody selectData = null;

#if UNITY_2017_3_OR_NEWER
        protected override void OnOpen(object userData)
#else
        protected internal override void OnOpen(object userData)
#endif
        {
            base.OnOpen(userData);

            //初始化
            IDataTable<DRCustomBody> drCB = GameEntry.DataTable.GetDataTable<DRCustomBody>();
            DRCustomBody[] dtCB = drCB.GetAllDataRows();
            for (int i = 0; i < dtCB.Length; i++)
            {
                var go = Instantiate(listgo, listContent);
                go.GetComponent<CustomRoleListCell>().Init(dtCB[i],this);
            }

            bonesTrans = CustomManager.Instance.testPlayer.GetBonesTrans();
            for (int i = 0; i < bonesTrans.Count; i++)
            {
                var newpoint = GameObject.Instantiate(point,transform);
                pointList.Add(newpoint);
            }

            Scroll.anchoredPosition = new Vector2(82, Scroll.anchoredPosition.y);
            Scroll.DOAnchorPosX(-85, 0.5f).SetEase(Ease.InOutCubic).onComplete = () =>
             {

             };
        }

        private void Start()
        {

        }

#if UNITY_2017_3_OR_NEWER
        protected override void OnClose(bool isShutdown, object userData)
#else
        protected internal override void OnClose(bool isShutdown, object userData)
#endif
        {
            base.OnClose(isShutdown, userData);
        }

        public void Update()
        {
            UpdateCustomBodyBoneTransShow();
        }

        private void RefreshSliderValue()
        {
            curShow.text = selectData.Name;
            curSlider.value = CustomManager.Instance.GetBodyBoneValue(selectData.Id);
        }

        public void OnSliderValueChange(float value)
        {
            if(selectData == null || selectData.Part == (int)EditableBodyPart.None)
            {
                return;
            }
            
            GameEntry.Event.Fire(this, CustomRoleBodyEventArgs.Create(selectData.Id, new Vector3(value, 0, 0)));
        }

        private void UpdateCustomBodyBoneTransShow()
        {
            for (int i = 0; i < bonesTrans.Count; i++)
            {
                var point = pointList[i];
                var pos = Camera.main.WorldToScreenPoint(bonesTrans[i].position);
                point.transform.position = pos;
            }
        }

        public void ClickListCell(DRCustomBody select)
        {
            selectData = select;
            RefreshSliderValue();
        }

        public void ClickReset()
        {
            CustomManager.Instance.ResetRole();
        }
    }
}
