using Castle.Components.DictionaryAdapter.Xml;
using Edge.Core.Domain.FccOrderInfo;
using Edge.Core.Parser.BinaryParser.MessageEntity;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace HengshanPaymentTerminal.MessageEntity.Incoming
{
    /// <summary>
    /// 油机推送订单信息数据对象
    /// </summary>
    public class OrderFromMachine:CommonMessage
    {
        /// <summary>
        /// 枪号
        /// </summary>
        public int nozzleNum { get; set; }

        /// <summary>
        /// 交易流水号
        /// </summary>
        public int ttc { get; set; }

        /// <summary>
        /// 加油时间
        /// </summary>
        public DateTime dispenserTime { get; set; }

        /// <summary>
        /// 挂枪时间
        /// </summary>
        public DateTime endTime { get; set; }

        /// <summary>
        /// 油品代码
        /// </summary>
        public string oilCode { get; set; }

        /// <summary>
        /// 付款方式/地点
        /// </summary>
        public byte payType { get; set; }

        /// <summary>
        /// 价格
        /// </summary>
        public decimal price { get; set; }

        /// <summary>
        /// 金额
        /// </summary>
        public decimal amount { get; set; }

        /// <summary>
        /// 升数
        /// </summary>
        public decimal volume { get; set; }

        public override byte[] ToCommonByteArray()
        {
            byte[] content = new byte[] { 0x55, this.Handle, (byte)this.nozzleNum, ((byte)RESULT.OVER) };
            return content2data(content);
        }

        public override CommonMessage ToObject(byte[] datas)
        {
            getBaseData(datas);
            this.nozzleNum = datas[7];

            this.ttc = Bytes2Number<int>(datas, 8, 4);

            Span<byte> dispenserSpan = datas.AsSpan(12, 7);
            this.dispenserTime = bytes2DateTime(dispenserSpan.ToArray());

            Span<byte> endSpan = datas.AsSpan(19, 7);
            this.endTime = bytes2DateTime(endSpan.ToArray());

            Span<byte> oilCodeSpan = datas.AsSpan(26, 2);
            this.oilCode = BitConverter.ToString(oilCodeSpan.ToArray()).Replace("-", "");

            this.payType = datas[28];

            ushort priceShort = Bytes2Number<ushort>(datas,29, 2);
            this.price = priceShort / 100m;

            uint amountInt = Bytes2Number<uint>(datas, 31, 3);
            this.amount = amountInt / 100m;

            uint volumeInt = Bytes2Number<uint>(datas, 34, 3);
            this.volume = volumeInt / 100m;

            return this;
        }

        public FccOrderInfo ToComponent()
        {
            
            return new FccOrderInfo
            {
                Ttc = this.ttc,
                AuthorizationTime = this.dispenserTime,
                EndTime = this.endTime,
                NozzleNum = this.nozzleNum,
                PayType = this.payType,
                PaymentStatus = payType == (int)0x21 ? 0 : 4 ,
                Amount = this.amount,
                Volume = this.volume,
                //AmountPayable = this.amount,
                UploadState = 0,
                IsDelete = 0,
                PaymentTime = null,
                OilName = "",
                CloundOrderId = null
            };
        }

        /// <summary>
        /// 补充授权订单数据
        /// </summary>
        /// <param name="order">当前数据库中订单数据</param>
        /// <returns></returns>
        public FccOrderInfo PaddingAuthorizationOrderData(FccOrderInfo order)
        {
            order.AuthorizationTime = this.dispenserTime;
            order.EndTime = this.endTime;
            order.Amount = this.amount;
            order.Volume = this.volume;
            return order;
        }
    }
}
