using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;

namespace parkMonitor.LOG
{
    class LogManager
    {
        private string logFileName = string.Empty;
        private string logPath = "Log";
        private string logFileExtName = "log";
        private bool writeLogTime = true;
        private bool logFileNameEndWithDate = true;
        private Encoding logFileEncoding = Encoding.UTF8;
        private object obj = new object();

        public LogManager()
        {
            this.LogPath = "Log";
            this.LogFileExtName = "log";
            this.WriteLogTime = true;
            this.logFileNameEndWithDate = true;
            this.LogFileEncoding = Encoding.UTF8;
        }
        public LogManager(string logPath,string logFileExtName,bool writeLogTime)
        {
            this.LogPath =logPath;
            this.LogFileExtName = logFileExtName;
            this.WriteLogTime = writeLogTime;
            this.LogFileNameEndWithDate = true;
        }
        
        //Log文件路径
        private string LogPath
        {
            get
            {
                if (this.logPath == null || this.logPath == string.Empty)
                {
                    this.logPath = System.IO.Path.Combine(ConfigurationManager.AppSettings["LogPath"], DateTime.Now.ToString("yyyy-MM-dd"));
                }
                return this.logPath;
            }
            set
            {
                this.logPath = value;
                if (this.logPath == null || this.logPath == string.Empty)
                {
                    this.logPath = System.IO.Path.Combine(ConfigurationManager.AppSettings["LogPath"], DateTime.Now.ToString("yyyy-MM-dd"));
                }
                else
                {
                    try
                    {
                        //是否绝对路径
                        if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
                        {
                            //绝对路径
                        }
                        else
                        {
                            //相对路径
                            this.logPath = System.IO.Path.Combine(ConfigurationManager.AppSettings["LogPath"], DateTime.Now.ToString("yyyy-MM-dd"));
                        }
                        if (!Directory.Exists(this.logPath))
                        {
                            Directory.CreateDirectory(this.logPath);
                        }
                    }
                    catch
                    {
                        this.logPath = System.IO.Path.Combine(ConfigurationManager.AppSettings["LogPath"], DateTime.Now.ToString("yyyy-MM-dd"));
                        //this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
                    }
                    if (!this.logPath.EndsWith(@"\"))
                    {
                        this.logPath += @"\";
                    }
                }
            }
        } 
        //Log文件扩展名
        private string LogFileExtName
        {
            get { return this.logFileExtName; }
            set { this.logFileExtName = value; }
        }
        //日志文件名是否带日期
        private bool LogFileNameEndWithDate
        {
            get { return this.logFileNameEndWithDate; }
            set { this.logFileNameEndWithDate = value; }
        }
        //是否在每个Log行前面添加时间
        private bool WriteLogTime
        {
            get { return this.writeLogTime; }
            set { this.writeLogTime = value; }
        }
        //日志文件的字符编码
        private Encoding LogFileEncoding
        {
            get { return this.logFileEncoding; }
            set { this.logFileEncoding = value; }
        }

        public void WriteLog(string logFile, string msg)
        {
            lock (obj)
            {
                try
                {
                    string dateString = string.Empty;
                    if (this.LogFileNameEndWithDate || logFile.Length == 0)
                    {
                        dateString = DateTime.Now.ToString("yyyy-MM-dd");
                    }
                    logFileName = string.Format("{0}{1}.{2}", this.LogPath,dateString, this.logFileExtName);
                    using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
                    {
                        if (writeLogTime)
                        {
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:") +logFile+":"+ msg);
                        }
                        else
                        {
                            sw.WriteLine(msg);
                        }
                    }
                }
                catch
                {

                }
            }
        }
        //
        public void WriteLog(LogFile logFile, string msg)
        {
            this.WriteLog(logFile.ToString(), msg);
        }
        public void WriteLog(string msg)
        {
            this.WriteLog(string.Empty, msg);
        }
    }

    public enum LogFile
    {
        LOG,
        RESET,
        ERROR,
        WARNING,
        INFO,
        ERROR_NUMBERPLATE
    }
}
