/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/


using System;
using System.Collections.Specialized;


namespace Novell.CASA
{	
	/// <summary>
	/// Represents the SecretStore
	/// </summary>
	public class SecretStore : MarshalByRefObject
	{		
		private string m_sMasterPassword = null;
		private static string SESSION_KEY_CHAIN = "SSCS_SESSION_KEY_CHAIN_ID";
		public static string LOCAL_REMOTE_KEY_CHAIN = "SSCS_LOCAL_REMOTE_KEY_CHAIN_ID";

		private NativeCalls m_NativeCalls = null;
		private IntPtr m_hsc = IntPtr.Zero;

		public SecretStore()
		{
			doSetup();
		}

		private void doSetup()
		{												
			m_NativeCalls = new NativeCalls();
			m_hsc = m_NativeCalls.openSecretStore("SecretStore");
			//Console.WriteLine("Session handle: {0}", m_hsc);
		}

		public static SecretStore getInstance()
		{
			SecretStore newSS = new SecretStore();		
			return newSS;
		}

		public void ReleaseInstance()
		{
			if (m_hsc != IntPtr.Zero)
				m_NativeCalls.closeSecretStore(m_hsc);
		}

		/*
		public static void ReleaseInstance()
		{
			// TODO:

		}
		*/


		internal Secret getSecret(string sKeyChainID, uint ssFlags, string sSecretID, int iSecretType, string sEPPassword)
		{
			if (m_NativeCalls != null)
				return m_NativeCalls.getSecret(m_hsc, sKeyChainID, ssFlags, sSecretID, iSecretType, sEPPassword);
			else			
				return null;			
		}
		


		public Secret getSecret(string id)
		{
			string sSecretId = id;
			int iSecretType = Secret.SS_BINARY;

			//Console.WriteLine("GetSecret called for "+id);
			if (sSecretId.StartsWith("SS_CredSet"))
			{
				sSecretId = sSecretId.Substring(11);
				iSecretType = Secret.SS_CREDSET;
			}
			else if (sSecretId.StartsWith("SS_App"))
			{
				sSecretId = sSecretId.Substring(10);
				iSecretType = Secret.SS_APP;
			}

			return getSecret(0, sSecretId, iSecretType, "");
		}

		public Secret getSecret(uint ssFlags, string sSecretID, int iSecretType, string sEPPassword)
		{
			return getSecret(SESSION_KEY_CHAIN, ssFlags, sSecretID, iSecretType, sEPPassword);
		}

		public void setSecret(uint flags, string sKeyChainID, Secret secret, int iSecretType)
		{
			m_NativeCalls.setSecret(m_hsc, sKeyChainID, flags, secret, iSecretType);	
		}

		public void setSecret(uint flags, Secret secret, int iSecretType)
		{
			// call the native calls to write this secret
			m_NativeCalls.setSecret(m_hsc, SESSION_KEY_CHAIN, flags, secret, iSecretType);
		}

		private void setSecret(uint flags, string sSecretID, string sEPPassword)
		{
			//setSecret(flags, SESSION_KEY_CHAIN, sSecretID, sEPPassword);
		}

		public void removeSecret(uint ssFlags, string sKeyChainID, string sEPPassword, string sSecretID, int iSecretType)
		{
			m_NativeCalls.RemoveSecret(m_hsc, ssFlags, sKeyChainID, sEPPassword, sSecretID, iSecretType);
		}

		public void removeSecret(string sSecretID, int iSecretType)
		{
			removeSecret(0, SESSION_KEY_CHAIN, "", sSecretID, iSecretType);
		}


		private void enumerateKeychainIDs(string sSearchSting)
		{
			if (m_NativeCalls != null)
			{
				//m_NativeCalls.enumKeychainIDs();
			}
		}


		public void setMasterPassword(string sMasterPassword)
		{
			m_sMasterPassword = sMasterPassword;
		}

		public StringCollection enumerateSecretIDs()
		{
			return enumerateSecretIDs(SESSION_KEY_CHAIN);
		}


		public StringCollection enumerateSecretIDs(string sKeyChainID)
		{			
			return m_NativeCalls.EnumerateSecretIDs(m_hsc, 0, sKeyChainID, "");
		}
		public void addKeyChain(string sKeyChainID)
		{
			m_NativeCalls.AddKeyChain(m_hsc, 0, sKeyChainID);
		}

	}
}