﻿
using System.Linq;
using Quest.MobileServices.Database;
using Quest.MobileServices.Database.Security;
using Quest.MobileServices.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Text;
using Quest.MobileServices.Managers;
using Quest.MobileServices.Models;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using Quest.MobileServices.Services;
using Xamarin.Forms;
using System.Threading.Tasks;

namespace Quest.MobileServices.ViewModels
{

    public class LoginPageViewModel : ViewModelBase
    {
        public ICommand LoginCommand { get; set; }
        public ICommand LogoutCommand { get; set; }

        public INavigation Navigation { get; set; }

        private IAuthenticationService _authService;

        public LoginPageViewModel(INavigation navigation = null)
        {
            Navigation = navigation;
            _authService = DependencyService.Get<IAuthenticationService>();
            WireCommands();
        }

        private void WireCommands()
        {
            LoginCommand = new RelayCommand(DoLogin);
        }

        public async Task PopModalAsync()
        {
            if (Navigation != null)
                await Navigation.PopModalAsync();
        }

        private async void DoLogin()
        {

            IsPresentingLoginUI = true;

            // When the App.Authenticate() returns, the login UI is hidden, regardless of success (for example, if the user taps "Cancel" in iOS).
            // This means the SplashPage will be visible again, so we need to make the sign in button clickable again by hiding the activity indicator (via the IsPresentingLoginUI property).

            // trigger the activity indicator through the IsPresentingLoginUI property on the ViewModel


            if (await _authService.Authenticate())
            {
                // Pop off the modally presented SplashPage.
                // Note that we're not popping the ADAL auth UI here; that's done automatcially by the ADAL library when the Authenticate() method returns.
                if (Navigation != null)
                    if (Navigation.ModalStack.Count>0)
                        await Navigation.PopModalAsync();

                // Broadcast a message that we have sucessfully authenticated.
                // This is mostly just for Android. We need to trigger Android to call the SalesDashboardPage.OnAppearing() method,
                // because unlike iOS, Android does not call the OnAppearing() method each time that the Page actually appears on screen.
                //MessagingCenter.Send(this, MessagingServiceConstants.AUTHENTICATED);
            }

            IsPresentingLoginUI = false;
        }

        private string userName;

        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
                OnPropertyChanged("UserName");
            }
        }

        bool _IsPresentingLoginUI;

        public bool IsPresentingLoginUI
        {
            get
            {
                return _IsPresentingLoginUI;
            }
            set
            {
                _IsPresentingLoginUI = value;
                OnPropertyChanged("IsPresentingLoginUI");
            }
        }

    }
}
