using Edge.Core.Parser.BinaryParser.MessageEntity;
using Edge.Core.Parser.BinaryParser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HengshanPaymentTerminal
{

    public class MessageTemplateLookup : IMessageTemplateLookup
    {
        /// <summary>
        /// Gets the default singleton instance of type MessageTemplateLookup.
        /// </summary>
        public static MessageTemplateLookup Default { get; } = new MessageTemplateLookup();

        /// <summary>
        /// Dictionary that holds the commands and types
        /// </summary>
        private static readonly Dictionary<string, string> messageCodeToTypeStrDict = new Dictionary<string, string>();

        static MessageTemplateLookup()
        {
            messageCodeToTypeStrDict.Add("0x10",
                "HengshanPaymentTerminal.MessageEntity.Incoming.HeartBeatMessage,HengshanPaymentTerminal");

            messageCodeToTypeStrDict.Add("0x55",
                "HengshanPaymentTerminal.MessageEntity.Incoming.CommonAnswerBack,HengshanPaymentTerminal");

            messageCodeToTypeStrDict.Add("0x18",
                "HengshanPaymentTerminal.MessageEntity.Incoming.OrderFromMachine,HengshanPaymentTerminal");

            messageCodeToTypeStrDict.Add("0x65",
                "HengshanPaymentTerminal.MessageEntity.Incoming.AuthorizationResponse,HengshanPaymentTerminal");

            messageCodeToTypeStrDict.Add("0x66",
                "HengshanPaymentTerminal.MessageEntity.Incoming.UnAhorizationResponse,HengshanPaymentTerminal");
            //messageCodeToTypeStrDict.Add("0x19",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.CancelAuthRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x1C",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.FuelingDataRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x20",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.ChangeAuthModeAck,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x21",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.QueryGrayRecordRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x2D",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.TransactionDataRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x30",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.PaymentRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x45",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.LockOrUnlockPumpAck,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x24",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.VolumeTotal,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x25",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.DataDownloadRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x27",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.DataContentRequest,HengshanPaymentTerminal");

            //messageCodeToTypeStrDict.Add("0x49",
            //    "HengshanPaymentTerminal.MessageEntity.Incoming.Totalizer,HengshanPaymentTerminal");
        }

        public MessageTemplateBase GetMessageTemplateByRawBytes(byte[] bytes)
        {
            var msgHandleCode = bytes.Skip(6).First();

            var key = "0x" + msgHandleCode.ToString("X").PadLeft(2, '0');
            if (messageCodeToTypeStrDict.ContainsKey(key))
            {
                var found = messageCodeToTypeStrDict[key];
                var targetType = Type.GetType(found);
                var targetInstance = (MessageTemplateBase)Activator.CreateInstance(targetType);
                return targetInstance;
            }
            else
            {
                throw new ArgumentException("Can't find correlated message entity type for incoming raw message bytes: " +
                    bytes.Select(s => s.ToString("X").PadLeft(2, '0')).Aggregate((n, acc) => n + " " + acc));
            }
        }
    }
}
