using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
using System.Diagnostics;

namespace Ooganizer.Platform.Utilities
{
    public class DebugLogger
    {
        private static string s_strFileNPath = "";
        private static FileM s_fileM = null;
        private static LogInstantiator s_LogI = null;
        private static int s_LoadTime = 0;
        private static bool s_firstTime = true;

        /// <summary>
        /// Use the Log(string line) method to quickly write to a temp log file in the current assembly directory
        /// </summary>
        static DebugLogger()
        {
            if (s_LogI == null)
            {
                int timebefore = System.Environment.TickCount;
                s_LogI = new LogInstantiator();
                s_LoadTime = System.Environment.TickCount - timebefore;
            }

            if (s_fileM == null)                
                s_fileM = new FileM(s_LogI.strFileName, "log", s_LogI.strDirectoryPath, true);

            if(s_strFileNPath == "")
                s_strFileNPath = s_fileM.PathNFile;
        }

        public static void Log(string line)
        {
            if (s_firstTime)
            {
                //s_fileM.WriteLineUTF8("EasyLogger's Logger.cs was loaded in " + s_LoadTime.ToString() + " miliseconds");
                s_firstTime = false;
            }
            s_fileM.WriteLineUTF8(line);            
        }        
                
        /// <summary>
        /// Small Helper class in order to get the location of the currently running assembly
        /// </summary>
        private class LogInstantiator
        {
            public string strFileName;
            public string strDirectoryPath;
            private static RegistryKey s_OogyRootKey;

            public LogInstantiator()
            {
                // For Random File names ~shouldn't be used anymore
                //string strRandomFileName = Path.GetRandomFileName();
                //strRandomFileName = strRandomFileName.Substring(0, (strRandomFileName.Length - 4)); // stripe extension (.xyz)                
                //strFileName = strRandomFileName;                    

                // Find the Calling Assembly that is NOT this one
                StackTrace stackTrace = new StackTrace();
                StackFrame stackFrame;
                MethodBase stackFrameMethod;
                string typeName;

                int framecount = 3;                
                stackFrame = stackTrace.GetFrame(framecount);
                stackFrameMethod = stackFrame.GetMethod();
                typeName = stackFrameMethod.ReflectedType.FullName;                

                // *IMP* Use the calling Assembly Type Name as the file name
                strFileName = typeName;
                
                // *Work-Around*, we can't have a circular reference with InstallationSpec, so we just
                // have to Query for the LogPath Directly instead of using Platform.InstallationSpec
                s_OogyRootKey = Registry.LocalMachine.OpenSubKey("Software\\Ooganizer",false);

                if (s_OogyRootKey != null)
                {
                    object keyvalue = s_OogyRootKey.GetValue("LogPath");
                    if ((keyvalue != null) && (keyvalue.ToString() != ""))
                        strDirectoryPath = keyvalue.ToString();
                }
            }
        }
    }
}
