using UnityEngine;

namespace Assets.Scripts.Gameplay
{
    public class Speedometer : MonoBehaviour {

        public GameObject SpeedoNeedle;
        public GameObject Start;
        public GameObject End;
        //private Vector3 _needlePosition;
        private float _distance;
        //private float _previousSpeed = 0;
        private CarController.CarController _car;

        void Awake()
        {
            var player = GameObject.FindGameObjectWithTag("Player");
            if (player != null) _car = player.GetComponent<CarController.CarController>();
            _distance = Vector3.Distance(Start.transform.position, End.transform.position);
        }

        void Update()
        {
            if (_car == null) return;
            //SetSpeedoPosition(_previousSpeed - _car.Speed, _car.Speed > _previousSpeed);
            //_previousSpeed = _car.Speed;
            SetPosition(_car.Speed);
        }

        void SetPosition(float carSpeed)
        {
            //var ratio = carSpeed / 220f;
            SpeedoNeedle.transform.position =
                Vector3.Lerp(Start.transform.position, End.transform.position, carSpeed / 220f);

        }
        //Todo: calculate position instead of shifting
        private void SetSpeedoPosition(float velocityChange, bool positiveVelocity)
        {
            /*
            _needlePosition = SpeedoNeedle.transform.position;
            if (positiveVelocity) {
                _needlePosition.x += velocityChange / 10;
            } else {
                _needlePosition.x -= velocityChange / 10;
            }
            _needlePosition.x = Mathf.Clamp (_needlePosition.x, -2.4F, 2.4F); 
            SpeedoNeedle.transform.position = _needlePosition;
            */
        }
    }
}
