using System.Linq.Expressions;
using BLW_Log.Controllers;
using BLW_Log.Models;
using Commonlib;
using DAL.PGModels;
using LiteDB;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;

namespace BLW_Log.Push
{
    public class DataPush : Hub
    {
        public static string TongJiKey = "CountTongJi_";
        public readonly IMemoryCache _memoryCache;
        public readonly PostgresContext dbcontext;
        public DataPush(IMemoryCache m, PostgresContext p)
        {
            this._memoryCache = m;
            this.dbcontext = p;
        }
        public async Task GetTakeCardStatus(string hostid, string hostnumber)
        {

            try
            {
                //ReturnInfo info = new ReturnInfo();
                //info.isok = true;
                try
                {

                    //string hostid = item.HostID;

                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("hostid", hostid);
                    string sss = LowerMachineLogController.Send_Http_Request_Params("api/CheckGetTakePower", dic);

                    if (!string.IsNullOrEmpty(sss))
                    {
                        Dictionary<string, string> ddd = new Dictionary<string, string>();
                        ddd.Add("HostID", hostid);
                        ddd.Add("TakeCard_Status", sss);

                        string? LastDateTime = "";
                        using var q = new PostgresContext();
                        var gha = q.StatisticsTotals.Where(A => A.HostNumber.Equals(hostnumber)).FirstOrDefault();
                        if (gha != null)
                        {
                            LastDateTime = gha.UpdateTime;
                        }
                        ddd.Add("LastDateTime", LastDateTime);
                        string js = JsonConvert.SerializeObject(ddd);
                        await Clients.Caller.SendAsync("TakeCardReceiveMessage", js);
                    }
                    //Monitor_Host_Wrap LG = lll.GetMonitorDataList();
                    //var LLL = LG.Data;
                    //foreach (var item in LLL)
                    //{
                    //    string hostid = item.HostID;

                    //    Dictionary<string, string> dic = new Dictionary<string, string>();
                    //    dic.Add("hostid", hostid);
                    //    string sss = LowerMachineLogController.Send_Http_Request_Params("api/CheckGetTakePower", dic);

                    //    Dictionary<string, string> ddd = new Dictionary<string, string>();
                    //    ddd.Add("MAC", item.MAC);
                    //    ddd.Add("TakeCard_Status", sss);

                    //    string js = JsonConvert.SerializeObject(ddd);
                    //    await Clients.Caller.SendAsync("ReceiveMessage", js);
                    //}
                }
                catch (Exception ex)
                {
                    //info.message = ex.Message;
                }
            }
            catch (Exception)
            {

            }
        }
        public async Task SendMessage(string user, string message)
        {
            List<Monitor_Host_EX> FinallyResult = null;
            if (_memoryCache.TryGetValue(LowerMachineLogController.JianKongLieBiao, out FinallyResult))
            {

                //using var a = new PostgresContext();
                //using var db = new LiteDatabase(@"MyData.db");
                //var col = db.GetCollection<LiteDB_Monitor_Host>("LiteDB_Monitor_Host");
                if (FinallyResult?.Count! > 0)
                {
                    foreach (var item in FinallyResult)
                    {

                        //string KKK = TongJiKey + item.MAC;
                        //col.EnsureIndex(X => X.MAC);
                        //mm = col.FindOne(A => A.MAC.Equals(item.MAC));

                        Monitor_Host_EX_Cache mm = null;
                        StatisticsTotal nn1 = dbcontext.StatisticsTotals.FirstOrDefault(A => A.Mac.Equals(item.MAC));
                        if (nn1 != null)
                        {
                            mm = new Monitor_Host_EX_Cache();
                            mm.RX = nn1.RxCount;
                            mm.TX = nn1.TxCount;
                            mm.MAC = nn1.Mac;
                            mm.LastData = nn1.UpdateTime;
                        }
                        else
                        {

                            Monitor_Host_EX ex = new Monitor_Host_EX();
                            var cccount1 = dbcontext.DevMonitorlogs.Where(A => A.HotelCode.Equals(item.HotelCode) && A.RoomNo.Equals(item.RoomNumber) && A.SendOrReceive.Equals(ConstKey.Send_TX)).Select(A => A.Id).Count();
                            var cccount2 = dbcontext.DevMonitorlogs.Where(A => A.HotelCode.Equals(item.HotelCode) && A.RoomNo.Equals(item.RoomNumber) && A.SendOrReceive.Equals(ConstKey.Receive_RX)).Select(A => A.Id).Count();

                            var qq = dbcontext.DevMonitorlogs.Where(A => A.HotelCode.Equals(item.HotelCode) && A.RoomNo.Equals(item.RoomNumber)).Select(A => A.Id);
                            if (qq.Count() > 0)
                            {
                                var ddd = qq.Max();
                                var entityLLL = dbcontext.DevMonitorlogs.FirstOrDefault(A => A.Id == ddd);

                                if (entityLLL != null)
                                {

                                    //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                    ex.LastData = entityLLL.CreateTime?.ToString("yyyy-MM-dd HH:mm:ss");
                                }
                            }
                            else
                            {
                                ex.LastData = "0000-00-00 00:00:00";
                            }
                            ex.MAC = item.MAC;
                            ex.TX = cccount1;
                            ex.RX = cccount2;

                            mm = new Monitor_Host_EX_Cache();
                            mm.MAC = ex.MAC;
                            mm.TX = ex.TX;
                            mm.RX = ex.RX;
                            mm.LastData = ex.LastData;


                            StatisticsTotal statisticsTotal = new StatisticsTotal();
                            statisticsTotal.Mac = ex.MAC;
                            statisticsTotal.TxCount = ex.TX;
                            statisticsTotal.RxCount = ex.RX;
                            statisticsTotal.UpdateTime = ex.LastData;

                            dbcontext.StatisticsTotals.Add(statisticsTotal);
                            await dbcontext.SaveChangesAsync();
                        }

                        string str = JsonConvert.SerializeObject(mm);
                        await Clients.Caller.SendAsync("ReceiveMessage", str);
                    }
                }
            }
        }
    }
}
