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 AskRoomStatusChangedReceiver : GenericReceiverBase, IAskRoomStatusChangedReceiver
    {
        public void SendRoomStatusSelf(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo)
        {
            byte[] data = CreateRoomStatusChangedRequestPacket(status);

            //AA 55 11 00 54 33 53 41 32 
            //03 80  帧号
            //E9 03 6B 24 33 13
            data[9] = FrameNo[0];
            data[10] = FrameNo[1];
            SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);
        }
        public void SendRoomStatusSelfNew(string HostNumber, string Mac, RoomStatus status, byte[] FrameNo, byte PMSState)
        {
            byte[] data = CreateRoomStatusChangedRequestPacketNew(status, PMSState);

            //AA 55 11 00 54 33 53 41 32 
            //03 80  帧号
            //E9 03 6B 24 33 13
            data[9] = FrameNo[0];
            data[10] = FrameNo[1];
            SendAndPushCommandQueue(data, HostNumber, Mac);// host.IP, host.Port);
        }

        /// <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.AskRoomStatus; }
        }

        #region Private Methods

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

            RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);

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

            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 byte[] CreateRoomStatusChangedRequestPacketNew(RoomStatus status, byte PMS_ChangeState)
        {
            SystemHeader systemHeader = CreateSystemHeader();

            RoomStatusChangedPacket roomStatusChangedPacket = CreateRoomStatusChangedPacket(status);

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

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

            var UUA1 = buffer.Take(buffer.Length - 2).ToList();
            UUA1.Add(PMS_ChangeState);
            UUA1.AddRange(new byte[] { 0x00, 0x00 });
            var buffer1 = UUA1.ToArray();

            return buffer1;
        }

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

        #endregion
    }
}
