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

namespace iSTP_Embedded
{
    /// <summary>
    /// Methods for converting Binary values
    /// </summary>
    public static class BinaryConverter
    {
        // Make NPP .NET code Library Independent
        private static bool IsLittleEndian = BitConverter.IsLittleEndian;

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, UInt64 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(UInt64));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(UInt64); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((UInt64*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, Int64 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(Int64));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(Int64); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((Int64*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, UInt32 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(UInt32));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(UInt32); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((UInt32*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, Int32 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(Int32));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(Int32); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((Int32*)Memory) = Number;
                }
            }
        }


        public static unsafe void GetBytes(Byte[] Buffer, int Offset, Single Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(Single));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(Single); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((Single*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, UInt16 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(UInt16));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(UInt16); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((UInt16*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, Int16 Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(Int16));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(Int16); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((Int16*)Memory) = Number;
                }
            }
        }

        public static unsafe void GetBytes(Byte[] Buffer, int Offset, Double Number)
        {
            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Src = ((Byte*)&Number + sizeof(Double));
                    Byte* Dest = Memory;
                    for (int i = 0; i < sizeof(Double); i++)
                    {
                        Src--;
                        *Dest = *Src;
                        Dest++;
                    }
                }
                else
                {
                    *((Double*)Memory) = Number;
                }
            }
        }

        public static unsafe UInt64 GetUInt64(Byte[] Buffer, int Offset)
        {
            UInt64 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(UInt64));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(UInt64); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(UInt64*)Memory;
                }

            }

            return Result;
        }

        public static unsafe Int64 GetInt64(Byte[] Buffer, int Offset)
        {
            Int64 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(Int64));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(Int64); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(Int64*)Memory;
                }

            }

            return Result;
        }

        public static unsafe UInt32 GetUInt32(Byte[] Buffer, int Offset)
        {
            UInt32 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(UInt32));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(UInt32); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(UInt32*)Memory;
                }

            }

            return Result;
        }

        public static unsafe Int32 GetInt32(Byte[] Buffer, int Offset)
        {
            Int32 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(Int32));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(Int32); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(Int32*)Memory;
                }

            }

            return Result;
        }

        public static unsafe UInt16 GetUInt16(Byte[] Buffer, int Offset)
        {
            UInt16 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(UInt16));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(UInt16); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(UInt16*)Memory;
                }

            }

            return Result;
        }

        public static unsafe Int16 GetInt16(Byte[] Buffer, int Offset)
        {
            Int16 Result;

            fixed (Byte* Memory = &(Buffer[Offset]))
            {
                if (true == IsLittleEndian)
                {
                    Byte* Dest = ((Byte*)&Result + sizeof(Int16));
                    Byte* Src = Memory;
                    for (int i = 0; i < sizeof(Int16); i++)
                    {
                        Dest--;
                        *Dest = *Src;
                        Src++;
                    }
                }
                else
                {
                    Result = *(Int16*)Memory;
                }

            }

            return Result;
        }

        public static unsafe Single GetSingle(Byte[] Buffer, int Offset)
        {
            Single Result;
            try
            {
                fixed (Byte* Memory = &(Buffer[Offset]))
                {
                    if (true == IsLittleEndian)
                    {
                        Byte* Dest = ((Byte*)&Result + sizeof(Single));
                        Byte* Src = Memory;
                        for (int i = 0; i < sizeof(Single); i++)
                        {
                            Dest--;
                            *Dest = *Src;
                            Src++;
                        }
                    }
                    else
                    {
                        Result = *(Single*)Memory;
                    }
                }
            }
            catch (Exception)
            {
                Result = 0;
            }

            return Result;
        }

        public static unsafe Double GetDouble(Byte[] Buffer, int Offset)
        {
            Double Result;
            try
            {
                fixed (Byte* Memory = &(Buffer[Offset]))
                {
                    if (true == IsLittleEndian)
                    {
                        Byte* Dest = ((Byte*)&Result + sizeof(Double));
                        Byte* Src = Memory;
                        for (int i = 0; i < sizeof(Double); i++)
                        {
                            Dest--;
                            *Dest = *Src;
                            Src++;
                        }
                    }
                    else
                    {
                        Result = *(Double*)Memory;
                    }

                }
            }
            catch (Exception)
            {
                Result = 0;
            }

            return Result;
        }
    }
}
