using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Views;

namespace WorkFlowCheck.MAUI.Pages.System;

public partial class InputPopup : Popup
{
    private TaskCompletionSource<(bool IsConfirmed, string Result)> _taskCompletionSource;
    public InputPopup(string userConfirmationLabel)
	{
		InitializeComponent();
        _taskCompletionSource = new();
        UserConfirmationLabel.Text = userConfirmationLabel;
        this.Closed += OnPopupClosed;
    }
    public Task<(bool IsConfirmed, string Result)> ShowAsync()
    {
        Application.Current.MainPage.ShowPopup(this);
        return _taskCompletionSource.Task;
    }
    private void OnOkClicked(object sender, EventArgs e)
    {
        Close((true, UserInputEntry.Text));
    }

    private void OnCancelClicked(object sender, EventArgs e)
    {
        Close((false, ""));
    }
    private void OnPopupClosed(object sender, PopupClosedEventArgs e)
    {
        if (e?.Result is ValueTuple<bool, string> value)
        {
            _taskCompletionSource.TrySetResult(value);
        }
        else
        {
            _taskCompletionSource.TrySetResult((false, null));
        }
    }

}