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

namespace Patterns_And_Principles.Patterns.The_Observer_Pattern.WithoutEvent.Script._02_RealWorldUsage.Observer
{
    public class HUDContoller : Abstract.Observer
    {
        private bool _isTurboOn;
        private float _currentHealth;
        private BikeController _bikeController;

        private void OnGUI()
        {
            GUILayout.BeginArea(new Rect(50, 50, 200, 200));

            GUILayout.Label("Health: " + _currentHealth);

            if (_isTurboOn)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.Label("Turbo Activated!");
                GUILayout.EndHorizontal();
            }

            if (_currentHealth <= 50f)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.Label("WARNING Low Health");
                GUILayout.EndHorizontal();
            }

            GUILayout.EndArea();
        }

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

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