using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIProofread.Model
{
    /// <summary>
    /// 文档列表
    /// </summary>
    public class DocumentList
    {
        public List<DocumentInfo> documentList = new List<DocumentInfo>();
        public DocumentInfo ActiveDocument { get; set; }
        public int Count => documentList.Count;

        public void Add(DocumentInfo documentInfo)
        {
            documentList.Add(documentInfo);
        }

        public void Clear()
        {
            documentList.ForEach(doc =>{ doc.Dispose(); });

            documentList.Clear();
        }

        public DocumentInfo Get(int index)
        {
            return documentList[index];
        }
        public DocumentInfo GetById(int id)
        {
            return Count > 0 ? documentList.FirstOrDefault(x => x.Id == id) : null;
        }
        public DocumentInfo Get(string uniqueId)
        {
            return Count > 0 ? documentList.FirstOrDefault(x => x.UniqueId == uniqueId) : null;
        }
        public DocumentInfo Get(Document doc)
        {
            return Count > 0 && doc != null ? documentList.FirstOrDefault(x => x.CurrentDocument == doc) : null;
        }

        public DocumentInfo GetActive()
        {
            // documentList.FirstOrDefault(x => x.IsActive)
            return Count > 0 ? ActiveDocument : null;
        }

        public bool Contains(DocumentInfo documentInfo)
        {
            return Count > 0 && documentList.Contains(documentInfo);
        }

        public bool Contains(string uniqueId)
        {
            return Count > 0 && documentList.Any(x => x.UniqueId == uniqueId);
        }

        // 通过文档判断是否包含
        public bool Contains(Document originDocument)
        {
            return Count > 0 && originDocument != null && documentList.Any(x => x.CurrentDocument == originDocument);
        }

        /// <summary>
        /// 移除文档
        /// </summary>
        /// <param name="documentInfo"></param>
        /// <returns></returns>
        public bool Remove(DocumentInfo documentInfo)
        {
            return documentList.Remove(documentInfo);
        }
        public bool Remove(Document originDocument)
        {
            if (Count > 0 && originDocument != null)
            {
                documentList.RemoveAll(x =>
                {
                    if (x.CurrentDocument == originDocument)
                    {
                        x.Close();
                        return true;
                    }
                    return false;
                });
            }
            return true;
        }

        /// <summary>
        /// 获取文档的索引
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(DocumentInfo item)
        {
            return documentList.IndexOf(item);
        }

        /// <summary>
        /// 设置当前激活的文档
        /// </summary>
        /// <param name="originDocument"></param>
        public DocumentInfo SetActiveDocument(Document originDocument)
        {
            if (originDocument == null) return null;
            var document = Get(originDocument);

                // 如果不存在，则添加
            if (document == null)
            {
                document = new DocumentInfo(originDocument);
                Add(document);
            }
            if (ActiveDocument == document) return document;
            else if (Globals.ThisAddIn.IsWPS)
            {
                // WPS 只有一个窗口 所以需要先关闭之前文档的面板
                ActiveDocument?.Deactive();
            }
            // 如果存在，则设置激活
            ActiveDocument = document;
            //document.IsActive = true;
            document.Active();
            return document;
        }

        internal void HideAllPane()
        {
            documentList.ForEach(d => d.HidePane());
        }
    }
}
