using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using BankTV.Consts;
using BankTV.Helpers;

namespace BankTV.Logger
{
    public class MyLogger
    {

        private static void LogFileAppendMessage(string message)
        {
            try
            {
                string loggerFileName = Path.Combine(GlobalHelper.GetLogsFolder(), GlobalConsts.LOG_FILE_NAME);
                if (!File.Exists(loggerFileName))
                {
                    FileStream fileStream = File.Create(loggerFileName);
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }   //if not null
                }   //if file not exist
                StreamWriter streamWriter = File.AppendText(loggerFileName);
                try
                {
                    streamWriter.WriteLine("[" + DateTime.Now.ToString() + "] " + message);
                }   //try
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }   //if not null
                }   //finally
            }   //try
            catch
            {
                //do sth
            }   //catch
        }


        public static void Log(Exception ex)
        {
            try
            {
                LogFileAppendMessage("ERROR: " + ex.Message);
                LogFileAppendMessage(ex.StackTrace.ToString());
            }   //try
            catch (Exception exception)
            {
                LogFileAppendMessage(ex.Message);
                LogFileAppendMessage(exception.Message);
            }   //catch
        }

        public static void Log(string message, LogLevel level = LogLevel.Info)
        {
            try
            {
                switch (level)
                {
                    case LogLevel.Error:
                        LogFileAppendMessage("ERROR: " + message);
                        break;
                    case LogLevel.Info:
                        LogFileAppendMessage("INFO: " + message);
                        break;
                    default:
                        LogFileAppendMessage(message);
                        break;
                }   //switch
            }   //try
            catch (Exception ex)
            {
                Log(ex);
            }

        }

        public enum LogLevel
        {
            Info,
            Error
        };

    }
}
