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;
using AR;

namespace Project
{
    public partial class fSetting_IOMessage : Form
    {
        AR.CommonSetting dummySetting;   //설정을 임시로 저장하고 있다가 완료시에 덮어준다.

        public fSetting_IOMessage()
        {
            InitializeComponent();

            //setting
            dummySetting = new AR.CommonSetting();
            AR.SETTING.Data.CopyTo(dummySetting);
            //COMM.SETTING.Data.CopyTo(dummySetting);

            this.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == Keys.Escape)
                    this.Close();
                if (DateTime.Now > PUB.LastInputTime) PUB.LastInputTime = DateTime.Now;
            };
            this.MouseMove += (s1, e1) => { if (DateTime.Now > PUB.LastInputTime) PUB.LastInputTime = DateTime.Now; };
            this.FormClosing += FSetting_IOMessage_FormClosing;
            this.bsI.Sort = "idx";
            this.bsO.Sort = "idx";
        }

        private void FSetting_IOMessage_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.bsI.EndEdit();
            this.bsO.EndEdit();
            this.Validate();

            var chgs1 = this.dataSet1.InputDescription.GetChanges();
            var chgs2 = this.dataSet1.ErrorDescription.GetChanges();
            if ((chgs1 != null && chgs1.Rows.Count > 0) || chgs2 != null && chgs2.Rows.Count > 0)
            {
                if (UTIL.MsgQ("There are unsaved changes. If you close now, these changes will be lost. Do you want to close the window?") != DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }

        private void __Load(object sender, EventArgs e)
        {
            RefreshIn();
            RefreshOut();
        }

        void RefreshIn()
        {
            this.dataSet1.InputDescription.Clear();
            this.dataSet1.InputDescription.Merge(PUB.mdm.dataSet.InputDescription);
            this.dataSet1.InputDescription.AcceptChanges();

            //자료를 업데이트해준다.
            var ecodelist = Enum.GetValues(typeof(eDIPin));
            foreach (var item in ecodelist)
            {
                var pin = (eDIPin)item;
                var idx = (int)pin;
                var name = ((eDIName)idx).ToString();
                if (Enum.IsDefined(typeof(eDIName), (byte)idx) == false)
                    name = string.Empty;
                
                var title = $"[X{idx:X2}] {name}";
                var dr = this.dataSet1.InputDescription.Where(t => t.Idx == idx).FirstOrDefault();
                if (dr == null)
                {
                    var newdr = dataSet1.InputDescription.NewInputDescriptionRow();
                    newdr.Name = pin.ToString();
                    newdr.Idx = (short)idx;
                    newdr.Title = title;
                    newdr.EndEdit();
                    this.dataSet1.InputDescription.AddInputDescriptionRow(newdr);
                }
                else
                {
                    if (dr.Title.Equals(title) == false)
                    {
                        dr.Title = title;
                        dr.EndEdit();
                    }
                }
            }
            this.dataSet1.InputDescription.AcceptChanges();
        }

        void RefreshOut()
        {
            this.dataSet1.OutputDescription.Clear();
            this.dataSet1.OutputDescription.Merge(PUB.mdm.dataSet.OutputDescription);
            this.dataSet1.OutputDescription.AcceptChanges();

            //자료를 업데이트해준다.
            var ecodelist = Enum.GetValues(typeof(eDOPin));
            foreach (var item in ecodelist)
            {
                var pin = (eDOPin)item;
                var idx = (int)pin;
                var name = ((eDOName)idx).ToString();
                if (Enum.IsDefined(typeof(eDOName), (byte)idx) == false)
                    name = string.Empty;

                var title = $"[Y{idx:X2}] {name}";

                var dr = this.dataSet1.OutputDescription.Where(t => t.Idx == idx).FirstOrDefault();
                if (dr == null)
                {
                    var newdr = dataSet1.OutputDescription.NewOutputDescriptionRow();
                    newdr.Name = pin.ToString();
                    newdr.Idx = (short)idx; 
                    newdr.Title = title;
                    newdr.EndEdit();
                    this.dataSet1.OutputDescription.AddOutputDescriptionRow(newdr);
                }
                else
                {
                    if (dr.Title.Equals(title) == false)
                    {
                        dr.Title = title;
                        dr.EndEdit();
                    }
                }
            }
            this.dataSet1.OutputDescription.AcceptChanges();
        }

        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            this.bsI.EndEdit();
            this.bsO.EndEdit();
            this.Validate();

            this.dataSet1.AcceptChanges();

            PUB.mdm.dataSet.InputDescription.Clear();
            PUB.mdm.dataSet.Merge(this.dataSet1.InputDescription);
            PUB.mdm.dataSet.InputDescription.AcceptChanges();
            PUB.mdm.SaveModelI();

            PUB.mdm.dataSet.OutputDescription.Clear();
            PUB.mdm.dataSet.Merge(this.dataSet1.OutputDescription);
            PUB.mdm.dataSet.OutputDescription.AcceptChanges();
            PUB.mdm.SaveModelO();

            //핀설정적용 230831
            DIO.Pin.SetOutputData(PUB.mdm.dataSet.OutputDescription);
            DIO.Pin.SetInputData(PUB.mdm.dataSet.InputDescription);
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            RefreshIn();
            RefreshOut();
        }

        private void inputDescriptionDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex ==4 )
            {
                var dv = sender as DataGridView;
                var str = string.Empty;
                var cell = dv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell != null && cell.Value != null) str = cell.Value.ToString();
                var f = new Dialog.fMessageInput(str);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    cell.Value = f.value;
                }
            }
        }
    }
}

