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
{
    [QueryProperty(nameof(PhoneNumber), nameof(PhoneNumber))]
    public class UserRegistrationVerifyOtpViewModel : BaseViewModel
    {
        #region Fields
        private string phoneNumber;
        private string step1;
        private string step2;
        private string step3;
        private string step4;
        private string errorMessage;
        #endregion

        #region Properties
        public string PhoneNumber
        {
            get => phoneNumber;
            set => phoneNumber = value;
        }
        public string Step1
        {
            get => step1;
            set
            {
                SetProperty(ref step1, value);
                ErrorMessage = string.Empty;
            }
        }
        public string Step2
        {
            get => step2;
            set
            {
                SetProperty(ref step2, value);
                ErrorMessage = string.Empty;
            }
        }
        public string Step3
        {
            get => step3;
            set
            {
                SetProperty(ref step3, value);
                ErrorMessage = string.Empty;
            }
        }
        public string Step4
        {
            get => step4;
            set
            {
                SetProperty(ref step4, value);
                ErrorMessage = string.Empty;
            }
        }
        public string ErrorMessage
        {
            get => errorMessage;
            set => SetProperty(ref errorMessage, value);
        }
        public Command VerifyOtp { get; set; }
        public Command Back { get; set; }

        #endregion

        #region Constructor
        public UserRegistrationVerifyOtpViewModel()
        {
            VerifyOtp = new Command(OnVerifyOtpClicked);
            Back = new Command(OnBackClicked);
        }
        #endregion

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

            if (string.IsNullOrWhiteSpace(Step1) || string.IsNullOrWhiteSpace(Step2) || string.IsNullOrWhiteSpace(Step3) || string.IsNullOrWhiteSpace(Step4))
            {
                ErrorMessage = "OTP required for Registration";
                return;
            }

            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();

            var otp = Step1 + Step2 + Step3 + Step4;
            var isOtpVerified = await gmCabsDriverService.VerifyOtp(PhoneNumber, otp);
            if (!isOtpVerified)
            {
                ErrorMessage = "OTP is incorrect";
                return;
            }

            await Shell.Current.GoToAsync($"//{nameof(LoginPage)}/{nameof(UserRegistrationGenerateOtpPage)}/{nameof(UserRegistrationVerifyOtpPage)}/{nameof(UserRegistrationUpdateDriverPinPage)}?{nameof(UserRegistrationUpdateDriverPinViewModel.PhoneNumber)}={phoneNumber}");
        }

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