private async void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            bool isValidated = ValidateInputs();

            SetResultLabelColor(true);

            if (isValidated)
            {
                SetResultLabelColor(false);

                // disable button click
                btnSubmit.IsEnabled = false;

                BillingPreviewRunInputs bprInputs = new BillingPreviewRunInputs(txtApiUserId.Text, txtApiUserPassword.Password, (DateTime)txtTargetDate.SelectedDate, txtRequestId.Text);

                // create async function
                imgLoading.Visibility = Visibility.Visible;
                lbResult.Text         = "Please wait...";
                var slowTask = Task <BillingPreviewRunResult> .Factory.StartNew(() => SubmitBillingPreviewRequest(bprInputs));

                await slowTask;

                StringBuilder messages = new StringBuilder();
                // update text result and show spinning icon
                if (slowTask.Result.Status == "Completed")
                {
                    SetResultLabelColor(false);
                    messages.AppendLine("Status: " + slowTask.Result.Status);
                    messages.AppendLine("Download Link: " + slowTask.Result.ResultFileUrl);
                    messages.AppendLine("RequestId: " + slowTask.Result.RequestId);
                    messages.AppendLine("Update Date: " + slowTask.Result.UpdateDate);
                    messages.AppendLine("Total Accounts: " + slowTask.Result.TotalAccounts);
                }
                else
                {
                    SetResultLabelColor(true);
                    messages.AppendLine("Status: " + slowTask.Result.Status);
                    if (slowTask.Result.Status == "Pending")
                    {
                        messages.AppendLine("Message: The request is submitted and in progress. Use the requestId and click the submit button again.");
                    }
                    else
                    {
                        messages.AppendLine("Message: " + slowTask.Result.Message);
                    }
                    messages.AppendLine("RequestId: " + slowTask.Result.RequestId);
                }
                lbResult.Text = messages.ToString();

                imgLoading.Visibility = Visibility.Hidden;

                // enable button click
                btnSubmit.IsEnabled = true;
            }
        }
        private BillingPreviewRunResult SubmitBillingPreviewRequest(BillingPreviewRunInputs inputs)
        {
            BillingPreviewRunResult  result         = new BillingPreviewRunResult("Start", "", 0, DateTime.Now, "", "");
            BillingPreviewRunService billingService = new BillingPreviewRunService();
            bool loginSuccess = billingService.Login(inputs.UserId, inputs.Password);

            if (loginSuccess)
            {
                string requestId = "";
                if (inputs.RequestId != "")
                {
                    requestId = inputs.RequestId;
                }
                else
                {
                    requestId = billingService.SubmitBillingPreviewRequest(inputs.TargetDate);
                }

                if (!string.IsNullOrWhiteSpace(requestId))
                {
                    // try 5 times to wait for the result.
                    for (int i = 0; i < 10; i++)
                    {
                        // check for result
                        result = billingService.GetBillingRequestById(requestId);

                        // break if the request is complete
                        if (result.Status == "Completed")
                        {
                            break;
                        }

                        // sleep 5 seconds
                        Thread.Sleep(5000);
                    }
                }
                else
                {
                    return(new BillingPreviewRunResult("Error", "", 0, DateTime.Now, "Submit BillingPreviewRun request failed!", requestId));
                }

                return(result);
            }
            else
            {
                return(new BillingPreviewRunResult("Error", "", 0, DateTime.Now, "Login failed! Check your credentials.", ""));
            }
        }