using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.Dialog
{
    public partial class fSystem_MotParameter : Form
    {
        public class cmdevents : EventArgs
        {
            public string cmd { get; set; }
            public string data { get; set; }
            public cmdevents(string cmd,string data)
            {
                this.cmd = cmd;
                this.data = data;
            }
        }
        public EventHandler<cmdevents> Commands;
        public fSystem_MotParameter()
        {
            InitializeComponent();
            this.KeyDown += SystemParameter_KeyDown;
            this.dataSet1.MotionParam.Clear();
            this.dataSet1.MotionParam.Merge(PUB.system_mot.dt);
            this.dataSet1.MotionParam.AcceptChanges();
            this.dataSet1.MotionParam.TableNewRow += MotionParam_TableNewRow;
        }

        private void MotionParam_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["Enable"] = false;
            if (this.dataSet1.MotionParam.Rows.Count < 1) e.Row["idx"] = 0;
            else e.Row["idx"] = this.dataSet1.MotionParam.Rows.Count;
            e.Row["UseOrigin"] = true;
            e.Row["UseEStop"] = true;
            e.Row["HomeAcc"] = 100;
            e.Row["HomeDcc"] = 100;
            e.Row["HomeHigh"] = 50;
            e.Row["HomeLow"] = 30;
            e.Row["SWLimitP"] = 0;
			e.Row["InpositionAccr"] = 0.1f;
        }

        void SystemParameter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape) this.Close();
        }

        private void SystemParameter_Load(object sender, EventArgs e)
        {
            this.dv1.AutoResizeColumns();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bs.EndEdit();

            PUB.system_mot.Save(this.dataSet1.MotionParam);

            //소스파일을 변경 해준다.
            //var sourcePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Don't change it");
            //var di = new System.IO.DirectoryInfo(sourcePath);
            //if (di.Exists)
            //{
            //    var fiEnum = new System.IO.FileInfo(di.FullName + "\\Class\\EnumData.cs");
            //    if (fiEnum.Exists)
            //    {
            //        var buffer = System.IO.File.ReadAllText(fiEnum.FullName, System.Text.Encoding.UTF8);
            //        var axis_start_tag = "public enum eaxis";
            //        var axis_sta = buffer.ToLower().IndexOf(axis_start_tag);
            //        var axis_end = buffer.IndexOf("}", axis_sta);

            //        var buffer_axis = buffer.Substring(axis_sta, axis_end - axis_sta);

            //        //값을 생성해준다.
            //        var sb_axis = new System.Text.StringBuilder();
            //        sb_axis.AppendLine("public enum eAxis : byte {");
            //        foreach (DSSetup.MotionParamRow dr in this.dataSet1.MotionParam)
            //        {
            //            if (dr.Enable == false) continue;
            //            if (string.IsNullOrEmpty(dr.Title)) continue;
            //            sb_axis.AppendLine($"\t\t{dr.Title} = {dr.idx},");
            //        }
            //        sb_axis.AppendLine("\t}");
            //        var buffer_head = buffer.Substring(0, axis_sta);
            //        var buffer_tail = buffer.Substring(axis_end + 1);
            //        var newbuffer = buffer_head + sb_axis.ToString() + buffer_tail;
            //        System.IO.File.WriteAllText(fiEnum.FullName, newbuffer, System.Text.Encoding.UTF8);
            //    }
            //}

            DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void motionParamDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var idx = this.dv1.Rows[e.RowIndex].Cells["dvc_idx"].Value;
            if (idx == null) return;

			if (e.ColumnIndex != 13) return;

            var val = short.Parse(idx.ToString());

			PUB.mot.ShowParameter(val);
		}

        private void motionParamDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }
    }
}
