using CloudBuilder.AI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CloudBuilder.AI.Service.Utility
{
    /// <summary>
    /// 章节分割器
    /// 不同的小说有自己的分割方法，可以传入参数
    /// </summary>
    public class ChapterSplitter
    {
        public ChapterSplitter()
        {

        }
        /// <summary>
        /// 按章节分割小说文本
        /// </summary>
        public List<AiChapterEntity> SplitNovelIntoChapters(string novelText, string chapterPatterns)
        {
            var chapters = new List<AiChapterEntity>();
            var lines = novelText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            var currentAiChapterEntity = new StringBuilder();
            var currentAiChapterEntityTitle = "未命名章节";
            var chapterCount = 1;

            foreach (var line in lines)
            {
                // 检查是否是章节标题
                if (IsChapterTitle(line, chapterPatterns))
                {
                    // 如果当前章节有内容，则保存
                    if (currentAiChapterEntity.Length > 0)
                    {
                        chapters.Add(new AiChapterEntity
                        {
                            Title = currentAiChapterEntityTitle,
                            Guid = Guid.NewGuid().ToString(),
                            Content = currentAiChapterEntity.ToString(),
                            ChapterIndex = chapterCount
                        });
                        currentAiChapterEntity.Clear();
                        chapterCount++;
                    }

                    // 更新章节标题
                    currentAiChapterEntityTitle = line.Trim();
                    // 添加章节标题
                    currentAiChapterEntity.AppendLine(line.Trim());
                    // 添加一个空行作为分隔，确保章节标题和正文内容分离
                    currentAiChapterEntity.AppendLine();
                }
                else
                {
                    // 添加到当前章节，确保每行独立
                    currentAiChapterEntity.AppendLine(line);
                }
            }

            // 添加最后一个章节
            if (currentAiChapterEntity.Length > 0)
            {
                chapters.Add(new AiChapterEntity
                {
                    Title = currentAiChapterEntityTitle,
                    Content = currentAiChapterEntity.ToString(),
                    Guid = Guid.NewGuid().ToString(),
                    ChapterIndex = chapterCount
                });
            }

            return chapters;
        }

        /// <summary>
        /// 检查是否为章节标题
        /// </summary>
        private bool IsChapterTitle(string line, string cptPattern)
        {
            // 去除首尾空白字符
            var trimmedLine = line.Trim();

            // 常见的章节标题模式
            var chapterPatterns = new[]
            {
                @"^第\s*[\u4e00-\u9fa50-9]+\s*章",  // 第X章, 第XX章等
                @"^第\s*[0-9]+\s*章",               // 第1章, 第2章等
            };

            if (!string.IsNullOrEmpty(cptPattern))
            {
                Array.Resize(ref chapterPatterns, chapterPatterns.Length + 1);
                chapterPatterns[chapterPatterns.Length - 1] = cptPattern;
            }

            foreach (var pattern in chapterPatterns)
            {
                if (Regex.IsMatch(trimmedLine, pattern, RegexOptions.IgnoreCase))
                {
                    //第一章内容说明了什么，请描述一下：
                    if (trimmedLine.Length > 20) return false;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 保存章节到单独的文件
        /// </summary>
        public void SaveChaptersToFiles(List<AiChapterEntity> chapters, string outputDirectory)
        {
            // 确保输出目录存在
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }

            foreach (var chapter in chapters)
            {
                // 使用章节标题作为文件名，清理非法字符
                var fileName = CleanFileName(chapter.Title) + ".txt";
                var filePath = Path.Combine(outputDirectory, fileName);
                File.WriteAllText(filePath, chapter.Content, Encoding.UTF8);
            }
        }

        /// <summary>
        /// 清理文件名中的非法字符
        /// </summary>
        private string CleanFileName(string fileName)
        {
            var invalidChars = Path.GetInvalidFileNameChars();
            foreach (var invalidChar in invalidChars)
            {
                fileName = fileName.Replace(invalidChar, '_');
            }

            // 限制文件名长度
            if (fileName.Length > 100)
            {
                fileName = fileName.Substring(0, 100);
            }

            return fileName;
        }
    }
}
