using System;
using UnityEngine;

namespace FastArena
{
    public class Player : MonoBehaviour
    {
        public float MovementSpeed;
        public float StrafeSpeed;
        public float JumpHeight;
        public float JumpCooldown;
        public CapsuleCollider CapsuleCollider;
        public GameObject CameraPoint;
        public GameObject ToolPoint;
        public GameObject[] ToolPrefabs;
        
        public Rigidbody Rigid { get; private set; }
        public Tool CurrentTool { get; private set; }
        [HideInInspector] public bool HasTool;

        private void Awake()
        {
            Rigid = GetComponentInChildren<Rigidbody>() ?? throw new NullReferenceException("can't find Rigidbody");
        }

        public void SelectTool(int toolNum)
        {
            if (HasTool) {
                DestroyImmediate(CurrentTool.gameObject);
                // CurrentTool = null;
                HasTool = false;
            }
            
            if (toolNum < 0 || toolNum >= ToolPrefabs.Length)
                return;
            var prefab = ToolPrefabs[toolNum];
            if (prefab == null)
                return;

            var tool = Instantiate(prefab, ToolPoint.transform);
            CurrentTool = tool.GetComponent<Tool>();
            HasTool = true;
        }
    }
}