UI/Player/StaminaBarUI.razor
@using Sandbox;
@using Sandbox.UI;
@using Opium;

@namespace Opium
@inherits PanelComponent

<root>
    @if (StaminaMechanic is null) return;

    <div class="Stamina @ShowBar()">
        <div class="StaminaBar" style="width: @(CalculateValue(StaminaMechanic.Stamina)* 100)%;"></div>
    </div>

    <div class="Posture @ShowBar()">
        <div class="PostureBar" style="width: @(CalculateValue(PostureMechanic.Value)* PostureMechanic.Default)%;"></div>
    </div>
</root>

@code
{
    [Property] public Opium.PlayerController Player { get; set; }

    public StaminaMechanic StaminaMechanic => Player.GetMechanic<StaminaMechanic>();
    public PostureMechanic PostureMechanic => Player.GetMechanic<PostureMechanic>();

    private float CalculateValue( float value )
    {
        return value.LerpInverse( 0, 100 );
    }

    protected override void OnUpdate()
    {
        if ( StaminaMechanic.CanUseStamina )
        {
            SetClass( "blink", false );
        }
        else
        {
            SetClass( "blink", true );
        }
    }

    string ShowBar()
    {
        // If we are using posture, show the bars.
        if ( PostureMechanic.Value < 100 ) return "using";

        if ( StaminaMechanic.TimeSinceLastUsedStamina < 2 )
		{
			return "using";
		}
		else
		{
			return "";
		}
	}

	/// <summary>
	/// the hash determines if the system should be rebuilt. If it changes, it will be rebuilt
	/// </summary>
	protected override int BuildHash() => System.HashCode.Combine( Time.Delta );
}