using System;
using System.Collections;
using System.Collections.Generic;
using ECM2;
using ECM2.Examples.FirstPerson;
using Fantasy;
using NBC;
using NBF.Fishing2;
using NBF.Utils;
using UnityEngine;
using UnityEngine.InputSystem;
using Object = UnityEngine.Object;

namespace NBF
{
    public partial class FPlayer : MonoService<FPlayer>
    {
        public Transform Root;
        public Transform Eye;
        public Transform FppLook;
        public Transform IK;
        public PlayerModelAsset ModelAsset;
        public CharacterMovement Character;
        public FirstPersonCharacter FirstPerson;
        public GameObject ModelGameObject { get; set; }


        public FPlayerData Data { get; private set; }

        public readonly List<FRod> Tackles = new List<FRod>();
        public FRod Rod { get; private set; }
        public Fsm<FPlayer> Fsm { get; private set; }

        public event Action<FHandItem> OnFishingSetEquiped;
        public event Action OnFishingSetUnequip;

        protected override void OnAwake()
        {
            Character = gameObject.GetComponent<CharacterMovement>();
            FirstPerson = gameObject.GetComponent<FirstPersonCharacter>();
            Data = FPlayerData.Instance;
            transform.localPosition = new Vector3(484, 1, 422);
            // Data.NeedChangeRightArmAngle = true;
        }

        private void Start()
        {
            InitFsm();
            AddInputEvent();
            CreatePlayerModel();
        }

        private void Update()
        {
            
        }

        private void LateUpdate()
        {
            UpdateMove();
            Fsm?.Update();

            Data.EyeAngle = GameUtils.GetVerticalAngle(transform, FppLook);
        }
        
        

        private void OnDestroy()
        {
            RemoveInputEvent();
        }

        #region 状态机

        private void InitFsm()
        {
            Fsm = new Fsm<FPlayer>("Player", this, true);
            Fsm.RegisterState<PlayerStateIdle>();
            Fsm.RegisterState<PlayerStateThrow>();
            Fsm.RegisterState<PlayerStateFishing>();
            Fsm.RegisterState<PlayerStateFight>();
            Fsm.RegisterState<PlayerStatePrepare>();
            Fsm.Start<PlayerStateIdle>();
        }

        #endregion

        #region 角色模型

        private void CreatePlayerModel()
        {
            var modelObject = PrefabsHelper.CreatePlayer(Root, "Human_Male");
            modelObject.transform.localPosition = Vector3.zero;
            ModelGameObject = modelObject;
            ModelAsset = modelObject.GetComponent<PlayerModelAsset>();
            ModelAsset.SetPlayer(this);
        }

        #endregion

        #region 使用物品

        public IEnumerator UseItem(ItemInfo item)
        {
            if (Data.ChangeItem) yield break;
            Data.ChangeItem = true;
            var itemType = item?.ConfigId.GetItemType();
            if (itemType == ItemType.Rod)
            {
                //判断旧的是否要收回
                yield return UnUseItemConfirm();

                Data.IsLureRod = true;
                var rodType = (ItemSubType)item.Config.Type;
                if (rodType == ItemSubType.RodTele)
                {
                    Data.IsLureRod = false;
                }

                Rod =
                    item.Config.InstantiateAndComponent<FRod>(SceneSettings.Instance.GearNode, Vector3.zero,
                        Quaternion.identity);
                yield return Rod.InitRod(this, item);
                Tackles.Add(Rod);
                OnFishingSetEquiped?.Invoke(Rod);
            }

            Data.ChangeItem = false;
        }

        public IEnumerator UnUseItem()
        {
            if (Data.ChangeItem) yield break;
            Data.ChangeItem = true;
            yield return UnUseItemConfirm();
            Data.ChangeItem = false;
        }

        private IEnumerator UnUseItemConfirm()
        {
            if (Rod != null)
            {
                OnFishingSetUnequip?.Invoke();
                yield return Rod.Destroy();
                yield return new WaitForSeconds(0.35f);
                Destroy(Rod.gameObject);
                Tackles.Remove(Rod);
                Rod = null;
                yield return new WaitForSeconds(0.15f);
            }
        }

        #endregion

        /// <summary>
        /// 线断了
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="loseBaitChance"></param>
        public void LineBreak(string msg, float loseBaitChance)
        {
            
        }
    }
}