using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace MyTimeClock
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Init.Run(args);
        }
        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                AssemblyName assemblyName = new AssemblyName(args.Name);
                if (System.IO.File.Exists(Application.StartupPath + @"\Bin\dlls\" + assemblyName.Name + ".dll"))
                {
                    return Assembly.LoadFrom(Application.StartupPath + @"\Bin\dlls\" + assemblyName.Name + ".dll");
                }
                else
                {
                    return args.RequestingAssembly;
                }
            }
            catch (Exception ex)
            {
                return args.RequestingAssembly;
            }
        }
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            if (!System.IO.Directory.Exists(Application.StartupPath + "\\logs"))
            {
                System.IO.Directory.CreateDirectory(Application.StartupPath + "\\logs");
            }
            WriteLogs("错误.log", "错误描述:" + ex.Message + "\r\n\r\n异常堆栈：" + ex.StackTrace + "\r\n\r\n异常方法：" + ex.TargetSite);
        }
        static void WriteLogs(string file_name, string text)
        {
            string log_text = text;
            System.IO.File.WriteAllText(Application.StartupPath + "\\logs\\" + file_name, log_text,System.Text.Encoding.UTF8);
            if (MessageBox.Show("捕获到无法处理的异常!\r\n\r\n已生成错误日志,是否要提交给我们，以帮助我们尽快解决这个BUG?" +
    "\r\n\r\n当前日志包含了当前电脑的系统版本、进程、运行库、错误描述等信息,以帮助我们分析解决问题。\r\n\r\n选“是”自动提交错误日志。\r\n选“否”取消。", "错误", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
            {
                MessageBox.Show("错误日志无法提交!!!建议您将软件目录下的logs文件夹打包后发给我们，以帮助我们尽快解决这个BUG" +
"\r\n\r\n当前日志包含了当前电脑的系统版本、进程、运行库、错误描述等信息,以帮助我们分析解决问题。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            //MessageBox.Show(string.Format("捕获到未处理异常：{0}\r\n异常信息：{1}\r\n异常堆栈：{2}", ex.GetType(), ex.Message, ex.StackTrace));
            if (!System.IO.Directory.Exists(Application.StartupPath + "\\logs"))
            {
                System.IO.Directory.CreateDirectory(Application.StartupPath + "\\logs");
            }
            WriteLogs("线程错误.log", "错误描述:" + ex.Message + "\r\n\r\n异常堆栈：" + ex.StackTrace + "\r\n\r\n异常方法：" + ex.TargetSite);
        }
    }
}
