using FLY.OBJComponents.Server.OBJProxy;
using FLY.Weight.Server.OBJProxy;
using FObjBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace FLY.Weight.Server
{
    public class TDGage : Misc.ISaveToXml
    {
        #region 初始化------------------------------------------------------------------
        public string mParamDirectory;//参数目录 m_path;//数据文件的 根目录默认是 Gage1
        public string mName;//名字 如:gage0
        #endregion

        #region 服务器数据--------------------------------------------------------------
        public int OBJ_Port//OBJ服务端口
        {
            get;
            set;
        }

        /// <summary>
        /// 吹膜测厚仪 ep
        /// </summary>
        public IPEndPoint BlowingEP { get; set; }

        public WeightSystem mData;
        WeightSystem_OBJProxy mServerOBJProxy;
        WarningSystem_OBJProxy mWarnSystemOBJProxy;

        /// <summary>
        /// 吹膜测厚仪, 旋转架模型
        /// </summary>
        FLY.Thick.Blowing.Client.BlowingDetectServiceClient mBDetect;
        #endregion

        public TDGage(string nam)
        {
            mName = nam;
            mParamDirectory = mName;// @"D:\" + m_name;

            if (!System.IO.Directory.Exists(mParamDirectory))
            {
                System.IO.Directory.CreateDirectory(mParamDirectory);
            }
            System.Environment.CurrentDirectory = mParamDirectory;

            Init1();

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

            #region 数据备份。。。。
            Misc.BackupFile.Add("WS.xml", Misc.BackupFile.xml_isFileOK);
            Misc.BackupFile.Add("system.xml", Misc.BackupFile.xml_isFileOK);

            Misc.BackupFile.CheckResult cr = Misc.BackupFile.Check();
            #endregion

            OBJ_Port = 20005;
            BlowingEP = Misc.StringConverter.ToIPEndPoint("127.0.0.1:20006");
            Load();

            #region 与服务器同步的数据

            mBDetect = new Thick.Blowing.Client.BlowingDetectServiceClient(FLY.Thick.Blowing.OBJ_INTERFACE.OBJ_INTERFACE_ID.BDETECT_ID);
         
            #endregion

            #region 提取保存的数据

            mData = new WeightSystem();
            mServerOBJProxy = new WeightSystem_OBJProxy(0,mData);

            mWarnSystemOBJProxy = new WarningSystem_OBJProxy(0, OBJ_INTERFACE.OBJ_INTERFACE.WARNING_OBJ_ID, mData.mWarning);
            #endregion

            Init2();

            mBDetect.PropertyChanged += MBDetect_PropertyChanged;
        }

        private void MBDetect_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FilmVelocity")
            {
                mData.Accessory.CurrentVelocitySet = (((int)(mBDetect.FilmVelocity * 10)) / 10.0f);
            }
        }

        /// <summary>
        /// 第2步, 连接到服务器,初始化参数 。由界面的 【登陆】 按钮触发
        /// </summary>
        private void Init2()
        {
            #region 网络初始化
            FObjSys.Current.Start_Conn_Server(new IPEndPoint(IPAddress.Any, OBJ_Port), mServerOBJProxy.ID);

            FObjSys.Current.Connect_to_Another_OBJSys(
                BlowingEP,
                mBDetect.ID
                );

            #endregion
        }

        #endregion

        public void Save()
        {
            Misc.SaveToXmlHepler.Save("system.xml", this);
        }
        public void Load()
        {
            Misc.SaveToXmlHepler.Load("system.xml", this);
        }
        #region ISaveToXml 成员

        public string[] GetSavePropertyNames()
        {
            return new string[]{
                "OBJ_Port",
                "BlowingEP"
            };
        }

        #endregion
    }
}