using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace s3sr
{
    /// <summary>
    /// Class that handles Error Reports
    /// </summary>
    class ErrorReport
    {
        #region Functions
        /// <summary>
        /// Attempt to create an Error Log on the application directory
        /// </summary>
        /// <param name="ex">Exception</param>
        /// <param name="exTitle">Main Information of the Exception</param>
        public static void CreateExceptionReport(Exception ex, string exTitle)
        {
            try
            {
                string LogPath = string.Format(@"{0} \s3sr_Error_{1}_{2}.log",
                    new string[] {
                    new FileInfo(Application.ExecutablePath).Directory.ToString(),
                    DateTime.Now.ToUniversalTime().ToString().Replace("/", "").Replace(":", "").Replace(" ", "_"),
                    (new Random().Next(100)).ToString()
                });

                if (!File.Exists(LogPath))
                    (File.Create(LogPath)).Close();

                List<string> Report = new List<string>(new string[] { exTitle, Environment.NewLine });
                Report.AddRange(ex.ToString().Split('\n'));
                File.WriteAllText(LogPath, string.Join(Environment.NewLine, Report.ToArray()), Encoding.Unicode);
            }
            catch (Exception exError)
            {
                MessageBox.Show(exError.ToString(), "Exception while Creating Error Log", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ErrorReport.CreateExceptionReport(exError, "Exception @ \"RXJyb3JSZXBvcnQuY3M= 22\";");
                return;
            }
        }
        #endregion
    }
}