using ProjectManagementSystem.Device.Core;
using ProjectManagementSystem.Device.Extenions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Device.Command
{
    public class CommandBox : IDeviceCommand
    {
        protected byte[] buffer = new byte[14] { 0x10, 0x40, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };

        public byte Function { get; set; }
        public int Address { get; set; }
        public int Spare { get; set; }
        public int Route { get; set; }
        public int Mark { get; set; }
        public byte MarkFunction { get; set; }
        public byte AgvId { get; set; }

        public virtual byte[] Serialization()
        {
            int route = 0x3030 + Route;

            buffer[1] = Function;
            buffer[3] = (byte)(Address + 0x30);

            //if (Spare > 0)
            //{
            //    buffer[4] = (byte)((Spare + 80) / 256 + 0x30);
            //    buffer[5] = (byte)((Spare + 80) % 256);
            //}

            if (Spare > 0)
            {
                int tempSpare = 0x3050 + Spare;
                buffer[4] = (byte)(tempSpare / 256);
                buffer[5] = (byte)(tempSpare % 256);
            }

            buffer[6] = (byte)((route >> 8) & 0xFF);
            buffer[7] = (byte)(route & 0xFF);
            buffer[8] = (byte)((Mark & 0xFF00) >> 8);
            buffer[9] = (byte)(Mark & 0x00FF);
            buffer[10] = MarkFunction;
            buffer[11] = AgvId;
            buffer[12] = buffer.XOR_Check(12);

            return buffer;
        }
    }
}
