using System;
using System.Collections;
using System.Text;
using System.Threading;
using sscs.verbs;
using sscs.common;

/*
 * After the bytes format is finalized for Windows this class might need to 
 * be modified..
 * Depending upon impact either a new method can be invoked after platfrom 
 * check or a Factory pattern can be used to redesign this class.
 */
       
/* 
 * Namespaces used inside SSCS(main) will be of the following format "sscs.*"
 * The NameSpaces available are.... 
 * sscs.common [session, RequestParser]
 * sscs.authentication
 * sscs.verbs;[ssVerb, .......]
 * sscs.cache [secretstore, keychain, secret]
 * sscs.lss;
 * sscs.synchronization;
 * sscs.crypto
 */
 
namespace sscs.common 
{
    /* This class will be used to convert raw bytes to one of the SecretStore 
     * Verbs. Although there is no need to have many instances,
     * this class will NOT be singleton class since we 
     * do not want a hit on performance (synchronization, obtaining locks,
     * releasing locks...etc )
     * Making it singleton is not giving us any advantage versus performance.
     */
    internal class RequestParser
    {
        Hashtable msgIdMap = new Hashtable();

        internal RequestParser()
        {
            msgIdMap.Add(1,"sscs.verbs.OpenSecretStore");
            msgIdMap.Add(2,"sscs.verbs.CloseSecretStore");
            msgIdMap.Add(3,"sscs.verbs.RemoveSecretStore");
            msgIdMap.Add(4,"sscs.verbs.EnumerateKeyChainIds");
            msgIdMap.Add(5,"sscs.verbs.AddKeyChain");
            msgIdMap.Add(6,"sscs.verbs.RemoveKeyChain");
            msgIdMap.Add(7,"sscs.verbs.EnumerateSecretIds");
            msgIdMap.Add(8,"sscs.verbs.ReadSecret");
            msgIdMap.Add(9,"sscs.verbs.WriteSecret");
            msgIdMap.Add(10,"sscs.verbs.RemoveSecret");
            msgIdMap.Add(11,"sscs.verbs.GetSecretStoreInfo");
            msgIdMap.Add(12,"sscs.verbs.GetKeyChainInfo");
            msgIdMap.Add(13,"sscs.verbs.LockCache");
            msgIdMap.Add(14,"sscs.verbs.UnLockCache");
            msgIdMap.Add(15,"sscs.verbs.SetMasterPasscode");
            msgIdMap.Add(16,"sscs.verbs.ReadKey");
            msgIdMap.Add(17,"sscs.verbs.WriteKey");
            msgIdMap.Add(18,"sscs.verbs.SetMasterPassword");
            msgIdMap.Add(19,"sscs.verbs.IsSecretPersistent");
			msgIdMap.Add(20,"sscs.verbs.ObjectSerialization");
        }
	
	    		
        /* Processes the request and returns the corrrect SSverb.
         * This interface works on the class member rawbytes and 
         * returns the result.  		  
         */
        internal SSVerb ParseRequest(byte[] rawbytes)
        {
            if (rawbytes == null)
 	        throw new FormatException("Message format incorrect");
 		       
            String className = GetClassName(rawbytes); 

            SSVerb theVerb = (SSVerb)Activator.CreateInstance(null, className ).Unwrap();
            theVerb.SetMessageContent(rawbytes); 
            
            /* 
             * TBD: We can send the activation params in the same call. 
             */
            //SSVerb theVerb = (SSVerb)Activator.CreateInstance(Type.GetType(className)).Unwrap();
           
            return theVerb;
        }

        private string GetClassName(byte[] ipcbytes)
        {
           /* 
            * Read first two bytes and get ushort
            * Look up table and send class name  
            */
            ushort msgId = BitConverter.ToUInt16(ipcbytes,0);
            return ((String)(msgIdMap[(int)msgId]));
        }
    }
}