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

namespace LHDN.CRM.BC.Common
{
    public class LogWriter
    {
        private string m_exePath = string.Empty;
        public LogWriter(string logMessage)
        {
            LogWrite(logMessage);
        }
        public static void LogMessage(string strMessage)
        {
            
                Helper.LogMessage(strMessage);
                //string strLogFolder = @"C:\CRMLog";
                //bool folderExists = Directory.Exists(strLogFolder);

                //if (!folderExists)
                //    Directory.CreateDirectory(strLogFolder);

                //if (string.IsNullOrEmpty(strLogFolder))
                //{
                //    strLogFolder = Utilities.GetAppPath();
                //}

                //string strLogFile = "Logger_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                //bool fileExists = Directory.Exists(strLogFile);
                //if (!fileExists)
                //    Directory.CreateDirectory(strLogFolder + "//" + strLogFile);

                //if (string.IsNullOrEmpty(strLogFile))
                //{
                //    strLogFile = Utilities.GetAppPath();
                //}


                //bool blnLogEnabled = true;

                //if (blnLogEnabled)
                //{
                //    using (System.IO.StreamWriter sw = new StreamWriter(strLogFolder + "\\" + strLogFile, true))
                //    {
                //        sw.WriteLine(DateTime.Now + ": " + strMessage);
                //    }
                //}
        }

        public void LogWrite(string logMessage)
        {
            m_exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            try
            {
                using (StreamWriter w = File.AppendText(m_exePath + "\\" + "log.txt"))
                {
                    Log(logMessage, w);
                }
            }
            catch (Exception ex)
            {
            }
        }

        public void Log(string logMessage, TextWriter txtWriter)
        {
            try
            {
                txtWriter.Write("\r\nLog Entry : ");
                txtWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());
                txtWriter.WriteLine("  :");
                txtWriter.WriteLine("  :{0}", logMessage);
                txtWriter.WriteLine("-------------------------------");
            }
            catch (Exception ex)
            {
            }
        }
    }
}
