using PCHMI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CowainHmi.Helper
{
    public class PlcValueHelper
    {
        public static string GetAddr(int plc, string addr)
        {
            return new VAR().GADDR(plc, addr);
        }
        public static string GetValueString(PlcTagColumn tag)
        {
            string address = GetAddr(tag.PLC, tag.Address);
            if (tag.DatType == 字段.DatType.String)
            {
                return PCHMI.VL.GET_STRING(tag.PLC, address, tag.StringLength);
            }
            else if (tag.DatType == 字段.DatType.SIEMENS_String)
            {
                return PCHMI.VL.GET_SIEMENS_STRING(tag.PLC, address, tag.StringLength);
            }
            else if (tag.DatType == 字段.DatType.DateTime)
            {
                return DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
            }
            else if (tag.DatType == 字段.DatType.BIT)
            {
                return PCHMI.VL.GET_BIT(tag.PLC, address).ToString();
            }
            else if (tag.DatType == 字段.DatType.INT32)
            {
                return new PClass().UuValue((ulong)PCHMI.VL.GET_INT32(tag.PLC, address), (uint)tag.DecimalPlaces, tag.DatType.ToString());
            }
            else if (tag.DatType == 字段.DatType.UINT32)
            {
                return new PClass().UuValue((ulong)PCHMI.VL.GET_UINT32(tag.PLC, address), (uint)tag.DecimalPlaces, tag.DatType.ToString());
            }
            else if (tag.DatType == 字段.DatType.INT16)
            {
                return new PClass().UuValue((ulong)PCHMI.VL.GET_INT16(tag.PLC, address), (uint)tag.DecimalPlaces, tag.DatType.ToString());
            }
            else if (tag.DatType == 字段.DatType.UINT16)
            {
                return new PClass().UuValue((ulong)PCHMI.VL.GET_UINT16(tag.PLC, address), (uint)tag.DecimalPlaces, tag.DatType.ToString());
            }
            else if (tag.DatType == 字段.DatType.F32)
            {
                return PCHMI.VL.GET_F32(tag.PLC, address).ToString();
            }
            else
            {
                return "Err Data Type";
            }
        }
        public static void UpdateTagValue(PlcTagColumn tag)
        {
            tag.VarValue = GetValueString(tag);
        }

        public static void SetValue(PlcTagColumn tag)
        {
            string address = GetAddr(tag.PLC, tag.Address);
            if (tag.DatType == 字段.DatType.String)
            {
                PCHMI.RT.SEND_STRING(tag.PLC, address, tag.VarValue);
            }
            else if (tag.DatType == 字段.DatType.SIEMENS_String)
            {
                PCHMI.RT.SEND_SIEMENS_STRING(tag.PLC, address, tag.VarValue);
            }
            else if (tag.DatType == 字段.DatType.BIT)
            {
                if (ushort.TryParse(tag.VarValue, out ushort val))
                {
                    PCHMI.RT.SEND_BIT(tag.PLC, address, val);
                }
            }
            else if (tag.DatType == 字段.DatType.INT32)
            {
                if (int.TryParse(tag.VarValue, out int val))
                {
                    PCHMI.RT.SEND_INT32(tag.PLC, address, val);
                }
            }
            else if (tag.DatType == 字段.DatType.UINT32)
            {
                if (uint.TryParse(tag.VarValue, out uint val))
                {
                    PCHMI.RT.SEND_UINT32(tag.PLC, address, val);
                }
            }
            else if (tag.DatType == 字段.DatType.INT16)
            {
                if (short.TryParse(tag.VarValue, out short val))
                {
                    PCHMI.RT.SEND_INT16(tag.PLC, address, val);
                }
            }
            else if (tag.DatType == 字段.DatType.UINT16)
            {
                if (ushort.TryParse(tag.VarValue, out ushort val))
                {
                    PCHMI.RT.SEND_UINT16(tag.PLC, address, val);
                }
            }
            else if (tag.DatType == 字段.DatType.F32)
            {
                if (float.TryParse(tag.VarValue, out float val))
                {
                    PCHMI.RT.SEND_F32(tag.PLC, address, val);
                }
            }
        }

    }
}
