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>
	/// 窗帘控制
	/// </summary>
	public class CurtainControlReceiver : GenericReceiverBase, ICurtainControlReceiver
	{
		/// <summary>
		/// 发送窗帘控制
		/// </summary>
		/// <param name="host">主机</param>
		/// <param name="CurtainId">窗帘id</param>
		/// <param name="ctrl">窗帘控制</param>
		public void SendCtrl(Host host, int CurtainId, CurtainCtrl ctrl)
		{
			var data = CreateDataPacket(CurtainId, ctrl);

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

		private byte[] CreateDataPacket(int CurtainId, CurtainCtrl key)
		{
			using (MemoryStream stream = new MemoryStream())
			{
				int headerLen = StructConverter.SizeOf(typeof(SystemHeader));

				stream.Seek(headerLen, SeekOrigin.Begin);

				stream.WriteByte((byte)CurtainId);
				stream.WriteByte((byte)key);
				stream.Write(new byte[] { 0, 0 }, 0, 2);

				SystemHeader systemHeader = CreateSystemHeader((int)stream.Length - headerLen);

				var systemHeaderData = StructConverter.StructToBytes(systemHeader);

				stream.Seek(0, SeekOrigin.Begin);
				stream.Write(systemHeaderData, 0, systemHeaderData.Length);

				return stream.ToArray();
			}
		}

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