using CowainHmi.Common;
using CowainHmi.Helper;
using CowainHmi.UIEditor;
using PCHMI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace CowainHmi
{
    public partial class 变量表格监控 : UserControl
    {
        private string varText = "";
        [Browsable(false)]
        public string TagText
        {
            get
            {
                return varText;
            }
            set
            {
                varText = value;
            }
        }

        [Browsable(true)]
        [Category("PCHMI")]
        [Description("设置变量表内容")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(UIPlcDataEdit), typeof(UITypeEditor))]
        public string VarText
        {
            get => varText;
            set => varText = value;
        }

        [Browsable(true)]
        [Category("PCHMI")]
        [Description("DB块号，只有西门子PLC支持设置")]
        public int DB
        {
            get => dB;
            set
            {
                dB = value;
                SetDB();
            }
        }
        private BindingList<PlcTagColumn> tagList = new BindingList<PlcTagColumn>();
        private int dB;

        public 变量表格监控()
        {
            InitializeComponent();
            this.dgvData.AutoGenerateColumns = false;
            this.dgvData.DataSource = tagList;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.DesignMode)
            {
                return;
            }
            if (!PClass.SystemRun)
            {
                return;
            }
            foreach (var item in tagList)
            {
                item.VarValue = PlcValueHelper.GetValueString(item);
            }
            //this.dgvData.Refresh();
        }
        private void SetDB()
        {
            foreach (var item in tagList)
            {
                if (DB > 0)
                {
                    string address = PlcValueHelper.GetAddr(item.PLC, item.Address);
                    string[] array = address.Split('.');
                    if (address.StartsWith("DB") && array.Length > 1)
                    {
                        array[0] = $"DB{DB}";
                        item.Address = string.Join(".", array);
                    }
                }
            }
        }
        private void ucPlcDataTable_Load(object sender, EventArgs e)
        {
            if (this.DesignMode)
            {
                return;
            }
            if (!PClass.SystemRun)
            {
                return;
            }
            var list = FrmPlcDataTable.Str2TagList(varText);
            foreach (var item in list)
            {
                tagList.Add(item);
            }
            SetDB();
            this.timer1.Enabled = true;

        }

        private void dgvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            new DataGridViewStyle().DgvRowPostPaint(this.dgvData, e);
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == this.dgvData.Columns["VarValue"].Index && e.RowIndex >= 0)
            {
                if (tagList[e.RowIndex].IsReadOnly)
                {
                    return;
                }
                if (tagList[e.RowIndex].DatType != 字段.DatType.BIT)
                {
                    DlgEditPlcValue editPlcValue = new DlgEditPlcValue(tagList[e.RowIndex]);
                    editPlcValue.ShowDialog();
                }
                else
                {
                    //是位变量，按下切换
                    var result = MessageBox.Show("确定要切换变量吗？", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result != DialogResult.Yes)
                    {
                        return;
                    }
                    if (tagList[e.RowIndex].VarValue == "1")
                    {
                        tagList[e.RowIndex].VarValue = "0";
                    }
                    else
                    {
                        tagList[e.RowIndex].VarValue = "1";
                    }
                    PlcValueHelper.SetValue(tagList[e.RowIndex]);
                }
            }
        }
    }
}
