using GuideScreen.Common.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using PVDEMCS.Services;

namespace PVDEMCS.Services.Impl
{
    /// <summary>
    /// 日志服务 实现
    /// </summary>
    internal class LogService : ILogService
    {

        private Logger logger;

        private LogService(Logger logger)
        {
            this.logger = logger;
        }

        public LogService(string name)
            : this(LogManager.GetLogger(name))
        {

        }

        public LogService()
        {
            logger = LogManager.GetCurrentClassLogger();
        }

        public string LogDirectory
        {
            get
            {
                return Path.Combine(AppContext.BaseDirectory, "Logs");
            }

        }

        public void Debug(string msg, params object[] args)
        {
            logger.Debug(msg, args);
        }

        public void Debug(string msg, Exception err)
        {
            logger.Debug(msg, err);
        }

        public void Info(string msg, params object[] args)
        {
            logger.Info(msg, args);
        }

        public void Info(string msg, Exception err)
        {
            logger.Info(msg, err);
        }

        public void Trace(string msg, params object[] args)
        {
            logger.Trace(msg, args);
        }

        public void Trace(string msg, Exception err)
        {
            logger.Trace(msg, err);
        }

        public void Error(string msg, params object[] args)
        {
            logger.Error(msg, args);
        }

        public void Error(string msg, Exception err)
        {
            logger.Error(msg, err);
        }

        public void Fatal(string msg, params object[] args)
        {
            logger.Fatal(msg, args);
        }

        public void Fatal(string msg, Exception err)
        {
            logger.Fatal(msg, err);
        }

        /// <summary>
        /// 清理多少天前日志
        /// </summary>
        /// <param name="day"></param>
        public void ClearAgoDays(int day)
        {
            if (!Directory.Exists(LogDirectory))
            {
                return;
            }
            var files = Directory.GetFiles(LogDirectory);
            if (files.Length == 0)
            {
                return;
            }
            var date = DateTime.Now.AddDays(-day);
            var clearFiles = files.Select(f => new FileInfo(f)).Where(f => f.CreationTime <= date).Select(f => f.FullName);
            foreach (var filePath in clearFiles)
            {
                try
                {
                    Info($"日志文件:{filePath} 已过期，将进行清理...");
                    File.Delete(filePath);
                    Info($"日志文件:{filePath} 清理完成");
                }
                catch (Exception ex)
                {
                    Info($"日志文件:{filePath} 清理失败！" + ex.StackTrace);
                }
            }
        }
    }
}
