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

namespace RCUHost.Implement
{
    public class UpgradeProgressBar : GenericReceiverBase, IUpgradeProgressBar
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(UpgradeProgressBar));
        public IHostRepository HostRepository { get; set; }

        public override void Process(ReceiverContext context)
        {
            string HostNumberOnly = context.SystemHeader.Value.HostNumber.ToString();
            int startIndex = StructConverter.SizeOf(context.SystemHeader);

            Host host = null;
            string Key = CacheKey.HostInfo_Key_HostNumber + "_" + HostNumberOnly;
            object obj = MemoryCacheHelper.Get(Key);
            if (obj != null)
            {
                host = obj as Host;

                var Da = context.Data;

                DecodeUpdateHostPacketReply(Da, startIndex, host);
            }
            //if (host != null)
            //{
            //    string dataHex = Tools.ByteToString(context.Data);
            //    int offset = StructConverter.SizeOf(context.SystemHeader);
            //    int length = context.Data.Length - offset - 2;
            //}

            //if (reply.HasValue)
            //{

            //}
        }
        public override CommandType CommandType
        {
            get { return CommandType.UpdateProgressBar; }
        }


        /// <summary>
        /// 解码 UpdateHostPacketReply
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        private void DecodeUpdateHostPacketReply(byte[] data, int startIndex, Host host)
        {
            var DDD = data.Skip(startIndex).ToList();
            var upgrade_status = DDD.Take(1).ToList().FirstOrDefault();
            string Upgrade_status = "";
            if (upgrade_status == 0x01)
            {
                Upgrade_status = "开始下载";
            }
            else if (upgrade_status == 0x02)
            {
                Upgrade_status = "下载中";
            }
            else if (upgrade_status == 0x03)
            {
                Upgrade_status = "下载完成,校验中";
            }
            else if (upgrade_status == 0x04)
            {
                Upgrade_status = "校验完成,RCU升级中";
            }
            else if (upgrade_status == 0x05)
            {
                Upgrade_status = "超时失败";
            }
            else
            {

            }
            string UpgradeFileType = "";
            var upgradeFileType = DDD.Skip(1).Take(1).ToList().FirstOrDefault();
            if (upgradeFileType == 0x00)
            {
                UpgradeFileType = "固件升级";
            }
            else
            {
                UpgradeFileType = "配置升级";
            }
            var FileBlockTotalCount = DDD.Skip(2).Take(2);
            var FileCurrentNumber = DDD.Skip(4).Take(2);
            byte FileLen = DDD.Skip(6).Take(1).ToList().FirstOrDefault();
            var FileName = DDD.Skip(7).Take(FileLen).ToList();


            int hotelid = host.SysHotel.ID;

            BarData bbb = new BarData();
            bbb.HostID = host.ID;
            bbb.Upgrade_status = Upgrade_status;
            bbb.UpgradeFileType = UpgradeFileType;
            ushort a = BitConverter.ToUInt16(FileBlockTotalCount.ToArray(), 0);
            ushort b = BitConverter.ToUInt16(FileCurrentNumber.ToArray(), 0);
            bbb.FileBlockTotalCount = a;
            bbb.FileCurrentNumber = b;
            double aa = (double)a;
            double bb = (double)b;
            double aaa = (bb / aa) * 100;
            int roundedNumber = (int)Math.Round(aaa);
            bbb.BaiFenBi = roundedNumber.ToString() + "%";
            bbb.FileName = Encoding.UTF8.GetString(FileName.ToArray());

            UploadCurrentVersionReceiver.UP_Grade_Json(host, bbb);
        }
    }
    public class BarData
    {
        public int HostID { get; set; }
        public string Upgrade_status = "";
        public string Upgrade_DateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        public string UpgradeFileType = "";
        public ushort FileBlockTotalCount = 0;
        public ushort FileCurrentNumber = 0;
        public string BaiFenBi = "";
        public string FileName = "";
        public string Version = "";
        public string ConfiguraVersion = "";
    }
}
