using Gremlin.GremlinData.EntityClasses;
using Gremlin.GremlinUI;
using Microsoft.EntityFrameworkCore;
using System;

namespace Gremlin.GremlinData.DBClasses
{
    public class GremlinContext : DbContext
    {
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Account> Accounts { get; set; }
        public DbSet<Quote> Quotes { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<LineItem> LineItems { get; set; }
        public DbSet<CustomDescription> CustomDescriptions { get; set; }
        public DbSet<ProductLine> ProductLines { get; set; }
        public DbSet<AccountType> AccountTypes { get; set; }
        public DbSet<SubMarket> SubMarkets { get; set; }
        public DbSet<RUSetting> RUSettings { get; set; }
        public DbSet<RegisteredUser> RegisteredUser { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = $"server={Properties.Settings.Default.server};" +
                    $"port={Properties.Settings.Default.port};" +
                    $"database={Properties.Settings.Default.database};" +
                    $"user={Properties.Settings.Default.user};" +
                    $"password={Encryption.ToInsecureString(Encryption.DecryptString(Properties.Settings.Default.password))};" +
                    $"SslMode={Properties.Settings.Default.SslMode};" +
                    $"SslCa={Properties.Settings.Default.SslCA}";

            try
            {
                optionsBuilder
                    .UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
                        //mySqlOptionsAction => mySqlOptionsAction.CharSetBehavior(Pomelo.EntityFrameworkCore.MySql.Infrastructure.CharSetBehavior.NeverAppend)).EnableDetailedErrors()
                        .EnableSensitiveDataLogging()
                        .EnableDetailedErrors();
            }
            catch (Exception ex)
            {
                ErrorHandler.ShowErrorInMessageBox(ex);
                ChooseDB chooseDB = new();
                _ = chooseDB.ShowDialog();
                OnConfiguring(optionsBuilder);
                throw;
            }

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //wozu dient die folgende Zeile?
            base.OnModelCreating(modelBuilder);

            ////alle Fluent-Konfigurationen aufrufen:
            //TO BE TESTED!
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(GremlinContext).Assembly);

            ////Fluent-Konfiguration einzeln für eine Entity aufrufen:
            //new AccountConfiguration().Configure(modelBuilder.Entity<Account>());
            //new ContactConfiguration().Configure(modelBuilder.Entity<Contact>());
            //new QuoteConfiguration().Configure(modelBuilder.Entity<Quote>());
            //new ProductConfiguration().Configure(modelBuilder.Entity<Product>());
            //new LineItemConfiguration().Configure(modelBuilder.Entity<LineItem>());
            //new CustomDescriptionConfiguration().Configure(modelBuilder.Entity<CustomDescription>());
            //new ProductLineConfiguration().Configure(modelBuilder.Entity<ProductLine>());
            //new AccountTypeConfiguration().Configure(modelBuilder.Entity<AccountType>());
            //new SubMarketConfiguration().Configure(modelBuilder.Entity<SubMarket>());
        }
    }
}