using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Services;
using WorkFlowCheck.MAUI.Services.Interfaces;

namespace WorkFlowCheck.MAUI
{
    public partial class App : Application
    {
        private readonly AppDbContext _dbContext;
        public App(IServiceProvider serviceProvider)
        {
            InitializeComponent();

            _dbContext = serviceProvider.GetRequiredService<AppDbContext>();

            _dbContext.Database.EnsureDeleted(); // Adatbázis törlése
            _dbContext.Database.EnsureCreated(); // Új adatbázis létrehozása


            MainPage = new AppShell();
            //MainPage = new NavigationPage(new MainPage(serviceProvider.GetRequiredService<IUserService>()));

        }
        protected async override void OnResume()
        {
            base.OnResume();
            var isLogged = await SecureStorage.Default.GetAsync("WFCUser");
            if (isLogged == null)
            {

            }
        }
        protected override void OnSleep()
        {
            SecureStorage.Default.Remove("WFCUser");
            base.OnSleep();
        }
        protected async override void OnStart()
        {
            base.OnStart();
            SecureStorage.Default.Remove("WFCUser");
            await Shell.Current.GoToAsync("//LoginPage");
        }
        private async Task<ApiResponseDTO<UserDTO>> CheckSecurity()
        {
            var isLogged = await SecureStorage.Default.GetAsync("WFCUser");
            if (isLogged == null)
            {
                return new ApiResponseDTO<UserDTO>()
                {
                    IsSuccess = false
                };
            }
            return new ApiResponseDTO<UserDTO>()
            {
                IsSuccess = true
            };
        }
    }
}
