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

namespace RCUHost.Implement
{
    public class RoomStatusChangedReceiver : GenericReceiverBase, IRoomStatusChangedReceiver
    {
        public void SendRoomStatus(Host host, RoomStatus status)
        {
            byte[] data = CreateRoomStatusChangedRequestPacket(status);

            var U = data.Take(16).ToList();
            //U.Add(0x00);
            U.Add(0x01);
            U.AddRange(new byte[] { 0x00, 0x00 });
            SendAndPushCommandQueue(U.ToArray(), host.HostNumber, host.MAC);// host.IP, host.Port);

            //ProcessRoomStatus(host, status);
        }
        public void SendRoomStatusSelf(string HostNumber, string Mac, RoomStatus status)
        {
            byte[] data = CreateRoomStatusChangedRequestPacket(status);

            SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);

            //ProcessRoomStatus(host, status);
        }

        /// <summary>
        /// 新版本要 状态位要变成2个字节
        /// </summary>
        /// <param name="HostNumber"></param>
        /// <param name="Mac"></param>
        /// <param name="status"></param>
        /// <param name="FrameNo"></param>
        public void SendRoomStatusSelfWithFrameNo(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo)
        {
            //AA 55 12 00 54 33 53 41 0C 22 80 FF FF FF FF 04 00 80 CO
            byte[] data = CreateRoomStatusChangedRequestPacket(status);

            data[9] = FrameNo[0];
            data[10] = FrameNo[1];
            var U = data.Take(16).ToList();
            U.Add(0x00);
            U.AddRange(new byte[] { 0x00, 0x00 });
            SendAndPushCommandQueue(U.ToArray(), HostNumber, Mac);// host.IP, host.Port);

            //ProcessRoomStatus(host, status);
        }

        /// <summary>
        /// 处理房态相关逻辑
        /// </summary>
        /// <param name="host"></param>
        /// <param name="roomStatus"></param>
        private void ProcessRoomStatus(Host host, RoomStatus roomStatus)
        {
            #region 处理“退房保险箱关”异常



            #endregion
        }

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

        #region Private Methods

        private byte[] CreateRoomStatusChangedRequestPacket(RoomStatus status)
        {
            SystemHeader systemHeader = CreateSystemHeader();

            RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);

            int size = StructConverter.SizeOf(systemHeader) + StructConverter.SizeOf(roomStatusChangedPacket);
            size = size + 1;

            systemHeader.FrameLength = (ushort)size;

            byte[] buffer = new byte[size];

            byte[] src1 = StructConverter.StructToBytes(systemHeader);
            Array.Copy(src1, 0, buffer, 0, src1.Length);

            byte[] src3 = StructConverter.StructToBytes(roomStatusChangedPacket);
            Array.Copy(src3, 0, buffer, src1.Length, src3.Length);

            return buffer;
        }

        private RoomStatusChangedPacket CreateRoomStatusChangedPacket(RoomStatus status)
        {
            return new RoomStatusChangedPacket { Status = (byte)status.ID };
        }

        #endregion
    }
}
