using System;
using System.IO;
using System.Reflection;

namespace LJLib.Tools.File
{
    internal class Logger : ILogger
    {
        private string _path;

        private string _filename = "log.txt";

        private object _syncRoot = new object();

        public Logger()
        {
            _path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
        }

        public Logger(string path)
        {
            _path = path;
        }

        public Logger(string path, string filename)
        {
            _path = path;
            _filename = filename;
        }

        public virtual string Path
        {
            get { return _path; }
        }

        public virtual string FileName
        {
            get { return _filename; }
            set { _filename = value; }
        }

        public virtual string FullName
        {
            get { return _path + "\\" + _filename; }
        }

        public virtual void WriteLog(string msg)
        {
            try
            {
                FileInfo file = new FileInfo(FullName);
                if (file.Exists && file.Length > 10240000)
                {
                    string tmpfile = FullName + ".tmp";
                    using (FileStream fs = file.Open(FileMode.Open, FileAccess.Read))
                    using (StreamReader reader = new StreamReader(fs))
                    {
                        fs.Seek(-1024000, SeekOrigin.End);
                        reader.ReadLine();
                        using (StreamWriter sw = System.IO.File.AppendText(tmpfile))
                        {
                            while (!reader.EndOfStream)
                            {
                                string strline = reader.ReadLine();
                                sw.WriteLine(strline);
                            }
                            sw.Flush();
                            sw.Close();
                        }
                    }
                    file.Delete();
                    System.IO.File.Move(tmpfile, FullName);
                }

                using (StreamWriter sw = System.IO.File.AppendText(FullName))
                {
                    string rslt = "//====================================================================\r\n";
                    rslt += "// 时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
                    rslt += "//--------------------------------------------------------------------\r\n";
                    rslt += "// " + msg.Replace("\r\n", "\r\n// ") + "\r\n";
                    rslt += "//====================================================================\r\n\r\n";
                    sw.Write(rslt);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        public virtual void InsertLog(string msg)
        {
            try
            {
                using (FileStream fs = System.IO.File.Open(FullName, FileMode.OpenOrCreate))
                {
                    StreamReader reader = new StreamReader(fs);
                    string filestr = reader.ReadToEnd();
                    filestr = DateTime.Now.ToString("yyyy-MM-dd HH:mm") + " " + msg + "\r\n" + filestr;
                    fs.Seek(0, SeekOrigin.Begin);
                    StreamWriter writer = new StreamWriter(fs);
                    writer.Write(filestr);
                    writer.Flush();
                }
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        public virtual bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        public virtual object SyncRoot
        {
            get
            {
                return _syncRoot;
            }
        }

        public static Logger Synchronized(Logger logger)
        {
            return new SyncLogger(logger);
        }
    }
}
