using UnityEngine;

namespace Sources
{
    public class PlayerHandling
    {
        public Vector3 Target => GetPlayerAimingPoint();
        public bool IsAttackingMain => Input.GetMouseButton(0);
        public bool IsAttackingSecondary => Input.GetMouseButton(1);

        private readonly Camera _camera;
        private readonly Player _player;
        private readonly int _ignoreLayers;

        public PlayerHandling(Player player, Camera camera, int ignoreLayers)
        {
            _player = player;
            _camera = camera;
            _ignoreLayers = ignoreLayers;
        }

        private Vector3 GetPlayerAimingPoint()
        {
            Vector3 target;
            var centerOfScreen = new Vector3(Screen.width / 2f, Screen.height / 2f, 0);
            RaycastHit hit;
            if (Physics.Raycast(_camera.ScreenPointToRay(centerOfScreen), out hit, 1000f, ~_ignoreLayers))
            {
                target = hit.point;
            }
            else
            {
                target = _player.transform.position + _camera.transform.forward * 1000f;
            }

            return target;
        }
    }
}