using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Equips;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Controls.Base;
using Mesnac.PlugIn.View;
using Mesnac.Gui.Workbench;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Mesnac.Action.ChemicalWeighing.Sys
{
    /// <summary>
    /// 系统初始化自动运行业务类
    /// </summary>
    public class SystemInitAutoAction : ChemicalWeighingAction, IAction
    {
        #region 外部引用

        [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]
        public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);
        [DllImport("user32.dll", EntryPoint = "SendMessageTimeout")]
        public static extern long SendMessageTimeout(int hWnd, int Msg, int wParam, int lParam, int fuFlags, int uTimeout, ref int lpdwResult);
        [DllImport("User32.dll", EntryPoint = "PostMessage")]
        public static extern int PostMessage(
        int hWnd, // handle to destination window
        int Msg, // message
        int wParam, // first message parameter
        int lParam // second message parameter
        );

        #endregion

        #region 定义变量

        private static bool _isFirstRun = true;
      
        public const int LOCALE_USER_DEFAULT = 0x0400;
        public const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        public const int LOCALE_SSHORTDATE = 0x1F;
        public const int LOCALE_STIMEFORMAT = 0x1003;
        public const int HWND_BROADCAST = 0xFFFF;
        public const int WM_SETTINGCHANGE = 0x001A;
        public const int SMTO_ABORTIFHUNG = 2;

        #endregion

        #region 业务入口

        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);           //必须要调用的

            if (_isFirstRun == true)
            {
                //首次运行订阅工作台关闭事件
                Mesnac.Gui.Workbench.WorkbenchSingleton.Workbench.ShutDown += new EventHandler(Workbench_ShutDown);

                //首次运行修改系统日期格式
                //SetDateTimeFormat();

                //首次运行启动更新计划状态和自动下传计划服务
               FinishBatch.FinishBatchService.Instance.Start();

                //首次运行时启动定时器运行服务,其中包含Socket通讯连接
                Sys.TimerRunService.Instance.Start();

                _isFirstRun = false;
            }
        }

        #endregion

        #region 系统关闭事件处理

        protected void Workbench_ShutDown(object sender, EventArgs e)
        {
          //  FinishBatch.FinishBatchService.Instance.Stop();   //停止生产记录存盘服务
            Sys.TimerRunService.Instance.Stop();
        }

        #endregion

        public static void SetDateTimeFormat()
        {
            int p = 0;
            //设置短日期格式
            SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, "yyyy-MM-dd");
            //设置时间格式,24小时制
            SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, "HH:mm:ss");
            //设置完成后必须调用,通知其他程序格式已经更改,否则即使是程序自身也不能使用新设置的格式
            SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 10, ref p);
        }
    }
}