namespace Phoneword
{
    public partial class MainPage : ContentPage
    {
        string _translatedNumber;

        public MainPage()
        {
            InitializeComponent();
        }

        private void TranslateButton_Clicked(object sender, EventArgs e)
        {
            string enteredNumber = PhoneNumberText.Text;
            _translatedNumber = PhonewordTranslator.ToNumber(enteredNumber);
            if (!string.IsNullOrEmpty(_translatedNumber))
            {
                CallButton.IsEnabled = true;
                CallButton.Text = $"Call {_translatedNumber}";
            }
            else
            {
                CallButton.IsEnabled = false;
                CallButton.Text = "Call";
            }
        }

        private async void CallButton_Clicked(object sender, EventArgs e)
        {
            if (await this.DisplayAlert(
                "Dial a Number",
                $"Would you like to call {_translatedNumber}?",
                "Yes",
                "No"))
            {
                try
                {
                    if (PhoneDialer.Default.IsSupported)
                        PhoneDialer.Default.Open(_translatedNumber);
                }
                catch (ArgumentNullException)
                {
                    await DisplayAlert("Unalble to dial", "Phone was not valid.", "OK");
                }
                catch (Exception)
                {
                    await DisplayAlert("Unalble to dial", "Phone dialing failed.", "OK");
                }
            }
        }
    }

}
