using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class GageInfoServiceClient : FObjServiceClient, IGageInfoService
    {

        public GageInfoServiceClient(UInt32 serviceId) : base(serviceId) { }

        public GageInfoServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
        #region IGageInfoService 接口
        private bool dataok=false;
        /// <summary>
        /// 数据好了！！！！
        /// 当 flyad7 的poslen, posOfGrid 发生变化时，DataOK = false, 需要重新记录。
        /// </summary>
        public bool DataOK
        {
            get { return dataok; }
            set
            {
                if (dataok != value)
                {
                    dataok = value;
                    NotifyPropertyChanged("DataOK");
                }
            }
        }


        private int poslen=9000;
        /// <summary>
        /// 机架总长，脉冲
        /// </summary>
        public int PosLen
        {
            get { return poslen; }
            set
            {
                if (poslen != value)
                {
                    poslen = value;

                }
            }
        }
        private int posofgrid=10;
        /// <summary>
        /// 1个grid = N个pos
        /// </summary>
        public int PosOfGrid
        {
            get { return posofgrid; }
            set
            {
                if (posofgrid != value)
                {
                    posofgrid = value;
                    NotifyPropertyChanged("PosOfGrid");
                }
            }
        }

        private List<int> forwdata = new List<int>();
        public List<int> ForwData
        {
            get { return forwdata; }
        }

        private List<int> backwdata = new List<int>();
        public List<int> BackwData
        {
            get { return backwdata; }
        }

        protected int progress;
        /// <summary>
        /// 0~100
        /// </summary>
        public int Progress
        {
            get
            {
                return progress;
            }
            set
            {
                if (progress != value)
                {
                    progress = value;
                    NotifyPropertyChanged("Progress");
                }
            }
        }

        private bool isRunning = false;
        /// <summary>
        /// 正在机架修正中
        /// </summary>
        public bool IsRunning 
        {
            get
            {
                return isRunning;
            }
            set 
            {
                if (isRunning != value)
                {
                    isRunning = value;
                    NotifyPropertyChanged("IsRunning");
                }
            }
        }

        /// <summary>
        /// 数据录制，记下来回扫描的AD值数组
        /// </summary>
        public bool Start()
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                GAGEINFO_OBJ_INTERFACE.CALL_START,
                null);
            return true;
        }
        public void Stop()
        {
            CurrObjSys.CallFunctionEx(
                mConn, mServerID, ID,
                GAGEINFO_OBJ_INTERFACE.CALL_STOP,
                null);
        }
        #endregion


        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    GAGEINFO_OBJ_INTERFACE.GET_STATE);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    GAGEINFO_OBJ_INTERFACE.GET_DATA);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff, SENSE_CONFIG.ADD);

            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case GAGEINFO_OBJ_INTERFACE.GET_STATE:
                    {
                        GAGEINFO_OBJ_INTERFACE.Pack_State p = new GAGEINFO_OBJ_INTERFACE.Pack_State();

                        if (!p.TryParse(infodata))
                            return;

                        Progress = p.progress;
                        DataOK = p.dataOk;
                        IsRunning = p.ing;

                    } break;
                case GAGEINFO_OBJ_INTERFACE.GET_DATA:
                    {
                        GAGEINFO_OBJ_INTERFACE.Pack_Data p = new GAGEINFO_OBJ_INTERFACE.Pack_Data();

                        if (!p.TryParse(infodata))
                            return;
                        PosOfGrid = p.posOfGrid;
                        PosLen = p.posLen;
                        ForwData.Clear();
                        if(p.forwData!=null)
                            ForwData.AddRange(p.forwData);
                        BackwData.Clear();
                        if(p.backwData!=null)
                            BackwData.AddRange(p.backwData);
                        NotifyPropertyChanged("Data");
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }
}
