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


namespace Project
{
    public class System_MotParameter
    {

        string filename;
        public DSSetup.MotionParamDataTable dt;
        public int MotaxisCount
        {
            get
            {
                if (this.dt.Rows.Count < 1) return 0;
                var maxid = this.dt.Max(t => t.idx);
                return maxid + 1;
            }
        }


        double[] _maxspeed;
        public double MaxSpeed(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 0;
            else return dr.MaxSpeed;
        }
        public double[] GetMaxSpeed
        {
            get
            {
                if (MotaxisCount < 1) return null;

                if (_maxspeed == null || _maxspeed.Length != MotaxisCount)
                {
                    _maxspeed = new double[MotaxisCount];
                    for (int i = 0; i < MotaxisCount; i++)
                    {
                        _maxspeed[i] = MaxSpeed(i);
                    }
                }

                return _maxspeed;
            }
        }

        public void ClearCache()
        {
            _maxspeed = null;
            _maxacc = null;
        }

        double[] _maxacc;

        public double MaxAcc(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 0;
            else return dr.MaxAcc;
        }

        public double[] GetMaxAcc
        {
            get
            {
                if (MotaxisCount < 1) return null;

                if (_maxacc == null || _maxacc.Length != MotaxisCount)
                {
                    _maxacc = new double[MotaxisCount];
                    for (int i = 0; i < MotaxisCount; i++)
                    {
                        _maxacc[i] = MaxAcc(i);
                    }
                }
                return _maxacc;
            }
        }

        public double HomeSpeedHigh(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 30;
            else return dr.HomeHigh;
        }
        public double[] GetHomeSpeedHigh
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new double[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = HomeSpeedHigh(i);
                }
                return retval;
            }
        }
        public double HomeSpeedLow(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 10;
            else return dr.HomeLow;
        }
        public double[] GetHomeSpeedLow
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new double[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = HomeSpeedLow(i);
                }
                return retval;
            }
        }
        public double HomeSpeedAcc(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 50;
            else return dr.HomeAcc;
        }
        public double[] GetHomeSpeedAcc
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new double[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = HomeSpeedAcc(i);
                }
                return retval;
            }
        }
        public Boolean UseEStopSignal(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return false;
            else return dr.UseEStop;
        }

        public Boolean UseOriginSignal(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return false;
            else return dr.UseOrigin;
        }

        public Boolean UseAxis(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return false;
            else return dr.Enable;
        }
        public double SWLimit(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 0;
            else return dr.SWLimitP;
        }

        public double HomeSpeedDcc(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 50;
            else return dr.HomeDcc;
        }


        public float INPAccurary(int idx)
        {
            var dr = dt.Where(t => t.idx == idx).FirstOrDefault();
            if (dr == null) return 0.1f;
            else return dr.InpositionAccr;
        }

        public double[] GetHomeSpeedDcc
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new double[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = HomeSpeedDcc(i);
                }
                return retval;
            }
        }
        public Boolean[] GetUseAxis
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new bool[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = UseAxis(i);
                }
                return retval;
            }
        }
        public Boolean[] GetUseOrigin
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new bool[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = UseOriginSignal(i);
                }
                return retval;
            }
        }

        public Boolean[] GetUseEStop
        {
            get
            {
                if (MotaxisCount < 1) return null;
                var retval = new bool[MotaxisCount];
                for (int i = 0; i < MotaxisCount; i++)
                {
                    retval[i] = UseEStopSignal(i);
                }
                return retval;
            }
        }
        public System_MotParameter(string filename)
        {
            this.filename = filename;
            //if (string.IsNullOrEmpty(filename)
            //this.filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "system_mot.xml");
            dt = new DSSetup.MotionParamDataTable();
        }
        public void Save(DSSetup.MotionParamDataTable dt)
        {
            this.dt.Clear();
            this.dt.Merge(dt);
            this.dt.AcceptChanges();
            if (string.IsNullOrEmpty(this.filename) == false)
                this.dt.WriteXml(this.filename);
        }
        public void Load()
        {
            if (string.IsNullOrEmpty(this.filename) == false)
            {
                dt.Clear();
                if (System.IO.File.Exists(this.filename))
                    dt.ReadXml(this.filename);
                dt.AcceptChanges();
            }
        }
    }
}
