namespace Newlon.Components.GUI.Seedpackets;

public class ChoosableHandler : SeedpacketHandler, ISeedpacketPress
{
    public ChoosableHandler(Seedpacket owner) : base(owner)
    {
    }

    public void Pressed()
    {
        if (LevelGUIElements.Instance.SeedpacketsHotbar.GetChildCount() > 9) return;
        _owner.disablePacket = true;

        var hotbarSeedpacket = Seedpacket.Prefab.Instantiate<Seedpacket>();
        LevelGUIElements.Instance.SeedpacketsHotbar.AddChild(hotbarSeedpacket);
        hotbarSeedpacket.SetResource(_owner.GetResource());

        var pregameHandler = new HotbarPregameHandler(hotbarSeedpacket);
        hotbarSeedpacket.SetHandler(pregameHandler);
        pregameHandler.Clicked += OnHotbarClicked;
        
        AudioSequencer.Play("tap", Seedpacket.TapStream);
    }

    public void OnHotbarClicked()
    {
        _owner.disablePacket = false;
    }
}