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

namespace Networking
{

    public class LogicProtocols
    {
        public const short SendLogicList = 200;
        public const short SendInventory = 201;
        public const short SendRoundTime = 202;
        public const short SendNotification = 203;
        public const short SendScore = 205;
        public const short SendLives = 204;


        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();
                }
            }


        }

        public class InventoryMsg : MessageBase
        {
            public Inventory.Data[] bagItems;

            public InventoryMsg() { }
            public InventoryMsg(Inventory inventory)
            {
                this.bagItems = inventory.BagItems.ToArray();
            }

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

                writer.Write(bagItems.Length);

                foreach(Inventory.Data data in bagItems)
                {
                    BlockToken token = data.element.ToToken();
                    byte[] bytes = Utility.ObjectToByteArray(token);
                    writer.WriteBytesAndSize(bytes, bytes.Length);

                    writer.Write(data.Count);
                    writer.Write(data.isInfinit);
                }
            }

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

                int count = reader.ReadInt32();
                bagItems = new Inventory.Data[count];

                for (int i = 0; i < count; i++)
                {
                    BlockToken token = Utility.ByteArrayToObject<BlockToken>(reader.ReadBytesAndSize());
                    bagItems[i] = new Inventory.Data();
                    bagItems[i].element = token.ToLogicBlock();
                    bagItems[i].Count = reader.ReadInt32();
                    bagItems[i].isInfinit = reader.ReadBoolean();
                }
            }
        }

        public class FloatMsg : MessageBase
        {
 
            public float Float;

            public FloatMsg() { }
            public FloatMsg(float Float)
            {
                this.Float = Float;
            }

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

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

                Float = reader.ReadSingle();
            }
        }

        public class StringMsg : MessageBase
        {

            public float String;

            public StringMsg() { }
            public StringMsg(float String)
            {
                this.String = String;
            }

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

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

                String = reader.ReadSingle();
            }
        }

        public class EmptyMsg : MessageBase
        {

        }
    }
}