using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using UtilLib;

namespace AIProofread.Model
{
    public class ExportDataItem
    {
        public int PageNumber { get; set; }
        public int LineNumber { get; set; }
        public string OriginSentence { get; set; }
        public CorrectItem Item { get; set; }

        private static readonly int MAX_WORD_LENGTH = 128;

        public ExportDataItem(CorrectItem item, int pageNumber, int lineNumber,string originSentence = "")
        {
            Item = item;
            PageNumber = pageNumber;
            LineNumber = lineNumber;
            OriginSentence = originSentence;
        }

        public static string GetSentence(ProofreadItem item)
        {
            var sentence = item.OriginSentence;
            var isInsert = item.content.Tag == "i";
            if (sentence.Length <= MAX_WORD_LENGTH && !isInsert)
            {
                return sentence;
            }
            // 截取中间位置
            try
            {
                var middlePosition = MAX_WORD_LENGTH / 2;
                var cutStart = item.content.Start - middlePosition;
                // 越界了
                if (cutStart < 0)
                {
                    cutStart = 0;
                }
                var originText = sentence.Substring(cutStart, Math.Min(sentence.Length, MAX_WORD_LENGTH));
                if (!isInsert)
                {
                    item.content.Start = item.content.Start - cutStart;
                    item.content.End = item.content.End - cutStart;
                    return originText;
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
            var range = item.mark.Range;

            // 获取range所在句子
            //var first = range.Sentences.First.Start;
            //var last = range.Sentences.Last.End;
            var sentences = range.Sentences;
            var sb = new StringBuilder();
            foreach (Microsoft.Office.Interop.Word.Range s in sentences)
            {
                var str = s.Text;
                if (isInsert && range.Start >= s.Start && range.End <= s.End)
                {
                    sb.Append(str.Replace("　", "˽")); 
                }
                else
                {
                    sb.Append(str);
                }
            }
            var first = sentences.First;
            // 内容位置：用于后续标红
            item.content.Start = range.Start - first.Start;
            item.content.End = range.End - first.Start;
            return sb.ToString().Trim();
        }

        public static List<ExportDataItem> GetExportData(Dictionary<int, ProofreadItem> marks)
        {
            List<ExportDataItem> list = new List<ExportDataItem>();

            foreach (var item in marks)
            {
                // 没有mark
                if (item.Value.mark == null) continue;
                var it = item.Value.content;
                // 单纯删除空格
                if (it.Tag == "d" && it.Origin.Trim().Length == 0) continue;

                var range = item.Value.mark.Range.Duplicate;
                range.End = item.Value.mark.Start;

                // 获取书签在文档的页码数
                var pageNumber = range.get_Information(WdInformation.wdActiveEndPageNumber); // wdActiveEndPageNumber
                // 获取书签在当前页面的行数
                var lineNumber = range.get_Information(WdInformation.wdFirstCharacterLineNumber);
                list.Add(new ExportDataItem(it, pageNumber, lineNumber,GetSentence(item.Value)));
            }
            // 根据页码和行数排序
            list.Sort((x, y) =>
            {
                if (x.PageNumber == y.PageNumber)
                {
                    if(x.LineNumber == y.LineNumber)
                    {
                        // 如果行数相同,则根据起始位置排序
                        return x.Item.Id.CompareTo(y.Item.Id);
                    }
                    // 如果页码相同,则根据行数排序
                    return x.LineNumber.CompareTo(y.LineNumber);
                }
                return x.PageNumber.CompareTo(y.PageNumber);
            });
            return list;
        }
    }
}
