using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudInfrastructure.Services.SAUB;
using AsbCloudInfrastructure.Services.WellOperationService;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class ConfigurationService : IConfigurationSection
    {
        public string this[string key]
        {
            get => "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True";
            set { }
        }

        public string Key => "";

        public string Path => "";

        public string Value { get; set; } = "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True";

        public IEnumerable<IConfigurationSection> GetChildren()
        {
            return null;
        }

        public IChangeToken GetReloadToken()
        {
            return null;
        }

        public IConfigurationSection GetSection(string key) => this;

    }

    internal static class ServiceFactory
    {

        private static DbContextOptions<AsbCloudDbContext> options = new DbContextOptionsBuilder<AsbCloudDbContext>()
                .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
                .Options;

        static CacheDb CacheDb { get; } = new CacheDb();
        static ConfigurationService ConfigurationService { get; } = new ConfigurationService();
        static TimezoneService TimezoneService { get; } = new TimezoneService();
        public static AsbCloudDbContext Context { get; } = MakeContext();
        public static AsbCloudDbContext MakeContext()
            => MakeContext(options);

        public static AsbCloudDbContext MakeContext(DbContextOptions<AsbCloudDbContext> options)
            => new AsbCloudDbContext(options);

        public static AsbCloudDbContext MakeContext(string cusomConnectionString)
            => MakeContext(new DbContextOptionsBuilder<AsbCloudDbContext>().UseNpgsql(cusomConnectionString).Options);

        public static void MapsterSetup()
            => AsbCloudInfrastructure.DependencyInjection.MapsterSetup();

        public static TelemetryTracker MakeTelemetryTracker()
            => new TelemetryTracker(CacheDb, ConfigurationService);

        public static TelemetryService MakeTelemetryService()
            => new TelemetryService(Context, MakeTelemetryTracker(), TimezoneService);

        public static WellService MakeWellService()
            => new WellService(Context, CacheDb, MakeTelemetryService(), TimezoneService);

        public static WellOperationService MakeWellOperationsService()
            => new WellOperationService(Context, CacheDb, MakeWellService());

        public static OperationsStatService MakeOperationsStatService()
            => new OperationsStatService(Context, CacheDb, MakeWellService());

        public static ScheduleReportService MakeScheduleReportService()
            => new ScheduleReportService(MakeOperationsStatService(), MakeWellService());
    }
}