using HTCoreServiceApp.DataHandle;
using HTCoreServiceApp.Models;
//using Opc.Ua;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace HTCoreServiceApp.Common
{
    public class AddressArg
    {
        private int start;
        private int length;
        private int addressOffset;

        public int Start { get => start; set => start = value; }
        public int Length { get => length; set => length = value; }
        public int AddressOffset { get => addressOffset; set => addressOffset = value; }
    }
    public class PLCHelper
    {
        public static AddressArg GetAddressArg(List<MetaTag> metaTags)
        {
            //list为空的报错处理
            AddressArg addressArg= new AddressArg();

            List<float> floats = new List<float>();

            string s1_address = Regex.Replace(metaTags[0].Address, "[a-z]", "", RegexOptions.IgnoreCase);
            byte s1_datatype = metaTags[0].DataType;
            int lastDataLenght = 1;
            var f_min = float.Parse(s1_address);
            var f_max = float.Parse(s1_address);
            if (s1_datatype == (byte)DataType.BOOL)
                lastDataLenght = 1;
            if (s1_datatype == (byte)DataType.INT)
                lastDataLenght = 2;
            if (s1_datatype == (byte)DataType.FLOAT)
                lastDataLenght = 4;
            if (s1_datatype == (byte)DataType.IFLOAT)
                lastDataLenght = 2;
			if (s1_datatype == (byte)DataType.PBOOL)
				lastDataLenght = 2;
			foreach (MetaTag metaTag in metaTags) 
            {
                string s_address = Regex.Replace(metaTag.Address, "[a-z]", "", RegexOptions.IgnoreCase);
                byte s_datatype = metaTag.DataType;
                float f = float.Parse(s_address);
                if (f<f_min) f_min= f;
                if (f > f_max)
                {
                    f_max = f;
                    if (s_datatype == (byte)DataType.BOOL)
                        lastDataLenght = 1;
                    if (s_datatype == (byte)DataType.INT)
                        lastDataLenght = 2;
                    if (s_datatype == (byte)DataType.FLOAT)
                        lastDataLenght = 4;
                    if (s_datatype == (byte)DataType.IFLOAT)
                        lastDataLenght = 2;
					if (s_datatype == (byte)DataType.PBOOL)
						lastDataLenght = 2;
				} 
            }
            
            addressArg.Start = (int)f_min;
            //addressArg.Length = (int)f_max + lastDataLenght - 1;
            addressArg.Length = (int)f_max - (int)f_min + lastDataLenght ;

            addressArg.AddressOffset = (int)f_min;

            return addressArg;
        }

        public static AddressArg GetAddressArg_Modbus_Word(List<MetaTag> metaTags)
        {
            //list为空的报错处理
            AddressArg addressArg = new AddressArg();

            List<float> floats = new List<float>();

            string s1_address = Regex.Replace(metaTags[0].Address, "[a-z]", "", RegexOptions.IgnoreCase);
            byte s1_datatype = metaTags[0].DataType;
            int lastDataLenght = 1;
            var f_min = float.Parse(s1_address);
            var f_max = float.Parse(s1_address);
            if (s1_datatype == (byte)DataType.BOOL)
                lastDataLenght = 1;
            if (s1_datatype == (byte)DataType.INT)
                lastDataLenght = 1;
            if (s1_datatype == (byte)DataType.FLOAT)
                lastDataLenght = 2;
            foreach (MetaTag metaTag in metaTags)
            {
                string s_address = Regex.Replace(metaTag.Address, "[a-z]", "", RegexOptions.IgnoreCase);
                byte s_datatype = metaTag.DataType;
                float f = float.Parse(s_address);
                if (f < f_min) f_min = f;
                if (f > f_max)
                {
                    f_max = f;
                    if (s_datatype == (byte)DataType.BOOL)
                        lastDataLenght = 1;
                    if (s_datatype == (byte)DataType.INT)
                        lastDataLenght = 1;
                    if (s_datatype == (byte)DataType.FLOAT)
                        lastDataLenght = 2;
                }
            }

            addressArg.Start = (int)f_min;
            //addressArg.Length = (int)f_max + lastDataLenght -1;
            addressArg.Length = (int)f_max - (int)f_min + lastDataLenght;

            addressArg.AddressOffset = (int)f_min;

            return addressArg;
        }
        //public static NodeId[] GetAddressArg_OpcUaClient_Arraay(List<MetaTag> metaTags)
        //{
        //    var nodes = new NodeId[metaTags.Count];
        //    int i = 0;
        //    foreach(var item in metaTags)
        //    {
        //        var node = new NodeId(item.Address);
        //        nodes[i] = node;
        //        i++; 
        //    }
        //    return nodes;
            
        //}

        public static int GetSiemensPDU(string s7type)
        {
            switch (s7type)
            {
                case "S7400":
                    return 480;
                case "S7300":
                    return 240;
                case "S71200":
                    return 120;
                case "S71500":
                    return 480;
            }
            return 0;
        }
    }
}
