using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIZ.Framework.Core;
using VIZ.Package.Domain;
using VIZ.Package.Service;
using VIZ.Package.Storage;

namespace VIZ.Package.Module
{
    /// <summary>
    /// Viz命令窗口模型
    /// </summary>
    public class VizCommandWindowModel : ViewModelBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(typeof(VizCommandWindowModel));

        /// <summary>
        /// 操作日志服务
        /// </summary>
        private RecordLogService recordLogService = new RecordLogService();

        /// <summary>
        /// 日志视图模型
        /// </summary>
        public VizCommandWindowModel()
        {
            // 初始化命令
            this.InitCommand();

            // 初始化消息
            this.InitMessage();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void InitCommand()
        {
            this.ClearCommand = new VCommand(this.Clear);
        }

        /// <summary>
        /// 初始化消息
        /// </summary>
        private void InitMessage()
        {
            ApplicationDomainEx.MessageManager.Register<VizCommandLogMessage>(this, this.OnVizCommandLogMessage);
        }

        // ==================================================================================
        // Command
        // ==================================================================================

        #region ClearCommand -- 清除消息

        /// <summary>
        /// 清除消息
        /// </summary>
        public VCommand ClearCommand { get; set; }

        /// <summary>
        /// 清除
        /// </summary>
        private void Clear()
        {
            VizCommandWindow view = this.GetView<VizCommandWindow>();
            if (view == null)
                return;

            view.tb.Clear();
        }

        #endregion

        // ==================================================================================
        // Message
        // ==================================================================================

        /// <summary>
        /// Viz命令日志
        /// </summary>
        /// <param name="msg">消息</param>
        private void OnVizCommandLogMessage(VizCommandLogMessage msg)
        {
            // 记录操作日志
            this.recordLogService.AppendLog(ApplicationConstants.APPLICATION_GROUP_NAME, RecordLogOperate.VizCommand, RecordLogTrigger.System, RecordLogConstants.SYSTEM_SEND_VIZ_COMMAND, msg.Log);

            // 更新界面
            WPFHelper.BeginInvoke(() =>
            {
                VizCommandWindow view = this.GetView<VizCommandWindow>();
                if (view == null)
                    return;

                string log = $"{DateTime.Now.ToString("HH:mm:ss")} {msg.Log}";

                view.tb.AppendText($"{log}\r\n");
                view.tb.ScrollToEnd();
            });
        }

        // ==================================================================================
        // Public Function
        // ==================================================================================

    }
}