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

namespace Project
{

    public class ModelInfoM
    {
        //스피드 값과, 위치값을 저장하면 된다.
        public int idx { get; set; }
        public string Title { get; set; }

        public Dictionary<int, PositionData[]> Position { get; set; }
   

        public ModelInfoM()
        {
            idx = -1;
            Title = string.Empty;
            ClearDataPosition();
        }
        void ClearDataPosition()
        {
            Position = new Dictionary<int, PositionData[]>();
            var motCount = Enum.GetNames(typeof(eAxis)).Length;
            for (int i = 0; i < motCount; i++)
            {
                var datas = new PositionData[64];
                for (int j = 0; j < datas.Length; j++)
                    datas[j] = new PositionData(-1, string.Empty, 0.0);

                Position.Add(i, datas);
            }
        }
     
        public Boolean isSet
        {
            get
            {
                if (idx == -1) return false;
                else return !Title.isEmpty();
            }
        }

        public void ReadValue(DataSet1.MCModelRow dr)
        {
            idx = dr.idx;
            Title = dr.Title;
            ReadValuePosition(idx);
        }

        public void ReadValue(int index)
        {
            var dr = PUB.mdm.dataSet.MCModel.Where(t => t.idx == index).FirstOrDefault();
            if (dr == null)
            {
                idx = -1;
                Title = string.Empty;
                ClearDataPosition();
            }
            else
            {
                idx = dr.idx;
                Title = dr.Title;
                ReadValuePosition(idx);
            }
        }
        public Boolean ReadValue(string title)
        {
            return ReadValue(title, PUB.mdm.dataSet.MCModel);
        }
        public Boolean ReadValue(string title, DataSet1.MCModelDataTable dt)
        {
            var dr = dt.Where(t => t.Title == title).FirstOrDefault();
            if (dr == null)
            {
                idx = -1;
                this.Title= string.Empty;
                ClearDataPosition();
                return false;
            }
            else
            {
                idx = dr.idx;
                this.Title = dr.Title;
                ReadValuePosition(idx);
                return true;
            }
        }
        public void ReadValuePosition(int modelidx)
        {
            ReadValuePosition(modelidx, PUB.mdm.dataSet.MCModel);
        }
        public void ReadValuePosition(int modelidx, DataSet1.MCModelDataTable dt)
        {
            ClearDataPosition();

            //각 모터별로 데이터를 가져온다
            int cnt = 0;
            foreach (var data in Position)
            {
                //해당 모터의 속도값을 가져온다
                var drows = dt.Where(t => t.pidx == modelidx && t.PosIndex >= 0 && t.MotIndex == data.Key);
                foreach (DataSet1.MCModelRow dr in drows)
                {
                    var item = data.Value[dr.PosIndex];
                    item.index = dr.PosIndex;
                    item.title = dr.PosTitle;
                    item.value = dr.Position;
                    item.speed = dr.Speed;
                    item.acc = dr.SpeedAcc;
                    item.dcc = dr.SpeedDcc;
                    data.Value[dr.PosIndex] = item;
                    cnt += 1;
                }
            }

            PUB.log.Add(String.Format("Motion({0}) Data Load : {1}", modelidx, cnt));
        }
        
    }


   

}
