using System.Diagnostics;
using System.IO;
using System.Text;
using ActorCore;

namespace Server.Core
{
    public static class ByteHelper
    {
        public static ActorId ReadActorId(this byte[] bytes, int offset)
        {
            return new ActorId(bytes[offset], bytes[offset + 1], bytes[offset + 2]);
        }

        public static void WriteTo(this byte[] bytes, int offset, ActorId actorId)
        {
            bytes[offset] = actorId.ModuleId;
            bytes[offset + 1] = actorId.NodeNum;
            bytes[offset + 2] = actorId.ActorTypeId;
        }

        public static void WriteTo(this byte[] bytes, int offset, short num)
        {
            bytes[offset] = (byte)(num & 0xff);
            bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
        }

        public static void WriteTo(this byte[] bytes, int offset, ushort num)
        {
            bytes[offset] = (byte)(num & 0xff);
            bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
        }

        public static unsafe void WriteTo(this byte[] bytes, int offset, long num)
        {
            byte* bPoint  = (byte*)&num;
            for (int i = 0; i < sizeof(long); ++i)
            {
                bytes[offset + i] = bPoint[i];
            }
        }
        
        public static void WriteTo(this byte[] bytes, int offset, int num)
        {
            //跟这个意思是一样的，把num解析为byte 拷贝到bytes中
            //BitConverter.GetBytes(num);
			
            bytes[offset] = (byte)(num & 0xff);
            bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
            bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
            bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
        }

        public static void WriteTo(this byte[] bytes, int offset, uint num)
        {
            bytes[offset] = (byte)(num & 0xff);
            bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
            bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
            bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
        }

        public static void WriteInt(this MemoryStream memoryStream, int num)
        {
            memoryStream.WriteByte((byte)(num & 0xff));
            memoryStream.WriteByte( (byte)((num & 0xff00) >> 8));
            memoryStream.WriteByte((byte)((num & 0xff0000) >> 16));
            memoryStream.WriteByte((byte)((num & 0xff000000) >> 24));
        }

    }
}