using System.Collections.Generic;
using System.Windows.Forms;
using log4net.Appender;
using log4net.Core;
using System.Threading;
using ChuPiao.Common.Utils;
using System;
using ChuPiao.Common.Enums;
using System.Configuration;
using System.Diagnostics;

namespace ChuPiao.Common.Core.Utils
{
    /// <summary>
    /// 自定义的log4net Appender，将日志呈现到界面上
    /// </summary>
    public class LogUIAppender : AppenderSkeleton
    {
        delegate void ShowLogInvoker(RichTextBox richTextBox, string log);
        public static event EventHandler ShowAlertLogEvent;

        private static Dictionary<string, RichTextBox> _richTextBoxLogs;
        private static RichTextBox _richTextBoxAlertLog;
        private BeepUtil _beepUtil = BeepUtil.Instance();
        /// <summary>
        /// 日志呈现控件
        /// </summary>
        public static Dictionary<string, RichTextBox> RichTextBoxLogs
        {
            get { return _richTextBoxLogs; }
            set { _richTextBoxLogs = value; }
        }
        /// <summary>
        /// 警告日志呈现控件
        /// </summary>
        public static RichTextBox RichTextBoxAlertLog
        {
            get { return _richTextBoxAlertLog; }
            set { _richTextBoxAlertLog = value; }
        }

        protected override void Append(LoggingEvent loggingEvent)
        {
            if (loggingEvent.Level != Level.Debug)
            {
                string logmsg = this.RenderLoggingEvent(loggingEvent);

                if (RichTextBoxLogs != null && RichTextBoxLogs.ContainsKey(loggingEvent.LoggerName))
                {
                    RichTextBox richTextBox = RichTextBoxLogs[loggingEvent.LoggerName];
                    //创建了相关句柄
                    //if (richTextBox.IsHandleCreated)
                    //{
                    //    richTextBox.BeginInvoke(new ShowLogInvoker(ShowLog), richTextBox, logmsg);
                    //} 
                    richTextBox.BeginInvoke(new ShowLogInvoker(ShowLog), richTextBox, logmsg);
                }

                if (loggingEvent.Level >= Level.Warn && RichTextBoxAlertLog != null)
                {
                    BeepUtil.Instance().StartCurrent();
                    RichTextBoxAlertLog.BeginInvoke(new ShowLogInvoker(ShowLog), RichTextBoxAlertLog, logmsg);
                    if (null != ShowAlertLogEvent)
                    {
                        ShowAlertLogEvent(this, new EventArgs());
                    }
                }
            }
        }
        private static void ShowLog(RichTextBox richTextBox, string logmsg)
        {
            if (richTextBox.Lines.Length > 500)
            {
                richTextBox.Clear();
            }
            richTextBox.AppendText(logmsg);
            richTextBox.ScrollToCaret();
            Application.DoEvents();
        }

        /// <summary>
        /// 停止在界面上显示日志
        /// </summary>
        public static void StopShowLog()
        {
            if (_richTextBoxLogs != null)
                _richTextBoxLogs.Clear();
            RichTextBoxAlertLog = null;
        }
    }
}
