using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;

namespace MitriaGames_Minecraft_Launcher_MK2
{
    internal static class Log
    {
        //로그파일 이름 규칙 어셈블리명-숫자.log
        private static readonly string LogName = $"{Assembly.GetExecutingAssembly().GetName().Name}";

        static Log()
        {
            if (!System.IO.Directory.Exists("logs"))
            {
                System.IO.Directory.CreateDirectory("logs");
            }
            var logFileName = $"logs\\{LogName}-" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            Trace.Listeners.Add(
                new TextWriterTraceListener(new FileStream(logFileName, FileMode.Append, FileAccess.Write,
                    FileShare.Read)));
        }

        public static void Initialize()
        {
            Trace.Write("====================================================================================================// ");
            Trace.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Trace.WriteLine(" //====================================================================================================");
            Trace.Flush();
        }
        public static void WriteLog(LogLevel logLevel, string message)
        {
            // Call Location
            var stackTrace = new StackTrace();
            var methodBase = stackTrace.GetFrame(1).GetMethod();
            string location = methodBase.DeclaringType != null
                ? methodBase.DeclaringType.Name + "/"
                : location = "null/";
            location += methodBase.Name;
            // Call Location End

            // Log String 
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("[");
            stringBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            stringBuilder.Append("]");
            stringBuilder.Append("\t");
            stringBuilder.Append(logLevel);
            stringBuilder.Append("\t");
            stringBuilder.Append(location.Trim());
            stringBuilder.Append("\t");
            stringBuilder.Append(message.Trim());
            Trace.WriteLine(stringBuilder.ToString());
            Trace.Flush();
        }
    }


    public enum LogLevel
    {
        Fatal,
        Error,
        Warn,
        Info,
        Debug,
        Trace
    }
}