using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Spreadsheet;
using Infrastructure;
using Infrastructure.Cache;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using OpenAuth.App.ServiceApp;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository.Domain;
using StackExchange.Redis;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
    /// <summary>
    /// 机场运维
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AirportMaintenanceController : ControllerBase
    {
        private readonly AirportMaintenanceApp _app;
        private readonly MqttClientManager _mqttClientManager;
        private readonly MqttMessageCenter _mqttCenter;
        private readonly RedisCacheContext _cache;

        public AirportMaintenanceController(AirportMaintenanceApp app, MqttClientManager mqttClientManager, MqttMessageCenter mqttCenter, RedisCacheContext cache)
        {
            _app = app;
            _mqttClientManager = mqttClientManager;
            _mqttCenter = mqttCenter;
            _cache = cache;
        }
        /// <summary>
        /// 机场注册 注册码生成
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public async Task<Response<LasaDeviceBindingCode>> GetDeviceBindingCode()
        {
            var result = new Response<LasaDeviceBindingCode>();
            try
            {
                result = await _app.GetDeviceBindingCode();
                //发送MQTT消息
                if (result.Code == 200 && result.Result != null)
                {
                    var topicRequest = $"thing/product/{result.Result.DeviceBindingCode}/requests";
                    var requestData = new
                    {
                        bid = Guid.NewGuid().ToString(),
                        method = "airport_organization_bind",
                        tid = Guid.NewGuid().ToString(),
                        timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                        data = new
                        {
                            bind_devices = new[]
                            {
                                new
                                {
                                    device_binding_code = result.Result.DeviceBindingCode,
                                    organization_id = result.Result.OrgId

                                }
                            }
                        }
                    };
                    string payload = JsonSerializer.Serialize(requestData);
                    await _mqttClientManager.PublishAsync(topicRequest, payload);
                    //在这个地方在接收一下？

                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }
        /// <summary>
        /// 机场注册 注册码生成
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<LasaGateway>> GetGateway()
        {
            var result = new Response<LasaGateway>();
            try
            {
                result = await _app.GetGateway();
                //自动更新主题  
                string sn = result.Result.GatewaySn;
                var topics = new List<string>();
                topics.AddRange(new[]
                {
                    $"thing/product/{sn}/osd",
                    $"thing/product/{sn}/events",
                    $"thing/product/{sn}/requests",
                    $"thing/product/{sn}/services"
                });
                await _mqttCenter.SubscribeAsync(topics.ToArray());
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }

        #region 固件版本管理
        /// <summary>
        /// 添加固件版本
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddFirmware(LasaFirmware info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddFirmware(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 切换相机
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<int>> ExchangeCamera(Zhibo zhiboReq)
        {
            Response<int> response = new Response<int>();
            try
            {
                var videoids = zhiboReq.videoId.Split("/");
                var topicRequest = $"thing/product/" + videoids[0] + "/services";
                var requestData = new
                {
                    bid = Guid.NewGuid().ToString(),
                    method = "live_camera_change",
                    tid = Guid.NewGuid().ToString(),
                    timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    data = new
                    {
                        camera_position = zhiboReq.position,
                        video_id = zhiboReq.videoId
                    }
                };
                string payload = JsonSerializer.Serialize(requestData);
                await _mqttClientManager.PublishAsync(topicRequest, payload);

                var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";

                await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
                {
                    var payload = args.ApplicationMessage.Payload;
                    var message = Encoding.UTF8.GetString(payload);
                    Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
                    await Task.CompletedTask;
                });
                response.Result = 0;

            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
            return response; ;

        }



        /// <summary>
        /// 设置直播镜头
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<int>> SetCamera(Zhibo zhiboReq)
        {
            Response<int> response = new Response<int>();
            try
            {

                var videoids = zhiboReq.videoId.Split("/");
                var topicRequest = $"thing/product/" + videoids[0] + "/services";
                var requestData = new
                {
                    bid = Guid.NewGuid().ToString(),
                    method = "live_lens_change",
                    tid = Guid.NewGuid().ToString(),
                    timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    data = new
                    {
                        video_type = zhiboReq.cameraType,
                        video_id = zhiboReq.videoId
                    }
                };
                string payload = JsonSerializer.Serialize(requestData);
                await _mqttClientManager.PublishAsync(topicRequest, payload);

                var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";

                await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
                {
                    var payload = args.ApplicationMessage.Payload;
                    var message = Encoding.UTF8.GetString(payload);
                    Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
                    await Task.CompletedTask;
                });

                response.Result = 0;

            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
            return response;

        }


        /// <summary>
        /// 设置直播清晰度
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<int>> SetCameraVideo(Zhibo zhiboReq)
        {
            Response<int> response = new Response<int>();
            try
            {
                var videoids = zhiboReq.videoId.Split("/");
                var topicRequest = $"thing/product/" + videoids[0] + "/services";
                var requestData = new
                {
                    bid = Guid.NewGuid().ToString(),
                    method = "live_set_quality",
                    tid = Guid.NewGuid().ToString(),
                    timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    data = new
                    {
                        video_quality = zhiboReq.videoQuality,
                        video_id = zhiboReq.videoId
                    }
                };
                string payload = JsonSerializer.Serialize(requestData);
                await _mqttClientManager.PublishAsync(topicRequest, payload);

                var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";

                await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
                {
                    var payload = args.ApplicationMessage.Payload;
                    var message = Encoding.UTF8.GetString(payload);
                    Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
                    await Task.CompletedTask;
                });

                response.Result = 0;

            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
            return response;

        }


        /// <summary>
        /// 停止直播
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<int>> EndLive(Zhibo zhiboReq)
        {
            Response<int> response = new Response<int>();
            try
            {
                var videoids = zhiboReq.videoId.Split("/");
                var topicRequest = $"thing/product/" + videoids[0] + "/services";
                var requestData = new
                {
                    bid = Guid.NewGuid().ToString(),
                    method = "live_stop_push",
                    tid = Guid.NewGuid().ToString(),
                    timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    data = new
                    {

                        video_id = zhiboReq.videoId
                    }
                };
                string payload = JsonSerializer.Serialize(requestData);
                await _mqttClientManager.PublishAsync(topicRequest, payload);

                var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";

                await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
                {
                    var payload = args.ApplicationMessage.Payload;
                    var message = Encoding.UTF8.GetString(payload);
                    Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
                    await Task.CompletedTask;
                });

                response.Result = 0;



            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
            return response;

        }



        /// <summary>
        /// 开始直播
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<int>> StartLive(Zhibo zhiboReq)
        {
            Response<int> response = new Response<int>();
            try
            {
                var videoids = zhiboReq.videoId.Split("/");
                var topicRequest = $"thing/product/" + videoids[0] + "/services";
                var requestData = new
                {
                    bid = Guid.NewGuid().ToString(),
                    method = "live_start_push",
                    tid = Guid.NewGuid().ToString(),
                    timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    data = new
                    {
                        url_type = zhiboReq.urlType,
                        url = zhiboReq.url,
                        video_quality = zhiboReq.videoQuality,
                        video_id = zhiboReq.videoId
                    }
                };
                string payload = JsonSerializer.Serialize(requestData);
                await _mqttClientManager.PublishAsync(topicRequest, payload);

                var topicRequest1 = $"thing/product/" + videoids[0] + "/services_reply";
                await _mqttClientManager.SubscribeAsync(topicRequest1, async (args) =>
                {
                    var payload1 = args.ApplicationMessage.Payload;
                    var message = Encoding.UTF8.GetString(payload1);
                    Console.WriteLine($"收到主题 [{args.ApplicationMessage.Topic}] 的消息: {message}");
                    await Task.CompletedTask;
                });

                response.Result = 0;



            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.Message;
            }
            return response;

        }


        #region redis 多用户控制
        /// <summary>
        /// 添加修改mqtt客户端信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddOrUpdateRedisUser(MqttClientResp info)
        {
            var result = new Response<bool>();
            try
            {
                var clientKey = $"client:{info.UserId}";
                string lockSetKey = "locked_users";

                // 查询所有锁定用户
                var existingLocked = await _cache.SetMembersAsync(lockSetKey);

                // 如果有锁定用户，并且锁定的用户不是当前用户，则拒绝
                if (existingLocked.Length > 0 && info.IsLock == true)
                {
                    bool isCurrentUserLocked = existingLocked.Any(u => u == info.UserId);
                    if (!isCurrentUserLocked)
                    {
                        result.Code = 400;
                        result.Message = "已有其他用户处于锁定状态，不能添加新的锁定用户。";
                        result.Result = false;
                        return result;
                    }
                }

                // 存客户端信息
                _cache.HashSetAsync(clientKey, new HashEntry[]
                {
                    new("ClientId", info.ClientId),
                    new("UserId", info.UserId),
                    new("UserName", info.UserName),
                    new("ConnectTime", info.ConnectTime.ToString("O")),
                    new("IsLock", info.IsLock ? "true" : "false")
                });

                if (info.IsLock)
                {
                    await _cache.SetAddAsync(lockSetKey, info.UserId);
                }
                else
                {
                    await _cache.SetRemoveAsync(lockSetKey, info.UserId);
                }
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取当前用户mqtt客户端信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<MqttClientResp>> GetRedisUser(string id)
        {
            var result = new Response<MqttClientResp>();
            try
            {
                result.Result = ParseClient(await _cache.HashGetAllAsync($"client:{id}"));
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        private MqttClientResp ParseClient(HashEntry[] entries)
        {
            if (entries == null || entries.Length == 0)
                return null;
            var dict = entries.ToDictionary(e => e.Name.ToString(), e => e.Value.ToString());
            return new MqttClientResp
            {
                ClientId = dict["ClientId"],
                UserId = dict["UserId"],
                UserName = dict["UserName"],
                ConnectTime = DateTime.Parse(dict["ConnectTime"]),
                IsLock = bool.Parse(dict["IsLock"])
            };
        }
        /// <summary>
        /// 获取所有锁定的用户客户端信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<MqttClientResp>> GetLockedClients()
        {
            var userIds = await _cache.SetMembersAsync("locked_users");

            var result = new List<MqttClientResp>();

            foreach (var userId in userIds)
            {
                var entries = await _cache.HashGetAllAsync($"client:{userId}");
                if (entries.Length > 0)
                    result.Add(ParseClient(entries));
            }
            return result;
        }
        #endregion

        #region 获取告警信息
        /// <summary>
        /// 获取告警信息
        /// </summary>
        /// <param name="level">告警等级，0-全部，1-注意，2-告警</param>
        /// <param name="model">设备，0-全部，1-设备管理，2-媒体管理，3-HMS告警</param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<LasaManageDeviceHms>>>> GetManageDeviceHmsList(int level, int model, DateTime startTime, DateTime endTime, int page, int limit)
        {
            var result = new Response<PageInfo<List<LasaManageDeviceHms>>>();
            try
            {
                result = await _app.GetManageDeviceHmsList(level, model, startTime, endTime, page, limit);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }
        #endregion
    }
}
