using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistantSolution.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GMCabsDriverAssistantSolution.ViewModels
{
    public class UserRegistrationGenerateOtpViewModel : BaseViewModel
    {
        #region Fields
        private string phoneNumber;
        private string errorMessage;
        #endregion

        #region Properties
        public string PhoneNumber
        {
            get => phoneNumber;
            set
            {
                SetProperty(ref phoneNumber, value);
                ErrorMessage = string.Empty;
            }
        }
        public string ErrorMessage
        {
            get => errorMessage;
            set => SetProperty(ref errorMessage, value);
        }
        public Command Register { get; set; }
        public Command Back { get; set; }
        #endregion

        #region Constructor
        public UserRegistrationGenerateOtpViewModel()
        {
            Register = new Command(OnRegisterClicked);
            Back = new Command(OnBackClicked);
#if DEBUG
            //PhoneNumber = "0411111119";
#endif
        }

        #endregion

        #region Methods
        private async void OnRegisterClicked(object obj)
        {
            ErrorMessage = "";

            if (string.IsNullOrWhiteSpace(PhoneNumber))
            {
                ErrorMessage = "Mobile required for Registration";
                return;
            }

            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            var isOtpSent = await gmCabsDriverService.GenerateOtp(PhoneNumber);
            if (!isOtpSent)
            {
                ErrorMessage = "";
                await Shell.Current.GoToAsync($"//{nameof(LoginPage)}/{nameof(UserRegistrationGenerateOtpPage)}/{nameof(UserSignUpPage)}?{nameof(UserRegistrationVerifyOtpViewModel.PhoneNumber)}={PhoneNumber}");
            }
            else
            {
                await Shell.Current.GoToAsync($"//{nameof(LoginPage)}/{nameof(UserRegistrationGenerateOtpPage)}/{nameof(UserRegistrationVerifyOtpPage)}?{nameof(UserRegistrationVerifyOtpViewModel.PhoneNumber)}={PhoneNumber}");
            }

        }



        private async void OnBackClicked(object obj)
        {
            await Shell.Current.GoToAsync("..");
        }
        #endregion
    }
}
