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

using System.ComponentModel;
using FlyADBase;
using FLY.Thick.Base.IService;

namespace FLY.Thick.Base.Server
{

    public class GM_GageInfo : GM_ScanMotion2, IGageInfoService, Misc.ISaveToXml
    {
        #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; }
            set
            {
                if (forwdata != value)
                {
                    forwdata = value;
                    NotifyPropertyChanged("ForwData");
                }
            }
        }

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

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

        
        /// <summary>
        /// 数据录制，记下来回扫描的AD值数组
        /// </summary>
        public override bool Start()
        {
            bool b = base.Start();
            if (!b)
                return false;

            Progress = 0;
            tempDataCnt = 0;
            ScanCnt = 1;
            DataOK = false;
            PosOfGrid = mFlyAD.PosOfGrid;
            PosLen = mFlyAD.PosLen;
            int grid_size = PosLen / PosOfGrid;
            ForwData.Clear();
            BackwData.Clear();
            for (int i = 0; i < grid_size; i++)
            {
                ForwData.Add(Misc.MyBase.NULL_VALUE);
                BackwData.Add(Misc.MyBase.NULL_VALUE);
            }
            NotifyPropertyChanged("Data");
            return base.Start2();
        }
        public override void Stop()
        {
            base.Stop();
            if (DataOK)
            {
                Progress = 100;
            }
            else 
            {
                PosOfGrid = mFlyAD.PosOfGrid;
                PosLen = mFlyAD.PosLen;
                int grid_size = PosLen / PosOfGrid;
                ForwData.Clear();
                BackwData.Clear();
                for (int i = 0; i < grid_size; i++)
                {
                    ForwData.Add(Misc.MyBase.NULL_VALUE);
                    BackwData.Add(Misc.MyBase.NULL_VALUE);
                }
                NotifyPropertyChanged("Data");
            }
            Save();

        }
        #endregion
        private int tempDataCnt = 0;
        private string param_path;
        public GM_GageInfo(FlyAD7 flyad):this(flyad, "gageinfo.xml")
        {

        }
        public GM_GageInfo(FlyAD7 flyad, string param_path)
            : base(flyad)
        {
            GMState = Common.CTRL_STATE.GAGEINFO;
            this.param_path = param_path;

            Load();
            if ((PosOfGrid != mFlyAD.PosOfGrid) || (PosLen != mFlyAD.PosLen))
                DataOK = false;
            else
                DataOK = true;
            flyad.PropertyChanged += new PropertyChangedEventHandler(flyad_PropertyChanged);

            DataPosEvent += new EndEventHandler(pollCorrect_DataPosEvent);
            EndEvent += new EndEventHandler(pollCorrect_EndEvent);
        }
        void flyad_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "PosOfGrid") ||
                (e.PropertyName == "PosLen"))
            {
                if ((PosOfGrid != mFlyAD.PosOfGrid) || (PosLen != mFlyAD.PosLen))
                    DataOK = false;
                else
                    DataOK = true;
            }
        }


        void pollCorrect_EndEvent(object obj, FlyADBase.MiniGridEventArgs e)
        {
            DataEnd(e.direction, e.posOfGrid, e.grid_start, e.buf);
        }
        void pollCorrect_DataPosEvent(object obj, FlyADBase.MiniGridEventArgs e)
        {
            int grid_len = PosLen / PosOfGrid;
            if (e.direction == Misc.DIRECTION.FORWARD)
            {
                int p = e.grid_start * 100 / grid_len;

                Progress = (100 * tempDataCnt + p) / (2);
            }
            else
            {
                int p = (grid_len - e.grid_start) * 100 / grid_len;

                Progress = (100 * tempDataCnt + p) / (2);
            }
        }
        void DataEnd(Misc.DIRECTION direction, int posOfGrid, int start_grid, int[] dat)
        {
            if (direction == Misc.DIRECTION.FORWARD)
            {
                for (int i = 0; i < dat.Length; i++)
                {
                    int grid = i + start_grid;
                    if (grid >= ForwData.Count())
                        break;
                    ForwData[grid] = dat[i];
                }
            }
            else 
            {
                for (int i = 0; i < dat.Length; i++)
                {
                    int grid = i + start_grid;
                    if (grid >= BackwData.Count())
                        break;
                    BackwData[grid] = dat[i];
                }
                DataOK = true;
            }
            NotifyPropertyChanged("Data");
            tempDataCnt++;
            Progress = 100 * tempDataCnt / (2);
        }

        void Save() 
        {
            Misc.SaveToXmlHepler.Save(param_path, this);
        }
        void Load() 
        {
            Misc.SaveToXmlHepler.Load(param_path, this);
        }


        public virtual string[] GetSavePropertyNames()
        {
            return new string[]{
                "PosLen","PosOfGrid","ForwData","BackwData"
            };
        }
    }
     
}
