using Edge.Core.Parser.BinaryParser.Attributes;
using Edge.Core.Parser.BinaryParser.MessageEntity;
using System;
using System.Collections.Generic;

namespace PressureGage_3051.MessageEntity
{
    public enum FunctionCode
    {
        /// <summary>
        /// Read Holding Registers
        /// </summary>
        READ_HOLDING_REGISTERS = 0x03,

        /// <summary>
        /// Read Input Registers
        /// </summary>
        READ_INPUT_REGISTERS = 0x04,

        /// <summary>
        /// Preset Single Register
        /// </summary>
        PRESET_SINGLE_REGISTER = 0x06,

        /// <summary>
        /// Preset Multiple Registers
        /// </summary>
        PRESET_MULTIPLE_REGISTER = 0x10,
    }

    public abstract class MessageBase : MessageTemplateBase
    {
        [Format(1, EncodingType.BIN, -9990)]
        public byte Address { get; set; }

        [Format(1, EncodingType.BIN, -9980)]
        public FunctionCode FunctionCode { get; set; }

        [EnumerableFormat("%cascade", -9970)]
        public List<byte> RawDataField { get; set; }

        public static float BytesToFloat(byte[] hexBytes)
        {
            for (int i = 1; i < hexBytes.Length;)
            {
                var t = hexBytes[i - 1];
                hexBytes[i - 1] = hexBytes[i];
                hexBytes[i] = t;
                i += 2;
            }

            return BitConverter.ToSingle(hexBytes, 0);
        }
    }
}