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

namespace RCUHost.Implement
{
    /// <summary>
    /// 主机电源控制
    /// </summary>
    public class PowerSupplyControlReceiver : GenericReceiverBase, IPowerSupplyControlReceiver
    {
        public IHostRepository HostRepository { get; set; }

        /// <summary>
        /// 发送主机电源控制
        /// </summary>
        /// <param name="host">主机</param>
        /// <param name="ctrl">电源控制</param>
        public void SendCtrl(Host host, PowerSupplyCtrl ctrl)
        {
            var data = CreateDataPacket(ctrl);
            SendAndPushCommandQueue(data, host.HostNumber, host.MAC);// host.IP, host.Port);
        }

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        private PowerSupplyPacketReply? DecodePowerSupplyPacketReply(byte[] data, int startIndex)
        {
            return StructConverter.BytesToStruct(data, startIndex, typeof(PowerSupplyPacketReply)) as PowerSupplyPacketReply?;
        }

        public override void Process(ReceiverContext context)
        {
            //base.Process(context);

            //int startIndex = StructConverter.SizeOf(context.SystemHeader);

            //PowerSupplyPacketReply? reply = DecodePowerSupplyPacketReply(context.Data, startIndex);

            //if (reply.HasValue)
            //{
            //    var supplyPacketHost = HostRepository.GetByHostNumber(context.SystemHeader.Value.HostNumber.ToString());

            //    if (supplyPacketHost != null)
            //    {
            //        if (reply.Value.Status == PowerSupplyPacketReply.Open)
            //        {
            //            supplyPacketHost.PowerSupply = true;
            //        }
            //        else if (reply.Value.Status == PowerSupplyPacketReply.Close)
            //        {
            //            supplyPacketHost.PowerSupply = false;
            //        }
            //        HostRepository.Update(supplyPacketHost);
            //    }
            //}
        }


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

                stream.Seek(headerLen, SeekOrigin.Begin);

                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.PowerSupplyControl; }
        }
    }
}
