using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CowainHmi
{
    public class PlcTagColumn : INotifyPropertyChanged
    {
        private string varValue = "";
        private string address = "DB1.0";

        public PlcTagColumn()
        {
        }

        public PlcTagColumn(string varName, string varDesc)
        {
            VarName = varName;
            VarDesc = varDesc;
        }
        public PlcTagColumn(string text)
        {
            string[] array = Regex.Split(text, "<,>", RegexOptions.IgnoreCase);
            if (array.Length == 8)
            {
                int val = 0;
                VarName = array[0];
                VarDesc = array[1];
                PLC = int.TryParse(array[2], out val) ? val : 0;
                Address = array[3];
                DatType = Enum.TryParse<字段.DatType>(array[4], out 字段.DatType datType) ? datType : 字段.DatType.INT16;
                DecimalPlaces = int.TryParse(array[5], out val) ? val : 0;
                StringLength = int.TryParse(array[6], out val) ? val : 0;
                IsReadOnly = bool.TryParse(array[7], out bool readOnly) ? readOnly : false;
            }
        }

        public string VarName { get; set; }
        public string VarDesc { get; set; }
        public int PLC { get; set; } = 0;
        public string Address
        {
            get => address;
            set
            {
                if (address != value)
                {
                    address = value;
                    OnPropertyChanged(nameof(Address));
                }
            }
        }
        public 字段.DatType DatType { get; set; } = 字段.DatType.INT16;
        public int DecimalPlaces { get; set; } = 0;
        public int StringLength { get; set; } = 10;
        public bool IsReadOnly { get; set; }
        public string VarValue
        {
            get => varValue;
            set
            {
                if (varValue != value)
                {
                    varValue = value;
                    OnPropertyChanged(nameof(VarValue));
                }
            }
        }
        public override string ToString()
        {
            return $"{VarName}<,>{VarDesc}<,>{PLC}<,>{Address}<,>{DatType.ToString()}<,>{DecimalPlaces}<,>{StringLength}<,>{IsReadOnly.ToString()}";
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
