using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PhilExample.Interfaces;
using PhilExample.UI.Pages;
using Xamarin.Forms;

namespace PhilExample.VMs
{
    internal class StartupVM : BaseVM
    {
        public override async Task OnAppearingAsync()
        {
            var userService = DependencyService.Get<IUserService>();
            var ekaService = DependencyService.Get<IExamplePageService>();
            var storageService = DependencyService.Get<IPhExStorage>();
            bool loggedIn = await userService.LoginAsync(15);

            if (!loggedIn)
                return;

            //TODO: await Task.WhenAll()
            var cats = await ekaService.GetCategoriesAsync();
            var radii = await ekaService.GetRadiiAsync();
            if (cats == null || !cats.Any() ||
                radii == null || !radii.Any())
                return;

            if (!await storageService.SaveCategoriesAsync(cats) ||
                !await storageService.SaveRadiiAsync(radii))
                return;

            await Shell.Current.GoToAsync($"///main//{nameof(DashboardPage)}");
        }
    }
}
