using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace lol_coder.Log
{
    public class LogWriter
    {
        public enum LogType { Error, Display, Normal }

        #region Variables
        public string LogFileName { get; set; }
        public bool isShowLogNormal { get; set; } = true;
        public bool isShowLogDisplay { get; set; } = false;
        public bool isShowLogError { get; set; } = true;

        public Color colorLogNormal { get; set; } = Color.FromArgb(0, 0, 0);
        public Color colorLogDisplay { get; set; } = Color.FromArgb(160, 50, 50);
        public Color colorLogError { get; set; } = Color.FromArgb(230, 0, 0);

        public bool isSaveLogNormal { get; set; } = true;
        public bool isSaveLogDisplay { get; set; } = false;
        public bool isSaveLogError { get; set; } = true;

        #endregion

        #region Singleton 

        private static LogWriter uniqueInstance = null;
        private static readonly Object mInstanceLocker = new Object();

        public static LogWriter getInstance()
        {
            lock (mInstanceLocker)
            {
                if (uniqueInstance == null)
                {
                    uniqueInstance = new LogWriter();
                }
            }
            return uniqueInstance;
        }

        #endregion

        #region Method 
        public void LogWrite(string logMessage, LogType logType)
        {
            string m_exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string folderPath = m_exePath + @"\Logs";
            DirectoryInfo di = new DirectoryInfo(folderPath);
            if (!di.Exists) di.Create();

            string suffix = "";
            switch (logType)
            {
                case LogType.Normal: suffix = " normal.txt"; break;
                case LogType.Display: suffix = " display.txt"; break;
                case LogType.Error: suffix = " error.txt"; break;
            }



            try
            {
                using (StreamWriter w = File.AppendText(folderPath + @"\" + LogFileName + suffix))
                {
                    w.WriteLine(DateTime.Now.ToString("yyMMdd HH:mm:ss.fff : ") + logMessage);
                }
            }
            catch (Exception ex) { }
        }

        #endregion
    }
}
