using DG.Tweening;
using NBC;
using NBF;
using RootMotion.FinalIK;
using UnityEngine;

public partial class FPlayer : MonoBehaviour
{
    public FPlayerData Data;

    /// <summary>
    /// 相机挂点
    /// </summary>
    public Transform CameraRoot;

    private CCDIK interactionCCDIK;

    private float interactionTargetWeight;

    public Fsm<FPlayer> Fsm { get; private set; }

    private PlayerArm[] Arms;
    public PlayerArm MainArm { get; private set; }

    public PlayerArm MinorArm { get; private set; }

    public PlayerAnimator PlayerAnimatorCtrl;

    public FPlayerUseGear Gears;

    public LureTrajectorySimulator LureTrajectorySimulator;

    public Transform Light;

    public Transform BackSpine;

    public Rigidbody Rigidbody;


    public PlayerCharacter Character;

    private void Awake()
    {
        Character = GetComponent<PlayerCharacter>();
        PlayerAnimatorCtrl = gameObject.GetComponent<PlayerAnimator>();
        if (PlayerAnimatorCtrl == null)
        {
            PlayerAnimatorCtrl = gameObject.AddComponent<PlayerAnimator>();
        }

        Gears = gameObject.GetComponent<FPlayerUseGear>();
        if (Gears == null)
        {
            Gears = gameObject.AddComponent<FPlayerUseGear>();
        }

        LureTrajectorySimulator = gameObject.AddComponent<LureTrajectorySimulator>();

        PlayerAnimatorCtrl.Player = this;
        Arms = GetComponentsInChildren<PlayerArm>();
    }

    private void Start()
    {
        ChangeArm();

        InteractiveMainHand(null);

        Init();
    }


    public void InitData(FPlayerData data)
    {
        Data = data;


        if (data.PlayerID == GameModel.RoleID)
        {
            Fishing.Inst.Player.SelfPlayer = this;
            BaseCamera.Main.transform.SetParent(CameraRoot);
            BaseCamera.Main.transform.localPosition = Vector3.zero;
        }
    }

    #region 初始化相关

    private void Init()
    {
        InitFsm();
    }

    private void InitFsm()
    {
        Fsm = new Fsm<FPlayer>("Player", this, true);
        Fsm.RegisterState<PlayerIdle>();
        Fsm.RegisterState<PlayerThrow>();
        Fsm.RegisterState<PlayerFishing>();
        Fsm.RegisterState<PlayerFight>();
        Fsm.RegisterState<PlayerShowFish>();
        Fsm.RegisterState<PlayerWaitThrow>();
        Fsm.Start<PlayerIdle>();
    }

    #endregion

    public void ChangeArm()
    {
        var isLeftHand = PlayerAnimatorCtrl.LeftHand;
        foreach (var arm in Arms)
        {
            if (isLeftHand)
            {
                if (arm.IsLeft)
                {
                    MainArm = arm;
                }
                else
                {
                    MinorArm = arm;
                }
            }
            else
            {
                if (!arm.IsLeft)
                {
                    MainArm = arm;
                }
                else
                {
                    MinorArm = arm;
                }
            }
        }
    }


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

        // CheckAndConnectRod();
        MoveTowardsInteraction();

        if (CanChangeGear())
        {
            Gears?.Update();
        }

        SyncLight();
    }

    private void SyncLight()
    {
        if (Light)
        {
            if (Light.gameObject.activeSelf != Data.openLight)
            {
                Light.gameObject.SetActive(Data.openLight);
            }
        }
    }

    private Tween _fovTween; // 存储当前的Tween动画，方便中断
    private float _zoomDuration = 0.2f; // 缩放动画时间

    public Boat NowBoat { get; private set; }

    /// <summary>
    /// 切换望远镜效果（打开/关闭）
    /// </summary>
    /// <param name="isZoomIn">true=打开望远镜，false=关闭望远镜</param>
    public void ToggleTelescope()
    {
        // 如果已经有动画在运行，先停止
        _fovTween?.Kill();

        // 根据传入的参数决定目标FOV
        float targetFOV = Data.openTelescope ? GameDef.ZoomedFOV : GameDef.NormalFOV;

        // 使用DoTween平滑过渡FOV
        _fovTween = DOTween.To(
            () => BaseCamera.Main.fieldOfView, // 获取当前FOV
            x => BaseCamera.Main.fieldOfView = x, // 设置新FOV
            targetFOV, // 目标FOV
            _zoomDuration // 动画时间
        ).SetEase(Ease.InOutQuad); // 使用缓动函数让动画更平滑
    }

    public bool CanChangeGear()
    {
        return true;
    }

    public void EnterBoat(Boat boat)
    {
        NowBoat = boat;
        boat.Use(this);
        PlayerAnimatorCtrl.Boat = true;
        transform.parent = boat.Place;
        transform.localPosition = Vector3.zero;
        transform.localRotation = Quaternion.identity;
        Rigidbody.isKinematic = true;
        Character.CharacterController.enabled = false;
    }

    public void ExitBoat()
    {
        NowBoat.UnUse();
        NowBoat = null;
        Rigidbody.isKinematic = true;
        Character.CharacterController.enabled = true;
    }
}