using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Repositories;
using Lada.Framework.Infrastructures.Helpers;
using ladaexpress.vn.Infrastructures.DTO;
using ladaexpress.vn.Infrastructures.DTO.Models.Topic;
using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Text.RegularExpressions;

namespace ladaexpress.vn.Infrastructures.Services
{
    public interface ITopicService
    {
        Task<TopicDetailModel> PrepareTopicDetail(int id);


        Task<TopicDetailModel> PrepareTopicDetailBySystemName(string systemName);
    }


    public class TopicService : ITopicService
    {
        private readonly IRepository<Topic> _topicRepository;
        private readonly IRepository<Category> _categoryRepository;
        private readonly IRepository<UrlRecord> _urlRecordRepository;
        private readonly IDbConnection _dbConnection;


        public TopicService(IRepository<Topic> topicRepository, IRepository<UrlRecord> urlRecordRepository,
            IRepository<Category> categoryRepository, IDbConnection dbConnection)
        {
            _topicRepository = topicRepository;
            _urlRecordRepository = urlRecordRepository;
            _categoryRepository = categoryRepository;
            _dbConnection = dbConnection;
        }
        public async Task<TopicDetailModel> PrepareTopicDetail(int id)
        {
            var model = new TopicDetailModel();
            var currentTime = DateTimeHelper.CurrentTime();
            var topic = await _topicRepository.Query().
                Include(p => p.UrlRecord)
                .FirstOrDefaultAsync(p => p.Id == id);
            if (topic != null && topic.Id > 0)
            {
                model = topic.ToDetailModel();



                if (!string.IsNullOrEmpty(model.Description))
                {
                    var plaintext = Regex.Replace(model.Description, "<(.|\n)*?>", "").Trim();
                    char[] delimiters = new char[] { ' ', '\r', '\n' };
                    var wordCount = plaintext.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;
                    int readingSpeed = 200;
                    int estimatedReadingTime = (wordCount * 60) / readingSpeed;
                    // Chuyển thời gian từ giây thành phút và giây
                    int minutes = estimatedReadingTime / 60;
                }


            }
            return model;
        }

        public async Task<TopicDetailModel> PrepareTopicDetailBySystemName(string systemName)
        {
            var model = new TopicDetailModel();
            var currentTime = DateTimeHelper.CurrentTime();

            if (string.IsNullOrEmpty(systemName))
            {
                return model;
            }

            var topic = await _topicRepository.Query().
                Include(p => p.UrlRecord)
                .FirstOrDefaultAsync(p => p.SystemName == systemName);
            if (topic != null && topic.Id > 0)
            {
                model = topic.ToDetailModel();

                if (!string.IsNullOrEmpty(model.Description))
                {
                    var plaintext = Regex.Replace(model.Description, "<(.|\n)*?>", "").Trim();
                    char[] delimiters = new char[] { ' ', '\r', '\n' };
                    var wordCount = plaintext.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;
                    int readingSpeed = 200;
                    int estimatedReadingTime = (wordCount * 60) / readingSpeed;
                    // Chuyển thời gian từ giây thành phút và giây
                    int minutes = estimatedReadingTime / 60;
                }
            }
            return model;
        }
    }
}
