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 TVControlReceiver : GenericReceiverBase, ITVControlReceiver
	{
		/// <summary>
		/// 发送电视按键
		/// </summary>
		/// <param name="host">主机</param>
		/// <param name="key">按键</param>
		public void SendKey(Host host, TvKey key)
		{
			var data = CreateDataPacket( key);

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

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

				stream.Seek(headerLen, SeekOrigin.Begin);

				stream.WriteByte((byte)0);
				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.TvControl; }
		}
	}
}
