using Domain;
using Domain.Weapons;
using Domain.XML_Model;
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Numerics;

namespace DataLayer;

public static class DbContextInitializer
{
    private static bool _isSeeded = false;
    public static async Task Initialize(DeathwatchDbContext context, string? filepath = null)
    {
        await context.Database.EnsureCreatedAsync();
        await SeedDatabase(context, filepath);
    }

    private static async Task SeedPlayers(DeathwatchDbContext context)
    {
        if (context.Players.Any()) return;
        var players = new[]
        {
            new Player
            {
                Experience = 17400,
                CC = 52,
                CT = 40,
                F = 46,
                E = 45,
                AG = 50,
                INT = 34,
                PER = 43,
                FM = 43,
                SOC = 40,
                Weapons = new List<Weapon>
                {
                    new()
                    {
                        At = "test",
                        Range = 30,
                        Attribute = "CT",
                        Type = "RANGE"
                    }
                }
            }
        };
        foreach (var player in players) await context.Players.AddAsync(player);
        await context.SaveChangesAsync();
    }

    public static async Task SeedDatabase(DeathwatchDbContext context, string? filesPath = null)
    {
        if (_isSeeded) return;
        await SeedPlayers(context);

        if (string.IsNullOrEmpty(filesPath)) return;
        var roots = new List<Root>();
        GetRootsElements(roots, filesPath);
        await ExtractFromRootAsync(context, roots);
        _isSeeded = true;
    }

    private static void GetRootsElements(List<Root> roots, string filesPath)
    {
        var fileExist = Directory.Exists(filesPath);
        if (!fileExist) return;
        var xmlFiles = Directory.GetFiles(filesPath, "*.xml");

        roots.AddRange(xmlFiles.Select(XmlHelper.DeserializeXmlFile<Root>));
    }

