using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

namespace Networking
{

    public class LogicProtocols
    {
        public const short SendLogicList = 200;

        public class LogicMsg : MessageBase
        {

            public LogicBlock[] elements;

            public LogicMsg() { }
            public LogicMsg(LogicBlock[] elements)
            {
                this.elements = elements;
            }

            public override void Serialize(NetworkWriter writer)
            {
                base.Serialize(writer);

                writer.Write(elements.Length);

                foreach (LogicBlock block in elements)
                {
                    BlockToken token = block.ToToken();
                    byte[] bytes = Utility.ObjectToByteArray(token);
                    writer.WriteBytesAndSize(bytes,bytes.Length);
                }
            }

            public override void Deserialize(NetworkReader reader)
            {
                base.Deserialize(reader);

                int count = reader.ReadInt32();
                elements = new LogicBlock[count];

                for (int i = 0; i < count; i++)
                {
                    BlockToken token = Utility.ByteArrayToObject<BlockToken>(reader.ReadBytesAndSize());
                    elements[i] = token.ToLogicBlock();
                }
            }


        }

    }
}