using Tiobon.Core.Hubs;
using Microsoft.AspNetCore.SignalR;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Tiobon.Core.Extensions.Middlewares;

namespace Tiobon.Core.Controllers
{
    [Route("api/[Controller]/[action]")]
    [ApiController]
    [AllowAnonymous, ApiExplorerSettings(GroupName = Grouping.GroupName_System)]
    public class MonitorController : BaseApiController
    {
        private readonly IHubContext<ChatHub> _hubContext;
        private readonly IWebHostEnvironment _env;
        private readonly IApplicationUserServices _applicationUserServices;
        private readonly ILogger<MonitorController> _logger;

        public MonitorController(IHubContext<ChatHub> hubContext, IWebHostEnvironment env, IApplicationUserServices applicationUserServices, ILogger<MonitorController> logger)
        {
            _hubContext = hubContext;
            _env = env;
            _applicationUserServices = applicationUserServices;
            _logger = logger;
        }

        /// <summary>
        /// 服务器配置信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ServiceResult<ServerViewModel> Server()
        {
            return Success(new ServerViewModel()
            {
                EnvironmentName = _env.EnvironmentName,
                OSArchitecture = RuntimeInformation.OSArchitecture.ObjToString(),
                ContentRootPath = _env.ContentRootPath,
                WebRootPath = _env.WebRootPath,
                FrameworkDescription = RuntimeInformation.FrameworkDescription,
                MemoryFootprint = (Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB",
                WorkingTime = DateHelper.TimeSubTract(DateTime.Now, Process.GetCurrentProcess().StartTime)
            }, "获取服务器配置信息成功");
        }


        /// <summary>
        /// SignalR send data
        /// </summary>
        /// <returns></returns>
        // GET: api/Logs
        [HttpGet]
        public ServiceResult<List<LogInfo>> Get()
        {
            if (AppSettings.app(new string[] { "Middleware", "SignalRSendLog", "Enabled" }).ObjToBool())
            {
                _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
            }
            return Success<List<LogInfo>>(null, "执行成功");
        }



        [HttpGet]
        public ServiceResult<RequestApiWeekView> GetRequestApiinfoByWeek()
        {
            return Success(LogLock.RequestApiinfoByWeek(), "成功");
        }

        [HttpGet]
        public ServiceResult<AccessApiDateView> GetAccessApiByDate()
        {
            //return new ServiceResult<AccessApiDateView>()
            //{
            //    msg = "获取成功",
            //    success = true,
            //    response = LogLock.AccessApiByDate()
            //};

            return Success(LogLock.AccessApiByDate(), "获取成功");
        }

        [HttpGet]
        public ServiceResult<AccessApiDateView> GetAccessApiByHour()
        {
            //return new ServiceResult<AccessApiDateView>()
            //{
            //    msg = "获取成功",
            //    success = true,
            //    response = LogLock.AccessApiByHour()
            //};

            return Success(LogLock.AccessApiByHour(), "获取成功");
        }

        private List<UserAccessModel> GetAccessLogsToday(IWebHostEnvironment environment)
        {
            List<UserAccessModel> userAccessModels = new();
            var accessLogs = LogLock.ReadLog(
                Path.Combine(environment.ContentRootPath, "Log"), "RecordAccessLogs_", Encoding.UTF8, ReadType.PrefixLatest
                ).ObjToString();
            try
            {
                return JsonConvert.DeserializeObject<List<UserAccessModel>>("[" + accessLogs + "]");
            }
            catch (Exception)
            {
                var accLogArr = accessLogs.Split("\n");
                foreach (var item in accLogArr)
                {
                    if (item.ObjToString() != "")
                    {
                        try
                        {
                            var accItem = JsonConvert.DeserializeObject<UserAccessModel>(item.TrimEnd(','));
                            userAccessModels.Add(accItem);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

            }

            return userAccessModels;
        }
        private List<ActiveUserVM> GetAccessLogsTrend(IWebHostEnvironment environment)
        {
            List<ActiveUserVM> userAccessModels = new();
            var accessLogs = LogLock.ReadLog(
                Path.Combine(environment.ContentRootPath, "Log"), "ACCESSTRENDLOG_", Encoding.UTF8, ReadType.PrefixLatest
                ).ObjToString();
            try
            {
                return JsonConvert.DeserializeObject<List<ActiveUserVM>>(accessLogs);
            }
            catch (Exception)
            {
                var accLogArr = accessLogs.Split("\n");
                foreach (var item in accLogArr)
                {
                    if (item.ObjToString() != "")
                    {
                        try
                        {
                            var accItem = JsonConvert.DeserializeObject<ActiveUserVM>(item.TrimStart('[').TrimEnd(']'));
                            userAccessModels.Add(accItem);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }

            }

            return userAccessModels;
        }

        [HttpGet]
        public ServiceResult<WelcomeInitData> GetActiveUsers([FromServices] IWebHostEnvironment environment)
        {
            var accessLogsToday = GetAccessLogsToday(environment).Where(d => d.BeginTime.ObjToDate() >= DateTime.Today);

            var Logs = accessLogsToday.OrderByDescending(d => d.BeginTime).Take(50).ToList();

            var errorCountToday = LogLock.GetLogData().Where(d => d.Import == 9).Count();

            accessLogsToday = accessLogsToday.Where(d => d.User != "").ToList();

            var activeUsers = (from n in accessLogsToday
                               group n by new { n.User } into g
                               select new ActiveUserVM
                               {
                                   user = g.Key.User,
                                   count = g.Count(),
                               }).ToList();

            int activeUsersCount = activeUsers.Count;
            activeUsers = activeUsers.OrderByDescending(d => d.count).Take(10).ToList();

            //return new ServiceResult<WelcomeInitData>()
            //{
            //    msg = "获取成功",
            //    success = true,
            //    response = new WelcomeInitData()
            //    {
            //        activeUsers = activeUsers,
            //        activeUserCount = activeUsersCount,
            //        errorCount = errorCountToday,
            //        logs = Logs,
            //        activeCount = GetAccessLogsTrend(environment)
            //    }
            //};

            return Success(new WelcomeInitData()
            {
                activeUsers = activeUsers,
                activeUserCount = activeUsersCount,
                errorCount = errorCountToday,
                logs = Logs,
                activeCount = GetAccessLogsTrend(environment)
            }, "获取成功");
        }

        [HttpGet]
        public async Task<ServiceResult<AccessApiDateView>> GetIds4Users()
        {
            List<ApiDate> apiDates = new List<ApiDate>();

            if (_applicationUserServices.IsEnable())
            {
                var users = await _applicationUserServices.Query(d => d.tdIsDelete == false);

                apiDates = (from n in users
                            group n by new { n.birth.Date } into g
                            select new ApiDate
                            {
                                date = g.Key?.Date.ToString("yyyy-MM-dd"),
                                count = g.Count(),
                            }).ToList();

                apiDates = apiDates.OrderByDescending(d => d.date).Take(30).ToList();
            }


            if (apiDates.Count == 0)
            {
                apiDates.Add(new ApiDate()
                {
                    date = "没数据,或未开启相应接口服务",
                    count = 0
                });
            }
            //return new ServiceResult<AccessApiDateView>()
            //{
            //    msg = "获取成功",
            //    success = true,
            //    response = new AccessApiDateView
            //    {
            //        columns = new string[] { "date", "count" },
            //        rows = apiDates.OrderBy(d => d.date).ToList(),
            //    }
            //};

            return Success(new AccessApiDateView
            {
                columns = new string[] { "date", "count" },
                rows = apiDates.OrderBy(d => d.date).ToList(),
            }, "获取成功");
        }

    }

    public class WelcomeInitData
    {
        public List<ActiveUserVM> activeUsers { get; set; }
        public int activeUserCount { get; set; }
        public List<UserAccessModel> logs { get; set; }
        public int errorCount { get; set; }
        public List<ActiveUserVM> activeCount { get; set; }
    }

}
