using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Common;
using Domain;
using RCUHost.Protocols;

namespace RCUHost.Implement
{
    /// <summary>
    /// 发送命令给rcu，设置寄存器内容（改为下发房型房号）
    /// </summary>
    public class HostRegisterSetReceiver : GenericReceiverBase, IHostRegisterSetReceiver
    {
        //private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostRegisterSetReceiver));
        /// <summary>
        /// 下发所有寄存器内容
        /// </summary>
        /// <param name="host"></param>
        public void Send(Host host)
        {
            var data = CreateDevicePacket(host, new byte[] { });
            //logger.Error(Tools.ByteToString(data));
            SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
        }
        /// <summary>
        /// 指定下发内容
        /// </summary>
        /// <param name="host"></param>
        /// <param name="sendData"></param>
        public void Send(Host host, byte[] sendData)
        {
            var data = CreateDevicePacket(host, sendData);
            SendAndPushCommandQueue(data, host.HostNumber, host.MAC);
        }

        private byte[] CreateDevicePacket(Host host, byte[] sendData)
        {
            SystemHeader systemHeader = CreateSystemHeader();
            byte[] deviceSecretDataPacket = sendData.Length > 0 ? sendData : CreateDeviceDataPacket(host);
            int size = StructConverter.SizeOf(systemHeader) + deviceSecretDataPacket.Length + 2;

            systemHeader.FrameLength = (ushort)size;

            using (MemoryStream stream = new MemoryStream(size))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(StructConverter.StructToBytes(systemHeader));
                    writer.Write(deviceSecretDataPacket);
                    writer.Write(new byte[] { 0, 0 });
                    return stream.ToArray();
                }
            }
        }
        /// <summary>
        /// 下发内容
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        private byte[] CreateDeviceDataPacket(Host host)
        {
            using (MemoryStream buffer = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(buffer))
                {
                    writer.Write(new byte[] { 0x0B });//存储寄存器内容的个数

                    writer.Write(new byte[] { 0x38, 0, 0, 0 });//项目编号
                    writer.Write(Tools.LongToByte4(Convert.ToInt64(host.SysHotel.Code)));// Encoding.Default.GetBytes(host.SysHotel.Code), 0, 4);

                    writer.Write(new byte[] { 0x44, 0, 0, 0 });//房号ID
                    writer.Write(BitConverter.GetBytes(host.ID), 0, 4);

                    writer.Write(new byte[] { 0x48, 0, 0, 0 });//房型ID
                    writer.Write(Tools.LongToByte4(Convert.ToInt64(host.RoomType.ID)));//BitConverter.GetBytes(host.RoomType.ID), 0, 4);

                    byte[] roomNumber = Tools.GetBytes(host.RoomNumber, 16);
                    writer.Write(new byte[] { 0, 0x01, 0, 0 });//房号名称（第1个寄存器）
                    writer.Write(roomNumber, 0, 4);
                    writer.Write(new byte[] { 0x04, 0x01, 0, 0 });//房号名称（第2个寄存器）
                    writer.Write(roomNumber, 4, 4);
                    writer.Write(new byte[] { 0x08, 0x01, 0, 0 });//房号名称（第3个寄存器）
                    writer.Write(roomNumber, 8, 4);
                    writer.Write(new byte[] { 0x0C, 0x01, 0, 0 });//房号名称（第4个寄存器）
                    writer.Write(roomNumber, 12, 4);

                    byte[] roomType = Tools.GetBytes(host.RoomType.Name, 16);
                    writer.Write(new byte[] { 0x10, 0x01, 0, 0 });//房型名称（第1个寄存器）
                    writer.Write(roomType, 0, 4);
                    writer.Write(new byte[] { 0x14, 0x01, 0, 0 });//房型名称（第2个寄存器）
                    writer.Write(roomType, 4, 4);
                    writer.Write(new byte[] { 0x18, 0x01, 0, 0 });//房型名称（第3个寄存器）
                    writer.Write(roomType, 8, 4);
                    writer.Write(new byte[] { 0x1C, 0x01, 0, 0 });//房型名称（第4个寄存器）
                    writer.Write(roomType, 12, 4);

                    return buffer.ToArray();
                }
            }
        }

        public override CommandType CommandType
        {
            get { return CommandType.SetRegister; }
        }
    }
}
