using HighWayIot.Log4net;
using HighWayIot.Winform.MainForm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using HighWayIot.Plc;

namespace HighWayIot.Winform
{
    internal static class Program
    {
        private static LogHelper logger = LogHelper.Instance;

        [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool AllocConsole();

        [System.Runtime.InteropServices.DllImport("Kernel32")]
        public static extern void FreeConsole();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            //Console.OutputEncoding = System.Text.Encoding.UTF8;
            //Console.InputEncoding = System.Text.Encoding.UTF8;

            try
            {
                AllocConsole();//调用系统API，调用控制台窗口
                               // 设置控制台编码为系统默认编码（解决乱码问题）
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //创建窗体
                LoginForm loginform = new LoginForm();
                logger.Info("程序初始化成功");
                //验证成功显示主窗体
                if (loginform.ShowDialog() == DialogResult.OK)
                {
                    logger.Info("登陆成功");
                    Application.Run(new BaseForm());
                }
                FreeConsole();//释放控制台
            }
            catch(Exception ex)
            {
                logger.Error("程序初始化异常", ex);
            }            
        }
    }
}
