using Permission;
using PlcCom;
using PlcUiControl;
using PlcUiForm;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using XmlToTreeView;
using YangjieTester.用户管理;
namespace YangjieTester
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            const string mutexName = "YangjieTester";

            using (var mutex = new System.Threading.Mutex(true, mutexName, out bool createdNew))
            {
                if (createdNew)
                {

                    //Application.Run(new PermissionConfigForm());
                    //return;

                    DatabaseHelper.InitializeDatabase();

                    if (BatchService.GetLatestBatchID(out string batchID, out bool finished, out string errorMessage) && !finished)
                    {
                        AppSession.CurrentBatch.BatchID = batchID;
                    }

                    var batch = BatchService.GetLatestBatch(out string err);
                    if (batch != null)
                    {
                        AppSession.CurrentBatch = batch;
                    }

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    //Application.Run(new BatchReportForm());
                    //return;

                    var netConfig = new DeltaAxPLCNetConfig();
                    DeltaAxCommProtocol deltaIACommProtocol = new DeltaAxCommProtocol(netConfig);
                    PlcBaseForm.SetDefaultProtocol(deltaIACommProtocol);
                    BackgroundService.Instance = new BackgroundService(deltaIACommProtocol);
                    
                    var con = deltaIACommProtocol.ConnectPLC();

                    if (con.rt != 0)
                    {
                        MessageBox.Show($"PLC连接失败:代码0X{con.rt:X}。请排查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    //初始化权限系统
                    PermissionManager.Initialize();

                    var signInForm = new SignInForm("developer", "developer123");
                    Application.Run(signInForm);

                    if (signInForm.DialogResult == DialogResult.OK)
                    {
                        BackgroundService.Instance.Start();
                        Application.AddMessageFilter(new InputMessageFilter());
                        Application.Run(new MainForm());
                        BackgroundService.Instance.Stop();
                    }

                    if (con.rt == 0)
                        deltaIACommProtocol.Close();
                }
                else
                {
                    Application.Exit();
                }
            }

        }
    }
}
