using Philisense.Congress.CollectionUI.View;
using Philisense.Congress.CollectionUI.View.UCControl.Login;
using Philisense.Congress.CollectionUI.ViewModel.UCControl.Login;
using Philisense.Congress.Common.View;
using Philisense.Congress.Control.NetProtocol;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace Philisense.Congress
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private string userName;

        public string UserName
        {
            get { return userName; }
            set
            {
                userName = value;
            }
        }
        private string userPwd;

        public string UserPwd
        {
            get { return userPwd; }
            set
            {
                userPwd = value;
            }
        }

        private string isRemind;

        public string IsRemind
        {
            get { return isRemind; }
            set { isRemind = value; }
        }

        private string _LastLoginTime;
        /// <summary>
        /// 最后登录时间
        /// </summary>
        public string LastLoginTime
        {
            get { return _LastLoginTime; }
            set { _LastLoginTime = value; }
        }
        public Philisense.Congress.EFDatabase.T_UserInfo UserInfo { get; set; }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        protected override void OnStartup(StartupEventArgs e)
        {
            //string ss = Guid.NewGuid().ToString();            

            #region 判断当前软件是否已注册

            //Philisense.Congress.Common.Reg.CheckReg checkReg = new Philisense.Congress.Common.Reg.CheckReg();
            //if (!checkReg.GetIsReg())
            //{
            //    CMessageBox.Show("会议软件尚未在此电脑注册");
            //    return;
            //}
            #endregion

            #region 判断当前实例是否已运行
            if (e.Args.Length > 0)
            {

            }
            else
            {
                Process Current = Process.GetCurrentProcess();
                Process[] processesByName = Process.GetProcessesByName(Current.ProcessName);
                if (processesByName.Length > 1)
                {
                    //for (int i = 0; i < processesByName.Length; i++)
                    //{
                    //    processesByName[i].Kill();
                    //}
                    //如果程序已经运行，则给出提示。并退出本进程。
                    CMessageBox.Show("会议系统已经在运行！", "系统提示");
                    //可能你不需要弹出窗口，在这里可以屏蔽掉
                    Process.GetCurrentProcess().Close();
                    return; //Exit;

                }
            }

            #endregion

            #region 注册Aspose
            FileControl.ModifyInMemory.ActivateMemoryPatching();
            #endregion


            base.OnStartup(e);
            //LoginViewModel loginviewmodel = new LoginViewModel();
            //LoginWindow login = new LoginWindow(loginviewmodel);
            //loginviewmodel.OnShowMain += login_OnShowMain;
            //login.Show();
            LoadNode();
            if (IsRemind == "True" && !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(UserPwd))
            {
                //LoginIn();
                Common.View.LoadClass lc = new LoadClass("Loading...");
                Common.View.LoadClass.DelegateNonParmAndHasReturn my = new
                 LoadClass.DelegateNonParmAndHasReturn(LoginIn);
                object obj = lc.NonParmAndHasReturnMethod(my);
            }
            else
            {
                LoginCheckViewModel logincheckModel = new LoginCheckViewModel();
                logincheckModel.OnShowMain += login_OnShowMain;
                LoginCheckView loginCheck = new LoginCheckView(logincheckModel);
                loginCheck.Show();
            }

        }

        private object LoginIn()
        {
            string OutMsg = "";
            Philisense.Congress.Collection.BLL.Sys.UserInfoManager userManager = new Philisense.Congress.Collection.BLL.Sys.UserInfoManager();
            UserInfo = userManager.GetUserInfoByName_Pwd(UserName, Common.ToolsHelper.MD5Encrypt(UserPwd), out OutMsg);
            if (UserInfo != null)
            {
                //if (DateTime.Now < UserInfo.UI_LastLoginDate || Convert.ToUInt64(DateTime.Now.ToString("yyyyMMdd")) >= 20230820)
                //{
                //    CMessageBox.Show("会议系统已到期！", "系统提示");
                //    return null;
                //}
                UserInfo.UI_LastLoginDate = DateTime.Now;
                UserInfo.UI_LastLoginIP = Common.ToolsHelper.GetLocalIP();
                userManager.Update(UserInfo);
                System.Windows.Application.Current.Dispatcher.Invoke(() =>
                {
                    Prism.Bootstrapper bootstrapper = new Prism.Bootstrapper();
                    bootstrapper.Run();
                });
            }
            return null;
        }

        object login_OnShowMain()
        {
            Prism.Bootstrapper bootstrapper = new Prism.Bootstrapper();
            bootstrapper.Run();
            return null;
        }

        public void LoadNode()
        {
            if (File.Exists("Xmls\\LoginConfig.xml"))
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load("Xmls\\LoginConfig.xml");
                XmlNodeList nodeList = xmlDocument.SelectSingleNode("data").ChildNodes;//获取Employees节点的所有子节点

                foreach (XmlNode xn in nodeList)//遍历
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("Name") == "登录名")
                    {
                        UserName = xe.InnerText;
                    }
                    if (xe.GetAttribute("Name") == "登录密码")
                    {
                        UserPwd = xe.InnerText;
                    }
                    if (xe.GetAttribute("Name") == "记住密码")
                    {
                        IsRemind = xe.InnerText;
                    }
                    if (xe.GetAttribute("Name") == "登录时间")
                    {
                        LastLoginTime = xe.InnerText;
                    }
                }
            }
        }

    }
}
