using Domain;
using Domain.Weapons;
using Domain.XML_Model;
using Infrastructure;
using System.Collections.Generic;

namespace DataLayer;

public static class DbContextInitializer
{
    public static void Initialize(DeathwatchDbContext context)
    {
        context.Database.EnsureCreated();

        // Look for any students.
        if (context.Players.Any()) return; // DB has been seeded

        var weapon = new Weapon
        {
            At = "test",
            Range = 30,
            Attribute = "CT",
            Type = "RANGE"
        };

        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> { weapon }
            }
        };
        foreach (var player in players) context.Players.Add(player);
        context.SaveChanges();

        SeedDatabase(context);
    }

    public static void SeedDatabase(DeathwatchDbContext context)
    {
        // Define the paths to the XML files
        var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
        var folderPath = Path.Combine($"{parent}", "Dotnet", "DataLayer", "XML");
        var xmlFiles = Directory.GetFiles(folderPath, "*.xml");

        //string accessoireFilePath = Path.Combine("XML", "Accessoires.xml");
        //string ameliorationFilePath = Path.Combine("XML", "Ameliorations.xml");
        //string extensionFilePath = Path.Combine("XML", "Extensions.xml");
        var roots = new List<Root>();
        try
        {

            foreach (var xmlFile in xmlFiles)
            {
                var root = XmlHelper.DeserializeXmlFile<Root>(xmlFile);
                roots.Add(root);
            }
        }
        catch (Exception e)
        {

            throw;
        }

        // Deserialize the XML files
        foreach (var root in roots)
        {
            if (root != null)
            {
                if (root.Extensions != null && root.Extensions != null && !context.Extensions.Any())
                {
                    context.Extensions.AddRange(root.Extensions);
                }

                if (root.AccessoiresArmes != null && root.AccessoiresArmes != null && !context.Accessoires.Any())
                {
                    context.Accessoires.AddRange(root.AccessoiresArmes);
                }

                if (root.Améliorations != null && root.Améliorations != null && !context.Améliorations.Any())
                {
                    context.Améliorations.AddRange(root.Améliorations);
                }

                if (root.Archétypes != null && root.Archétypes != null && !context.Archétypes.Any())
                {
                    context.Archétypes.AddRange(root.Archétypes);
                }

                if (root.Armes != null && root.Armes != null && !context.Armes.Any())
                {
                    context.Armes.AddRange(root.Armes);
                }

                if (root.Armures != null && root.Armures != null && !context.Armures.Any())
                {
                    context.Armures.AddRange(root.Armures);
                }

                if (root.Assermentations != null && root.Assermentations != null && !context.Assermentations.Any())
                {
                    context.Assermentations.AddRange(root.Assermentations);
                }

                if (root.AttributsArmes != null && root.AttributsArmes != null && !context.Attributs.Any())
                {
                    context.Attributs.AddRange(root.AttributsArmes);
                }

                if (root.Carrières != null && root.Carrières != null && !context.Carrières.Any())
                {
                    context.Carrières.AddRange(root.Carrières);
                }

                if (root.Competences != null && root.Competences != null && !context.Competences.Any())
                {
                    context.Competences.AddRange(root.Competences);
                }

                if (root.Coordonnées != null && root.Coordonnées != null && !context.Coordonnées.Any())
                {
                    context.Coordonnées.AddRange(root.Coordonnées);
                }

                if (root.Cybernétiques != null && root.Cybernétiques != null && !context.Cybernétiques.Any())
                {
                    context.Cybernétiques.AddRange(root.Cybernétiques);

                }

                if (root.Disciplines != null && root.Disciplines != null && !context.Disciplines.Any())
                {
                    context.Disciplines.AddRange(root.Disciplines);
                }

                if (root.Divinations != null && root.Divinations != null && !context.Divinations.Any())
                {
                    context.Divinations.AddRange(root.Divinations);
                }

                if (root.Equipements != null && root.Equipements != null && !context.Equipements.Any())
                {
                    context.Equipements.AddRange(root.Equipements);
                }

                if (root.Implants != null && root.Implants != null && !context.Implants.Any())
                {
                    context.Implants.AddRange(root.Implants);
                }

                if (root.Mondes != null && root.Mondes != null && !context.Mondes.Any())
                {
                    context.Mondes.AddRange(root.Mondes);
                }

                if (root.Mutations != null && root.Mutations != null && !context.Mutations.Any())
                {
                    context.Mutations.AddRange(root.Mutations);
                }

                if (root.Pouvoirs != null && root.Pouvoirs != null && !context.Pouvoirs.Any())
                {
                    context.Pouvoirs.AddRange(root.Pouvoirs);
                }

                if (root.Promotions != null && root.Promotions != null && !context.Promotions.Any())
                {
                    context.Promotions.AddRange(root.Promotions);
                }

                if (root.SeuilsXp != null && root.SeuilsXp != null && !context.Seuils.Any())
                {
                    context.Seuils.AddRange(root.SeuilsXp);
                }

                if (root.Sousmondes != null && root.Sousmondes != null && !context.Sousmondes.Any())
                {
                    context.Sousmondes.AddRange(root.Sousmondes);
                }

                if (root.Talents != null && root.Talents != null && !context.Talents.Any())
                {
                    context.Talents.AddRange(root.Talents);
                }

                if (root.Traits != null && root.Traits != null && !context.Traits.Any())
                {
                    context.Traits.AddRange(root.Traits);
                }

                if (root.Troubles != null && root.Troubles != null && !context.Troubles.Any())
                {
                    context.Troubles.AddRange(root.Troubles);
                }

                if (root.TypeMondes != null && root.TypeMondes != null && !context.TypeMondes.Any())
                {
                    context.TypeMondes.AddRange(root.TypeMondes);
                }

                if (root.TypesNom != null && root.TypesNom != null && !context.TypesNom.Any())
                {
                    context.TypesNom.AddRange(root.TypesNom);
                }
            }
        }


        context.SaveChanges();
    }
}