using AIProofread.Controls;
using AIProofread.Model;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace AIProofread.core
{
    /// <summary>
    /// 常识性检测数据
    /// </summary>
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class CommonSenseDetection
    {
        /// <summary>
        /// 检测历史
        /// </summary>
        //public static readonly Dictionary<int, List<CommonsenseDetectionItem>> histories = new Dictionary<int, List<CommonsenseDetectionItem>>();
        // 暂时改成全局
        private List<CommonsenseDetectionItem> history = new List<CommonsenseDetectionItem>();

        // 生成单例
        public static readonly CommonSenseDetection instance = new CommonSenseDetection();
        private FormCommonsenseDetection formCommonsenseDetection;
        private CommonSenseDetection() { }

        public static CommonSenseDetection Instance(FormCommonsenseDetection handler)
        {
            instance.formCommonsenseDetection = handler;
            return instance;
        }

        public void WebInitializationCompleted()
        {
            if (formCommonsenseDetection != null && !formCommonsenseDetection.IsDisposed)
            {
                formCommonsenseDetection.InitializationCompleted();
            }
        }


        // 检测状态
        public bool isChecking = false;
        public string checkingSummary;
        public string checkingLocation;
        public string checkingKey;

        public string GetCheckStatus()
        {
            return JSONObject.Create()
                .Put("isChecking", isChecking)
                .Put("checkingKey", checkingKey)
                .Put("checkingLocation", checkingLocation)
                .Put("checkingSummary", checkingSummary)
                .ToString();
        }

        public void SetCheckingData(bool isChecking, string checkingKey, string checkingSummary,string checkingLocation)
        {
            if (isChecking)
            {
                this.isChecking = isChecking;
                this.checkingKey = checkingKey;
                this.checkingSummary = checkingSummary;
                this.checkingLocation = checkingLocation;
                Globals.ThisAddIn.ribbon.SetDetectionBtnStatus(false,false);
            }
            else
            {
                this.isChecking = false;
                this.checkingKey = null;
                this.checkingSummary = null;
                this.checkingLocation = null;
                var enableAll = string.IsNullOrEmpty(checkingSummary);
                Globals.ThisAddIn.ribbon.SetDetectionBtnStatus(enableAll,!enableAll);
            }
        }

        public string GetHistory()
        {
            //int id = Globals.ThisAddIn.ActiveDocument.Id;
            return JsonConvert.SerializeObject(history);// histories.ContainsKey(id) ? JsonConvert.SerializeObject(histories[id]) : "[]";
        }
        public void SetHistory(string json)
        {
            var item = JsonConvert.DeserializeObject<CommonsenseDetectionItem>(json);
            history.Add(item);
            //int id = Globals.ThisAddIn.ActiveDocument.Id;
            //if (histories.ContainsKey(id))
            //{
            //    histories.Add(id, new List<CommonsenseDetectionItem>() { item });
            //}
            //else
            //{
            //    histories[id].Add(item);
            //}
        }

        public void Close()
        {
            if (isChecking)
            {
                Globals.ThisAddIn.formCommonsenseDetection.Hide();
                return;
            }
            Globals.ThisAddIn.HideDetection();
        }
    }
}
