using GMCabsDriverAssistant.Models;
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 UserSignUpViewModel : BaseViewModel
    {
        #region Fields
        private string driverslicence;
        private string password;
        private string firstname;
        private string lastname;
        private string email;
        private string message;
        private string phoneNumber;
        #endregion
        #region Properties
        public string DriverLicence
        {
            get => driverslicence;
            set
            {
                SetProperty(ref driverslicence, value);
                ErrorMessage = string.Empty;
            }
        }
        public string Password
        {
            get => password;
            set
            {
                SetProperty(ref password, value);
                ErrorMessage = string.Empty;
            }
        }
        public string FirstName
        {
            get => firstname;
            set
            {
                SetProperty(ref firstname, value);
                ErrorMessage = string.Empty;
            }
        }
        public string LastName
        {
            get => lastname;
            set
            {
                SetProperty(ref lastname, value);
                ErrorMessage = string.Empty;
            }
        }
        public string Email
        {
            get => email;
            set
            {
                SetProperty(ref email, value);
                ErrorMessage = string.Empty;
            }
        }
        public string ErrorMessage
        {
            get => message;
            set => SetProperty(ref message, value);
        }
        public Command SignUpCommand { get; }
        public string PhoneNumber
        {
            get => phoneNumber;
            set
            {
                SetProperty(ref phoneNumber, value);
                ErrorMessage = string.Empty;
            }
        }

        #endregion
        #region Constructor
        public UserSignUpViewModel()
        {
            SignUpCommand = new Command(OnSignUpClicked);
#if DEBUG
#endif
        }
        #endregion
        #region Methods 
        private async void OnSignUpClicked(object obj)
        {
            CreateLightDriverRequest createLightDriverRequest = new CreateLightDriverRequest();
            ErrorMessage = "";
            if (string.IsNullOrWhiteSpace(DriverLicence))
            {
                ErrorMessage = "Driver Licence required for Sign-Up";
                return;
            }
            if (string.IsNullOrWhiteSpace(FirstName))
            {
                ErrorMessage = "First Name required for Sign-Up";
                return;
            }
            if (string.IsNullOrWhiteSpace(LastName))
            {
                ErrorMessage = "Last Name required for Sign-Up";
                return;
            }
            if (string.IsNullOrWhiteSpace(PhoneNumber))
            {
                ErrorMessage = "Mobile required for Sign-Up";
                return;
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                ErrorMessage = "Password required for Sign-Up";
                return;
            }
            if (string.IsNullOrWhiteSpace(Email))
            {
                ErrorMessage = "Email required for Sign-Up";
                return;
            }
            IsBusy = true;
            createLightDriverRequest.DriverLicence = DriverLicence;
            createLightDriverRequest.FirstName = FirstName;
            createLightDriverRequest.LastName = LastName;
            createLightDriverRequest.Password = Password;
            createLightDriverRequest.Email = Email;
            createLightDriverRequest.MobileNumber = PhoneNumber;
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            bool result = await gmCabsDriverService.CreateLightDriver(createLightDriverRequest);
            if (result)
            {
                IsBusy = false;
                await Shell.Current.GoToAsync($"//{nameof(LoginPage)}/{nameof(UserSignUpPage)}/{nameof(UserRegistrationVerifyOtpPage)}?{nameof(UserSignUpViewModel.PhoneNumber)}={PhoneNumber}");
            }
            else
            {
                IsBusy = false;
                ErrorMessage = "Sign Up falied";
                return;
            }
        }
        #endregion

    }
}
