using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SlnMesnac.Config;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using SlnMesnac.Redis;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using System.Linq;
using Serilog;
using SlnMesnac.Common;
using ServiceStack.Messaging;

namespace SlnMesnac.Business
{
    public class MessageService : BackgroundService
    {
        private readonly RedisHandler _redisHandler;
        private readonly ILogger<MessageService> _logger;
        private readonly AppConfig _appConfig;
        private IMesProductReadInfoService _mesProductReadInfoService;

        public MessageService(IMesProductReadInfoService mesProductReadInfoService,RedisHandler redisHandler, ILogger<MessageService> logger, IConfiguration configuration)
        {
            _mesProductReadInfoService = mesProductReadInfoService;
            _redisHandler = redisHandler;
            _logger = logger;
            _appConfig = configuration.GetSection("AppConfig").Get<AppConfig>();
           
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    MesProductReadInfo? mesProductReadInfo = _mesProductReadInfoService.Query(x => x.UseFlag == 1 && x.ReadStatus != 2).OrderBy(x => x.ReadId).FirstOrDefault();
                    if (mesProductReadInfo != null)
                    {
                        Log.Information($"总次数:{mesProductReadInfo.ReadTotal}  朗读第次数{mesProductReadInfo.ReadCount}  开始朗读:====>{mesProductReadInfo.ReadInfo}");
                        // SpeechStr.Instance.Speak(mesProductReadInfo.ReadInfo);
                        _redisHandler.PublishMessage("read_messages_public", mesProductReadInfo.ReadInfo);
                        mesProductReadInfo.ReadCount++;
                        mesProductReadInfo.ReadStatus = 1;
                        mesProductReadInfo.UpdateTime = DateTime.Now;
                        if (mesProductReadInfo.ReadCount >= mesProductReadInfo.ReadTotal)
                        {
                            mesProductReadInfo.ReadStatus = 2;
                            mesProductReadInfo.EndTime = DateTime.Now;
                        }
                        _mesProductReadInfoService.Update(mesProductReadInfo);
                        // Thread.Sleep(1500);
                        await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken);
                    }
                    else
                    {
                        await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); // 每10秒检查一次
                       // Thread.Sleep(1000 * 10);
                    }

                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "消息服务发生错误");
                }
            }
        }
    }
}