    private static async Task ExtractFromRootAsync(DeathwatchDbContext context, List<Root> roots)
    {
        foreach (var root in roots.OfType<Root>())
        {
            if (!context.Accessoires.Any())
                context.Accessoires.AddRange(root.AccessoiresArmes);
            else
                foreach (var item in
                         root.AccessoiresArmes.Where(item => !context.Accessoires.Any(e => e.Id == item.Id)))
                    await context.Accessoires.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Améliorations.Any())
                context.Améliorations.AddRange(root.Améliorations);
            else
                foreach (var item in root.Améliorations.Where(item => !context.Améliorations.Any(e => e.Id == item.Id)))
                    await context.Améliorations.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Archétypes.Any())
                context.Archétypes.AddRange(root.Archétypes);
            else
                foreach (var item in root.Archétypes.Where(item => !context.Archétypes.Any(e => e.Id == item.Id)))
                    await context.Archétypes.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Armes.Any())
                context.Armes.AddRange(root.Armes);
            else
                foreach (var item in root.Armes.Where(item => !context.Armes.Any(e => e.Id == item.Id)))
                    await context.Armes.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Armures.Any())
                context.Armures.AddRange(root.Armures);
            else
                foreach (var item in root.Armures.Where(item => !context.Armures.Any(e => e.Id == item.Id)))
                    await context.Armures.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Assermentations.Any())
                context.Assermentations.AddRange(root.Assermentations);
            else
                foreach (var item in root.Assermentations.Where(item =>
                             !context.Assermentations.Any(e => e.Id == item.Id)))
                    await context.Assermentations.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Attributs.Any())
                context.Attributs.AddRange(root.AttributsArmes);
            else
                foreach (var item in root.AttributsArmes.Where(item => !context.Attributs.Any(e => e.Id == item.Id)))
                    await context.Attributs.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Carrières.Any())
                context.Carrières.AddRange(root.Carrières);
            else
                foreach (var item in root.Carrières.Where(item => !context.Carrières.Any(e => e.Id == item.Id)))
                    await context.Carrières.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Competences.Any())
                context.Competences.AddRange(root.Competences);
            else
                foreach (var item in root.Competences.Where(item => !context.Competences.Any(e => e.Id == item.Id)))
                    await context.Competences.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Coordonnées.Any())
                context.Coordonnées.AddRange(root.Coordonnées);
            else
                foreach (var item in root.Coordonnées.Where(item => !context.Coordonnées.Any(e => e.Id == item.Id)))
                    await context.Coordonnées.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Cybernétiques.Any())
                context.Cybernétiques.AddRange(root.Cybernétiques);
            else
                foreach (var item in root.Cybernétiques.Where(item => !context.Cybernétiques.Any(e => e.Id == item.Id)))
                    await context.Cybernétiques.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Disciplines.Any())
                context.Disciplines.AddRange(root.Disciplines);
            else
                foreach (var item in root.Disciplines.Where(item => !context.Disciplines.Any(e => e.Id == item.Id)))
                    await context.Disciplines.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Divinations.Any())
                context.Divinations.AddRange(root.Divinations);
            else
                foreach (var item in root.Divinations.Where(item => !context.Divinations.Any(e => e.Id == item.Id)))
                    await context.Divinations.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Equipements.Any())
                context.Equipements.AddRange(root.Equipements);
            else
                foreach (var item in root.Equipements.Where(item => !context.Equipements.Any(e => e.Id == item.Id)))
                    await context.Equipements.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Implants.Any())
                context.Implants.AddRange(root.Implants);
            else
                foreach (var item in root.Implants.Where(item => !context.Implants.Any(e => e.Id == item.Id)))
                    await context.Implants.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Mondes.Any())
                context.Mondes.AddRange(root.Mondes);
            else
                foreach (var item in root.Mondes.Where(item => !context.Mondes.Any(e => e.Id == item.Id)))
                    await context.Mondes.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Mutations.Any())
                context.Mutations.AddRange(root.Mutations);
            else
                foreach (var item in root.Mutations.Where(item => !context.Mutations.Any(e => e.Id == item.Id)))
                    await context.Mutations.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Pouvoirs.Any())
                context.Pouvoirs.AddRange(root.Pouvoirs);
            else
                foreach (var item in root.Pouvoirs.Where(item => !context.Pouvoirs.Any(e => e.Id == item.Id)))
                    await context.Pouvoirs.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Promotions.Any())
                context.Promotions.AddRange(root.Promotions);
            else
                foreach (var item in root.Promotions.Where(item => !context.Promotions.Any(e => e.Id == item.Id)))
                    await context.Promotions.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Seuils.Any())
                context.Seuils.AddRange(root.SeuilsXp);
            else
                foreach (var item in root.SeuilsXp.Where(item => !context.Seuils.Any(e => e.Id == item.Id)))
                    await context.Seuils.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Sousmondes.Any())
                context.Sousmondes.AddRange(root.Sousmondes);
            else
                foreach (var item in root.Sousmondes.Where(item => !context.Sousmondes.Any(e => e.Id == item.Id)))
                    await context.Sousmondes.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Traits.Any())
                context.Traits.AddRange(root.Traits);
            else
                foreach (var item in root.Traits.Where(item => !context.Traits.Any(e => e.Id == item.Id)))
                    await context.Traits.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Troubles.Any())
                context.Troubles.AddRange(root.Troubles);
            else
                foreach (var item in root.Troubles.Where(item => !context.Troubles.Any(e => e.Id == item.Id)))
                    await context.Troubles.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.TypeMondes.Any())
                context.TypeMondes.AddRange(root.TypeMondes);
            else
                foreach (var item in root.TypeMondes.Where(item => !context.TypeMondes.Any(e => e.Id == item.Id)))
                    await context.TypeMondes.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.TypesNom.Any())
                context.TypesNom.AddRange(root.TypesNom);
            else
                foreach (var item in root.TypesNom.Where(item => !context.TypesNom.Any(e => e.Id == item.Id)))
                    await context.TypesNom.AddAsync(item);

            await context.SaveChangesAsync();

            if (!context.Talents.Any())
                context.Talents.AddRange(root.Talents);
            else
                foreach (var item in root.Talents)
                {
                    if (!context.Talents.AsNoTracking().Any(e => e.Id == item.Id))
                        await context.Talents.AddAsync(item);
                    await context.SaveChangesAsync();

                }

        }
    }
}