using Borepin.ViewModel;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Commands;
using Prism.Navigation;
using Borepin.Base;
using FabAccessAPI.Schema;
using Prism.Services;
using System;
using NaturalSort.Extension;
using System.Linq;
using Borepin.Service;
using Xamarin.Forms;

namespace Borepin.PageModel
{
    public class UserListPageModel : ConnectionModelBase
    {
        #region Constructors
        public UserListPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService) : base(navigationService, pageDialogService, apiService)
        {
            RefreshCommand = new DelegateCommand(async ()=> await RefreshCommandExecute().ConfigureAwait(true));
            AddUserCommand = new DelegateCommand(AddUserCommandExecute);
        }
        #endregion

        #region Data
        public override async Task LoadAPIData()
        {
            List<Task> tasks = new List<Task>();

            IReadOnlyList<User> user_list = await _API.Session.UserSystem.Manage.GetUserList().ConfigureAwait(false);

            List<UserListItemViewModel> viewmodel_list= new List<UserListItemViewModel>();
            foreach (User user in user_list)
            {
                UserListItemViewModel new_viewmodel = new UserListItemViewModel(_NavigationService, _PageDialogService);
                tasks.Add(new_viewmodel.LoadInstance(user));
                viewmodel_list.Add(new_viewmodel);
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            viewmodel_list.OrderBy(x => x.Instance.Username, StringComparison.OrdinalIgnoreCase.WithNaturalSort());
            UserListItemViewModel_List = viewmodel_list;
        }
        #endregion

        #region Fields
        private IList<UserListItemViewModel> _UserListItemViewModel_List;
        public IList<UserListItemViewModel> UserListItemViewModel_List
        {
            get => _UserListItemViewModel_List;
            set => SetProperty(ref _UserListItemViewModel_List, value);
        }

        private bool _IsRefreshing;
        public bool IsRefreshing
        {
            get => _IsRefreshing;
            set => SetProperty(ref _IsRefreshing, value);
        }
        #endregion

        #region Commands
        private ICommand _RefreshCommand;
        public ICommand RefreshCommand
        {
            get => _RefreshCommand;
            set => SetProperty(ref _RefreshCommand, value);
        }
        public async Task RefreshCommandExecute()
        {
            if(_API.IsConnected)
            {
                await LoadAPIData().ConfigureAwait(true);
            }
            
            IsRefreshing = false;
        }

        private ICommand _AddUserCommand;
        public ICommand AddUserCommand
        {
            get => _AddUserCommand;
            set => SetProperty(ref _AddUserCommand, value);
        }
        public void AddUserCommandExecute()
        {
            Device.BeginInvokeOnMainThread(async () =>
            {
                INavigationResult result = await _NavigationService.NavigateAsync("AddUserPage").ConfigureAwait(false);
                if (result.Exception != null)
                {
                    Log.Fatal(result.Exception, "Navigating failed");
                }
            });
        }
        #endregion
    }
}
