using System;
using ATK;
namespace Zebra_RFID_Mobile
{
    class GenericReader
    {
        MetaData metaData;
        BaseReciever genericListner;
        private GenericReader() 
        {
            genericListner = BaseReciever.getInstance(); 
        }

        //create an object of SingleObject
        private static GenericReader instance = new GenericReader();

        //Get the only object available
        public static GenericReader getInstance()
        {
            if (instance == null)
            {
                instance = new GenericReader();
            }
            return instance;
        }


        public void returnCommandObject(string rawString)
        {
            DateTime timenow = DateTime.Now;
            System.Diagnostics.Debug.WriteLine(timenow.Hour + ":" + timenow.Minute + ":" + timenow.Second + ":" + timenow.Millisecond + " <<"+rawString);
            try 
            {
                if (rawString != null)
                {
                    if (rawString == "")
                    {
                            if (ReaderConfig.SettingsStaus.ResponseStatusMsg == null || (ReaderConfig.SettingsStaus.ResponseStatusMsg != null && !ReaderConfig.SettingsStaus.ResponseStatusMsg.StartsWith("Operation")))
                        {
                            genericListner.handleStatusMessages(ReaderConfig.lastMetadataResponse);
                            ReaderConfig.lastMetadataResponse = null;
                        }
                    }
                    else
                    {
                        IMsg msg = ASCIIProcessor.getReplyMsg(rawString, this.metaData);
                        this.getReplyDetails(msg, rawString);
                    }
                }

            }
            catch(Exception e)
            {
                string str = e.Message;
            }           
        }
        
        public void getReplyDetails(IMsg msg , string data)
        {
            try
            {
                if (msg != null)
                {                    
                        switch (msg.getMsgType())
                        {
                            case MSG_TYPE.COMMAND:
                                genericListner.configurationFromReader(msg);
                                break;
                            case MSG_TYPE.METADATA:
                                metaData = (MetaData)msg;                                
                                ReaderConfig.lastMetadataResponse = (Response_Status.FromString(data));
                                genericListner.responseMetaDataParsed(ReaderConfig.lastMetadataResponse);
                            break;
                            case MSG_TYPE.RESPONSE_MSG:
                                genericListner.responseDataParsedFromGenericReader((ResponseMsg)msg);
                                break;
                            case MSG_TYPE.NOTIFICATION_MSG:
                                genericListner.notificationFromReader(msg);
                                break;
                            default:
                                break;
                        }
                  }
            }
            catch(Exception e) 
            {
               string str=e.Message;
            }
        }

        public void stopInventoryTimer()
        {
            genericListner.Startinventory_timer(false, false);
        }
    }
}
 
