using System;
using System.IO;
using System.Windows.Forms;

namespace AdvertsingProfitControl
{
    internal class GlobalClasses
    {
        public static string LogFilePath = Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\";
        public GlobalClasses()
        {
            //Check to make sure the directory exists and if it doesn't then create it.
            if (Directory.Exists(Logger.LogFolderPath)) return;
            try
            {
                Directory.CreateDirectory(Logger.LogFolderPath);
            }
            catch (IOException e)
            {
                MessageBox.Show(e.Message + Environment.NewLine + @"Advertising Profit Control will be forced to exit.", @"Failed to Create Log File Directory", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Logs any error related to uncaught thread exceptions.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void LogError(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            using (
                var file =
                    new StreamWriter(Logger.LogFolderPath + DateTime.Now.ToString("yy-MM-dd") + ".log", true))
            {
                file.WriteLine(DateTime.Now + ": Unhandled Thread Exception:\n" + e.Exception.Message + Environment.NewLine);
                MessageBox.Show(@"An unhandled thread exception has occurred. Advertising Profit Control will be forced to exit.",
                    @"Fatal Error");
                Application.Exit();
            }
        }

        /// <summary>
        /// Logs any unhandled exceptions that fires.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void LogError(object sender, UnhandledExceptionEventArgs e)
        {
            using (var file = new StreamWriter(Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\" + DateTime.Now.ToString("yy-MM-dd") + ".log", true))
            {
                file.WriteLine(DateTime.Now + ": An unhandled exception occurred. \nException Object: " + e.ExceptionObject + Environment.NewLine);
                MessageBox.Show(@"An unknown error has occurred. Advertising Profit Control will be forced to exit.",
    @"Fatal Error");
                Application.Exit();
            }
        }

        /// <summary>
        /// Used to log anything from information for the user to errors that don't cause a total application failure.
        /// </summary>
        /// <param name="message">The message to log for future use.</param>
        public static void WriteToLog(string message)
        {
            using (
            var file = new StreamWriter(Path.GetPathRoot(Environment.SystemDirectory) + @"Users\" + Environment.UserName + @"\AppData\Local\APC\Information.log", true))
            {
                file.WriteLine(message);
            }
        }
    }
}
