using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WorkFlowCheck.MAUI.Services;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.DependencyInjection;
using CommunityToolkit.Maui;
using WorkFlowCheck.MAUI.DataLayer;
using WorkFlowCheck.MAUI.Mapper;
using WorkFlowCheck.MAUI.Services.Interfaces;
using DevExpress.Maui;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui.Core;
using WorkFlowCheck.MAUI.Handlers;

namespace WorkFlowCheck.MAUI
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder.UseDevExpress(useLocalization: false)
                .UseDevExpressControls()
                .UseDevExpressCollectionView();
            builder.Services.AddAutoMapper(typeof(MapperProfile));
            // appsettings.json beállítása
            string jsonFilePath = Path.Combine(FileSystem.AppDataDirectory, "appsettings.json");
            // Másold a fájlt a Raw erőforrásokból az AppDataDirectory-ba, ha még nincs ott
            if (!File.Exists(jsonFilePath))
            {
                using var stream = FileSystem.OpenAppPackageFileAsync("appsettings.json").Result;
                using var reader = new StreamReader(stream);
                File.WriteAllText(jsonFilePath, reader.ReadToEnd());
            }

            var configuration = new ConfigurationBuilder().SetBasePath(FileSystem.AppDataDirectory) // A konfigurációs fájl betöltése az alkalmazás adat könyvtárából
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
                       

            builder.Services.AddHttpClient();
            builder.Services.AddDbContext<AppDbContext>();

            builder.Services.AddSingleton<IConfiguration>(configuration);
            builder.Services.AddSingleton<IUserService, UserService>();
            builder.Services.AddSingleton<ISyncService, SyncService>();
            builder.Services.AddSingleton<IBaseStockService, BaseStockService>();
            builder.Services.AddSingleton<ICheckListService, CheckListService>();

            builder.Services.AddTransient<MainPage>();

            builder.UseMauiApp<App>()
                .UseDevExpressEditors()
                .UseMauiCommunityToolkitCamera()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                    fonts.AddFont("FontAwesome.ttf", "FontAwesome");
                })
                .UseMauiCommunityToolkitCore()
                .UseMauiCommunityToolkit();
#if DEBUG
            builder.Logging.AddDebug();
#endif
            
            var app = builder.Build();

            ServiceProvider = app.Services;
            return app;
        }

        public static IServiceProvider ServiceProvider { get; private set; }
    }
}