using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace Core.Mes.ServerManager
{
    /// <summary>
    /// FrmSeverMain 的摘要说明。
    /// </summary>
    public class FrmSeverMain : System.Windows.Forms.Form
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public FrmSeverMain()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (cMain != null)
                    {
                        cMain.Dispose();
                    }
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }


                System.GC.Collect();
                base.Dispose(disposing);
            }
            catch
            {

            }

        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(480, 112);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // listBox1
            // 
            this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(0, 112);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(480, 98);
            this.listBox1.TabIndex = 1;
            // 
            // FrmSeverMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(480, 215);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.pictureBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "FrmSeverMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "应用服务";
            this.Load += new System.EventHandler(this.FrmSeverMain_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.FrmSeverMain_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        //[STAThread]
        //static void Main()
        //{			
        //    Application.Run(new FrmSeverMain());
        //}
        public static Configuration config;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ListBox listBox1;
        private ClsServerMain cMain;

        private void FrmSeverMain_Load(object sender, System.EventArgs e)
        {
            //==>
            //if (System.DateTime.Today > new System.DateTime(2007,1,1))	return;

            ApplicationStart();
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            this.pictureBox1.Image = System.Drawing.Image.FromFile(@"Image\登录框.jpg");
            cMain = new ClsServerMain();

            cMain.ShowLogEvent += new Core.Mes.ServerManager.ClsServerMain.ShowLogDelegate(cMain_ShowLog);

            cMain.InitServerMain();
        }

        private void cMain_ShowLog(string log)
        {
            this.listBox1.Items.Add(log);
            this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
        }

        private void FrmSeverMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("是否确认退出服务端程序？", "提示", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                ApplicationExit();
                Application.Exit();
                System.Diagnostics.Process.GetCurrentProcess().Kill();

            }
        }

        private static void ApplicationExit()
        {

            string path = string.Format(@"{0}/log/Application/{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd"));

            using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
            {

                StringBuilder sbtxt = new StringBuilder();
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(string.Format("服务关闭时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm::ss")));
                sbtxt.AppendLine("==============================================");
                sw.WriteLine(sbtxt.ToString());

            }


        }
        private static void ApplicationStart()
        {
            string path = string.Format(@"{0}/log/Application/{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd"));

            using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
            {

                StringBuilder sbtxt = new StringBuilder();
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(string.Format("服务开启时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm::ss")));
                sbtxt.AppendLine("==============================================");
                sw.WriteLine(sbtxt.ToString());

            }
        }
    }
}
