using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
//using AsbCloudInfrastructure.Services.Cache;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    
    class Program
    {
        static void Main(/*string[] args*/)
        {
            TypeAdapterConfig.GlobalSettings.Default.Config
                .ForType<DateTimeOffset, DateTime>()
                .MapWith((source) => source.DateTime);

            TypeAdapterConfig.GlobalSettings.Default.Config
                .ForType<DateTime, DateTimeOffset>()
                .MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);

            TypeAdapterConfig.GlobalSettings.Default.Config
                .ForType<TimeDto, TimeOnly>()
                .MapWith((source) => source == default? default: source.MakeTimeOnly());

            TypeAdapterConfig.GlobalSettings.Default.Config
                .ForType<TimeOnly, TimeDto>()
                .MapWith((source) => new (source));

            var sh = new ScheduleDto{
                ShiftStart = new TimeDto { Hour = 11, Minute = 30, }
            };
            var en = sh.Adapt<Schedule>();
            var aa = en.Adapt<ScheduleDto>();
            // use ServiceFactory to make services
            Console.WriteLine("hit keyboard to start");
            Console.ReadLine();

            for (int i = 0; i < 24; i++)
            {
                //Thread.Sleep(3000);
                var t = new Thread(_ => {
                    for (int j = 0; j < 64; j++)
                        //Task.Run(GetClastersAsync).Wait();
                        GetClasters();
                });
                t.Start();
            }

            Console.WriteLine("End of Test");
            Console.ReadLine();
        }

        static TimeSpan obso = TimeSpan.FromSeconds(5);
        static (long, long) GetClasters()
        {
            using var db = ServiceFactory.MakeContext();
            var sw = System.Diagnostics.Stopwatch.StartNew();
            var cs = db.TelemetryDataSaub
                .Where(t => t.IdTelemetry == 135)
                .OrderBy(t => t.DateTime)
                .Take(100_000)
                .FromCache("tds", obso, r=>(r.IdTelemetry, r.DateTime))
                .ToList();
            sw.Stop();
            Console.WriteLine($"{DateTime.Now}\tth: {Thread.CurrentThread.ManagedThreadId}\trequests {EfCacheL2.RequestsToDb}\ttime {sw.ElapsedMilliseconds}\tcount {cs.Count}");
            //Console.WriteLine($"{DateTime.Now}\tth: {Thread.CurrentThread.ManagedThreadId}\ttime {sw.ElapsedMilliseconds}\tcount {cs.Count}");

            GC.Collect();
            Thread.Sleep(100);
            return (cs.Count, sw.ElapsedMilliseconds);
        }

        static async Task<(long, long)> GetClastersAsync()
        {
            using var db = ServiceFactory.MakeContext();
            var sw = System.Diagnostics.Stopwatch.StartNew();
            var cs = ( await db.TelemetryDataSaub
                .Where(t => t.IdTelemetry == 135)
                .OrderBy(t => t.DateTime)
                .Take(100_000)
                .FromCacheAsync("tds", obso, r => (r.IdTelemetry, r.DateTime)))
                .ToList();
            sw.Stop();
            Console.WriteLine($"{DateTime.Now}\tth: {Thread.CurrentThread.ManagedThreadId}\trequests {EfCacheL2.RequestsToDb}\ttime {sw.ElapsedMilliseconds}\tcount {cs.Count}");
            //Console.WriteLine($"{DateTime.Now}\tth: {Thread.CurrentThread.ManagedThreadId}\ttime {sw.ElapsedMilliseconds}\tcount {cs.Count}");

            GC.Collect();
            Thread.Sleep(100);
            return (cs.Count, sw.ElapsedMilliseconds);
        }
    }
}