using Godot;

namespace Cirno.Scripts.UI;

public partial class AudioSlider : HSlider
{
    [Export] public StringName AudioBusName { get; private set; } = "Master";
    
    private int _audioBusIndex;

    public override void _Ready()
    {
        _audioBusIndex = AudioServer.GetBusIndex(AudioBusName);

        CallDeferred(MethodName.InitializeDeferred);
    }

    private void InitializeDeferred()
    {
        this.Value = AudioServer.GetBusVolumeLinear(_audioBusIndex);
        
        this.ValueChanged += MusicVolumeSliderOnValueChanged;
    }
    
    private void MusicVolumeSliderOnValueChanged(double value)
    {
        AudioServer.SetBusVolumeLinear(_audioBusIndex, (float)value);
    }
}