using System;
using UnityEngine;

namespace Assets.Scripts.Camera
{
    public class ChaseCam : MonoBehaviour
    {
        public Transform Target;
        private float _x;
        private float _y;
        private float _yOffset;
        private float _distance;

        void Start ()
        {
            _x = 0;
            _y = 11.25f;
            _yOffset = 1.5f;
            _distance = 9f;

        }

        // Update is called once per frame
        void Update () {
		    ShowFollowMode();
        }

        void ShowFollowMode()
        {
            var xRotation = 0f;
            var rot = Target.localEulerAngles.y;
            if (rot > 180 && _x < 90) rot -= 360;
            _x = Mathf.SmoothDamp(_x, rot, ref xRotation, 0.15f);
            if (xRotation > 2f)
            {
                //Debug.Log(rot + " : " + _x + " > " + xRotation);
                //Debug.Break();
            }

            var rotation = Quaternion.Euler(_y, _x, 0);
            var position = rotation * new Vector3(0.0f, 0.5f, -_distance) + Target.position;
            position.y += _yOffset;
            transform.rotation = rotation;
            transform.position = position;
        }

    }
}
