using AIProofread.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Interop;

namespace AIProofread
{
    public class Logger
    {
        public static FormLogger LoggerForm;
        //private static readonly string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void Log(string tag,string message)
        {
            string time = DateTime.Now.ToString("HH:mm:ss");

            // 如果日志窗口已经打开，则显示日志
            if (LoggerForm != null && !LoggerForm.IsDisposed && LoggerForm.Visible)
            {
                LoggerForm.Log(time, tag, message);
            }
            string path = Config.APP_LOG_PATH + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            if (!Directory.Exists(Config.APP_LOG_PATH))
            {
                Directory.CreateDirectory(Config.APP_LOG_PATH);
            }
            try
            {
                StreamWriter streamWriter = File.AppendText(path);
                streamWriter.WriteLine("***************************[" + tag + "]***************************");
                streamWriter.WriteLine("消息：" + message);
                streamWriter.WriteLine("时间：" + time);
                streamWriter.WriteLine();
                streamWriter.Flush();
                streamWriter.Close();
                streamWriter.Dispose();
            }
            catch (Exception) { }
        }
        public static void Log(string msg)
        {
            Log((Config.IS_WPS ? "WPS" : "WORD"), msg);
        }
        public static void Log(Exception e)
        {
            Log(e.Message + "\n" + e.StackTrace);
        }
        public static void Log(string tag, Exception e)
        {
            Log(tag,e.Message + "\n" + e.StackTrace);
        }

        public static void LogToWeb(string msg)
        {
            if (Config.RUN_IN_DEBUG)
            {
                Globals.ThisAddIn.SendMessageToWeb("DEBUG-LOG", msg);
            }
        }
    }
}
