using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eDosStation
{
    public class SqlLog: IDisposable
    {
        private string sqlLogFolder;
        private string sqlLogFile;
        private bool disposed = false;
        System.IO.StreamWriter wr;
        System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-US");

        public SqlLog()
        {
            wr = null;
            try
            {
                sqlLogFolder = ConfigurationManager.AppSettings["sqlLogFolder"];
                if (System.IO.Directory.Exists(sqlLogFolder))
                {
                    sqlLogFile = System.IO.Path.Combine(sqlLogFolder, "sql.txt");
                    wr = new System.IO.StreamWriter(sqlLogFile, true);
                }
            } catch(System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (wr?.BaseStream != null) wr.Close();
                    wr?.Dispose();
                }
                disposed = true;
            }
        }
        ~SqlLog()
        {
            Dispose(false);
        }
        
        public void LogCommand(string ermsg)
        {
            if (wr !=null && wr.BaseStream.CanWrite)
            {
                wr.WriteLine($"-- {System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")} --- err {ermsg} ---");
                wr.Flush();
            }
        }

        public void LogCommand(System.Data.SqlClient.SqlCommand co )
        {
            if (wr != null)
            {
                wr.WriteLine($"-- {System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")} ---");
                if (co.CommandType == System.Data.CommandType.StoredProcedure) wr.Write("exec ");
                wr.Write(co.CommandText);
                int paidx = 0;
                foreach (System.Data.SqlClient.SqlParameter pa in co.Parameters)
                {
                    if (paidx++ > 0) wr.Write(",");
                    wr.Write($" @{pa.ParameterName}=");
                    if (pa.Value == DBNull.Value) wr.Write("null");
                    else if (pa.DbType == System.Data.DbType.String) wr.Write($"'{pa.Value}'");
                    else if (pa.DbType == System.Data.DbType.DateTime) wr.Write($"'{pa.Value:yyyyMMdd HH:mm}'");
                    else if (pa.DbType == System.Data.DbType.Double)
                    {
                        wr.Write(((double)pa.Value).ToString("0.0", ci));
                    }
                    else wr.Write(pa.Value);
                }
                wr.WriteLine();
                wr.WriteLine("----");
                wr.Flush();
            }
        }
    }
}
