using System;

namespace ZionVault.Logger
{
    class LogFile : ILog
    {
        private static string DebugLogFile =
            String.Format(@"{0}\OnSet Tecnologia\Zion Desktop User Suite\Logs\application-debug.log", System.Environment.GetEnvironmentVariable("PROGRAMDATA"));
        private static string ErrorLogFile =
            String.Format(@"{0}\OnSet Tecnologia\Zion Desktop User Suite\Logs\application-error.log", System.Environment.GetEnvironmentVariable("PROGRAMDATA"));
        private static string InfoLogFile =
            String.Format(@"{0}\OnSet Tecnologia\Zion Desktop User Suite\Logs\application-info.log", System.Environment.GetEnvironmentVariable("PROGRAMDATA"));

        private static object CurrentLock = new Object();

        public enum LogType
        {
            DEBUG,
            ERROR,
            INFO
        }

        public void debug(string message)
        {
            lock (CurrentLock)
            {
                WriteLog(LogType.DEBUG, message, DebugLogFile);
            }
        }

        public void error(string message)
        {
            lock (CurrentLock)
            {
                WriteLog(LogType.ERROR, message, ErrorLogFile);
            }
        }

        public void info(string message)
        {
            lock (CurrentLock)
            {
                WriteLog(LogType.INFO, message, InfoLogFile);
            }
        }

        private void WriteLog(LogType Type, string Message, string LogFile)
        {
            lock (CurrentLock)
            {
                if (!System.IO.Directory.Exists(LogFile))
                    System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(LogFile));

                if (!System.IO.File.Exists(LogFile))
                    System.IO.File.Create(LogFile);

                using (System.IO.StreamWriter writer = System.IO.File.AppendText(LogFile))
                {
                    writer.WriteLine(String.Format("[{0}], {1} - {2}",
                        Type.ToString("g"),
                        DateTime.Now.ToString("hh:mm:ss dd/MM/yyyy"),
                        Message));
                }
            }
        }
    }
}
