using System;
using Patterns_And_Principles.Patterns.The_Observer_Pattern.WithoutEvent.Script._02_RealWorldUsage.Subject;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Patterns_And_Principles.Patterns.The_Observer_Pattern.WithoutEvent.Script._02_RealWorldUsage.Observer
{
    public class CameraController : Abstract.Observer
    {
        private bool _isTurboOn;
        private Vector3 _initalPosition;
        private float _shakingMagnitude = 0.1f;
        private BikeController _bikeController;

        private void OnEnable()
        {
            _initalPosition = gameObject.transform.localPosition;
        }

        private void Update()
        {
            if (_isTurboOn)
            {
                gameObject.transform.localPosition = _initalPosition + (Random.insideUnitSphere * _shakingMagnitude);
            }
            else
            {
                gameObject.transform.localPosition = _initalPosition;
            }
        }

        public override void Notify(Abstract.Subject subject)
        {
            if (!_bikeController)
            {
                _bikeController = subject.GetComponent<BikeController>();
            }

            if (_bikeController)
            {
                _isTurboOn = _bikeController.IsTurboOn;
            }
        }
    }
}