using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

using FObjBase;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.Common;
using FLY.Thick.BulkDataModule;
using FLY.Thick.BlowingScan.Client;
using FLY.Thick.Blowing.Client;

namespace FLY.Thick.BlowingScan.UI.Client
{
    public class TDGage : ThickTcpUiInWindow.TDGage
    {
        #region 参数--------------------------------------------------------------------

        #region 本地数据----------------------------------------------------------------
        /// <summary>
        /// 系统参数
        /// </summary>
        //public SysParam mSysParam;
        public LCUS1 mLCUS;
        #endregion
        #region 服务器数据--------------------------------------------------------------


        /// <summary>
        /// 动态数据
        /// </summary>
        public DynArea mDynArea;


        public PasswordServiceClient mPasswordService;

        public BlowingScanServiceClient mRenZiJiaService;
        public BlowingDetectServiceClient mBDetect;
        public BlowingProfileServiceClient mProfile;
        public FLY.OBJComponents.Client.WarningServiceClient mWarningService;

        public FLY.OBJComponents.Client.BufferWindow<OBJComponents.Common.FlyData_WarningHistory> mWarningReasonWindow;

        public InitParamServiceClient mInitParamService;

        public FlyADServiceClient mFlyAdServiceClient;

        public GetSampleServiceClient mGetSampleServiceClient;
        #endregion

        #endregion

        public TDGage()
        {
            Init1();
        }

        #region Init登陆服务器初始化----------------------------------------------------
        /// <summary>
        /// 第1步, 加载本地数据
        /// </summary>
         void Init1()
        {

            #region 提取保存的数据
            mSysParam = new SysParam();//系统参数
            #endregion

            mLCUS = new LCUS1();
            Misc.BindingOperations.SetBinding(mSysParam, "LCUS1_PortName", mLCUS, "PortName");


            #region 与服务器同步的数据
            mBulkDataClient = new BulkDataServiceClientAdv(new BulkDataServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.BULK_ID));

            mDynAreaService = new DynAreaServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.DYNAREA_ID);
            mDynArea = mDynAreaService.mDynArea;

            mWarningService = new OBJComponents.Client.WarningServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.WARNING_OBJ_ID);
            //创建窗口观察 报警原因列表
            mWarningReasonWindow = new OBJComponents.Client.BufferWindow<OBJComponents.Common.FlyData_WarningHistory>(mWarningService.ReasonList, 100);


            mTDGageService = new TDGageServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.TDGAGE_ID);

            mInitParamService = new InitParamServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.INITPARAM_ID);

            mFixService = new FixServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.FIX_ID);

            mProfile = new BlowingProfileServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.PROFILE_ID);

            mPasswordService = new PasswordServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.PASSWORD_ID);

            mRenZiJiaService = new BlowingScanServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.RENZIJIA_ID);
            mBDetect = new BlowingDetectServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.BDETECT_ID);
            mFlyAdServiceClient = new FlyADServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.FLYAD_ID);
            mGetSampleServiceClient = new GetSampleServiceClient(OBJ_INTERFACE.OBJ_INTERFACE_ID.GET_SAMPLE_ID);
            #endregion
        }

         TCPCConn conn = null;
        /// <summary>
        /// 第2步, 连接到服务器,初始化参数 。由界面的 【登陆】 按钮触发
        /// </summary>
        public void Init2(IPEndPoint serveripep)
        {

            if(!mSysParam.ServerIPEP.Equals(serveripep))
            {
                mSysParam.ServerIPEP = serveripep;
                mSysParam.Save();
            }
            #region 网络初始化
            List<UInt32> objids = new List<UInt32>(new UInt32[]{
                mDynAreaService.ID,
                ((BulkDataServiceClient)(mBulkDataClient.bulkdata)).ID,
                mTDGageService.ID,
                mFixService.ID,
                mPasswordService.ID,
                mRenZiJiaService.ID,
                mInitParamService.ID,
                mProfile.ID,
                mBDetect.ID,
                mFlyAdServiceClient.ID,
                mGetSampleServiceClient.ID
            });
            objids.AddRange(mWarningService.GetIDs());

            if (conn != null)
            {
                if (!conn.RemoteEP.Equals(mSysParam.ServerIPEP))
                {
                    //断开之前的连接
                    FObjSys.Current.Disconnect_to_Another_OBJSys(conn.RemoteEP, objids.ToArray());
                }
            }

            conn = FObjSys.Current.Connect_to_Another_OBJSys(mSysParam.ServerIPEP, objids.ToArray());

            #endregion
        }

        public void Init2() 
        {
            Init2(mSysParam.ServerIPEP);
        }
        
        #endregion

    }
}