using AIProofread.Controls;
using log4net.Config;
using log4net;
using System;
using System.IO;
using log4net.Appender;
using log4net.Layout;
using log4net.Repository.Hierarchy;

namespace AIProofread
{
    public class LogHelper
    {
        public static FormLogger LoggerForm;

        //private static readonly string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        public static ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static LogHelper()
        {
            //log4net.GlobalContext.Properties["APP_DATA"] = Config.APP_LOG_PATH;
            //XmlConfigurator.Configure();
            //Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
            PatternLayout layout = new PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            layout.ActivateOptions();
            //string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            //string logFilePath = Path.Combine(appDataPath, "YourAppName", "logs");

            RollingFileAppender roller = new RollingFileAppender();
            roller.File = Config.APP_LOG_PATH;
            roller.AppendToFile = true;
            roller.RollingStyle = RollingFileAppender.RollingMode.Date;
            roller.DatePattern = "yyyy-MM-dd'.txt'";
            roller.StaticLogFileName = false;
            roller.Layout = layout;
            roller.ActivateOptions();
            hierarchy.Root.AddAppender(roller);
            hierarchy.Root.Level = log4net.Core.Level.Debug;
            hierarchy.Configured = true;
        }

        /// <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);
            }
            if (!Directory.Exists(Config.APP_LOG_PATH))
            {
                Directory.CreateDirectory(Config.APP_LOG_PATH);
            }
            Logger.Debug($"[{tag}] {message}");
            //try
            //{
            //    string path = Config.APP_LOG_PATH + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            //    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)
        {
            Logger.Error(Config.IS_WPS ? "WPS" : "WORD" + e.ToString(), e);
            //Log(e.Message + "\n" + e.StackTrace);
        }
        public static void Log(string tag, Exception e)
        {
            Logger.Error(tag, e);
        }

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

        internal static ILog GetLogger(Type type)
        {
            return LogManager.GetLogger(type);
        }
    }
}
