#include "com_novell_casa_MiCasa.h"

#include <jni.h>
#include <micasa.h>
#include <sscs_utf8.h>

#ifdef __cplusplus
extern "C" {
#endif


JNIEXPORT jint JNICALL 
Java_com_novell_casa_MiCasa_jmiCASASetCredential
  (JNIEnv *env, jobject notused, 
  jint			ssFlags, 
  jstring		jsAppSecretID, 
  jstring		jsSharedSecretID, 
  jint			jUsernameType, 
  jstring		jsUsername, 
  jstring		jsPassword
  )
{	
	int rcode = 0;	
	SSCS_SECRET_ID_T		appSecretId = {0};
	SSCS_SECRET_ID_T		sharedSecretId = {0};
	SSCS_BASIC_CREDENTIAL	credential = {0};

	const char* utf_string;
	jboolean isCopy;	
	utf_string = (*env)->GetStringUTFChars(env, jsAppSecretID, &isCopy);
			
	sscs_Utf8Strcpy((char*)appSecretId.id, utf_string);
	appSecretId.len = sscs_Utf8Strlen(utf_string) + 1;	

	if (isCopy == JNI_TRUE)
		(*env)->ReleaseStringUTFChars(env,jsAppSecretID, utf_string);

	// handle sharedsecretid
	if (jsSharedSecretID)
	{
		utf_string = (*env)->GetStringUTFChars(env, jsSharedSecretID, &isCopy);
		sscs_Utf8Strcpy((char*)sharedSecretId.id, utf_string);
		sharedSecretId.len = sscs_Utf8Strlen(utf_string)+1;

		if (isCopy == JNI_TRUE)
			(*env)->ReleaseStringUTFChars(env, jsSharedSecretID, utf_string);
	}

	// setup credential
	utf_string = (*env)->GetStringUTFChars(env, jsUsername, &isCopy);
	sscs_Utf8Strcpy((char*)credential.username, utf_string);
	credential.unLen = sscs_Utf8Strlen(utf_string)+1;
	credential.unFlags = jUsernameType;
	if (isCopy == JNI_TRUE)
		(*env)->ReleaseStringUTFChars(env, jsUsername, utf_string);

	// password	
	utf_string = (*env)->GetStringUTFChars(env, jsPassword, &isCopy);
	sscs_Utf8Strcpy((char*)credential.password, utf_string);
	credential.pwordLen = sscs_Utf8Strlen(utf_string)+1;	
	if (isCopy == JNI_TRUE)
		(*env)->ReleaseStringUTFChars(env, jsPassword, utf_string);
	

	// make the call	
	if (jsSharedSecretID)
		rcode = miCASASetCredential(            
				ssFlags,
				&appSecretId,		
				&sharedSecretId, 	
				jUsernameType,
				&credential,		
				NULL
				);
	else		
		rcode = miCASASetCredential(            
			ssFlags,
			&appSecretId,		
			NULL,
			jUsernameType,	
			&credential,		
			NULL
		);
	
	return rcode;
}

/*
 * Class:     com_novell_casa_micasa_SecretStore
 * Method:    jmiCASAGetCredential
 * Signature: (I[B[BILcom/novell/casa/micasa/Secret;)I
 */
JNIEXPORT jint JNICALL 
Java_com_novell_casa_MiCasa_jmiCASAGetCredential
  (JNIEnv *env, jobject unused, 
  jint			ssFlags, 
  jstring		jsAppSecretID, 
  jstring		jsSharedSecretID, 
  jint			jUsernameType,
  jobject		jcred)
{
	int rcode = 0;	
	SSCS_SECRET_ID_T		appSecretId = {0};
	SSCS_SECRET_ID_T		sharedSecretId = {0};
	SSCS_BASIC_CREDENTIAL	credential = {0};

	const char* utf_string;
	jboolean isCopy;
	int unType = jUsernameType;

	utf_string = (*env)->GetStringUTFChars(env, jsAppSecretID, &isCopy);
	sscs_Utf8Strcpy((char*)appSecretId.id, utf_string);
	appSecretId.len = sscs_Utf8Strlen(utf_string) + 1;
	if (isCopy == JNI_TRUE)
		(*env)->ReleaseStringUTFChars(env, jsAppSecretID, utf_string);

	// handle sharedsecretid
	if (jsSharedSecretID)
	{
		utf_string = (*env)->GetStringUTFChars(env, jsSharedSecretID, &isCopy);
		sscs_Utf8Strcpy((char*)sharedSecretId.id, utf_string);
		sharedSecretId.len = sscs_Utf8Strlen(utf_string)+1;

		if (isCopy == JNI_TRUE)
			(*env)->ReleaseStringUTFChars(env, jsSharedSecretID, utf_string);
	}

	// make the call	
	if (jsSharedSecretID)
		rcode = miCASAGetCredential(            
				ssFlags,
				&appSecretId,		
				&sharedSecretId, 	
				&unType,					
				&credential,		
				NULL
				);
	else		
		rcode = miCASAGetCredential(            
			ssFlags,
			&appSecretId,		
			NULL,
			&unType,
			&credential,		
			NULL
		);
	

	if (rcode == 0)
	{
		// we have a credential
		jclass theclazz;
		jmethodID mid; 

		theclazz = (*env)->GetObjectClass(env, jcred);
		mid = (*env)->GetMethodID(env, theclazz, "setUsername", "(Ljava/lang/String;)V");
		if (mid != NULL)
		{
			//printf("The Username: %s\r\n", credential.username);
			jstring jusername = (*env)->NewStringUTF(env, (char *)credential.username);
			(*env)->CallObjectMethod(env, jcred, mid, jusername);
		}

		mid = (*env)->GetMethodID(env, theclazz, "setPassword", "(Ljava/lang/String;)V");
		{
			jstring jpassword = (*env)->NewStringUTF(env, (char *)credential.password);
			(*env)->CallObjectMethod(env, jcred, mid, jpassword);
		}
	}
	return rcode;
}
  
 
/*
 * Class:     com_novell_casa_micasa_SecretStore
 * Method:    jmiCASARemoveCredential
 * Signature: (I[B[B)I
 */
JNIEXPORT jint JNICALL 
Java_com_novell_casa_MiCasa_jmiCASARemoveCredential
  (JNIEnv *env, jobject unused, 
  jint ssFlags, 
  jstring jsAppSecretID, 
  jstring jsSharedSecretID)
{
	int rcode = 0;	
	SSCS_SECRET_ID_T		appSecretId = {0};
	SSCS_SECRET_ID_T		sharedSecretId = {0};
	
	const char* utf_string;
	jboolean isCopy;

	utf_string = (*env)->GetStringUTFChars(env, jsAppSecretID, &isCopy);
	sscs_Utf8Strcpy((char*)appSecretId.id, utf_string);
	appSecretId.len = sscs_Utf8Strlen(utf_string) + 1;
	if (isCopy == JNI_TRUE)
		(*env)->ReleaseStringUTFChars(env, jsAppSecretID, utf_string);

	// handle sharedsecretid
	if (jsSharedSecretID)
	{
		utf_string = (*env)->GetStringUTFChars(env, jsSharedSecretID, &isCopy);
		sscs_Utf8Strcpy((char*)sharedSecretId.id, utf_string);
		sharedSecretId.len = sscs_Utf8Strlen(utf_string)+1;

		if (isCopy == JNI_TRUE)
			(*env)->ReleaseStringUTFChars(env, jsSharedSecretID, utf_string);
	}

	// make the call	
	if (jsSharedSecretID)
		rcode = miCASARemoveCredential(            
				ssFlags,
				&appSecretId,		
				&sharedSecretId,
				NULL);

	else		
		rcode = miCASARemoveCredential(            
			ssFlags,
			&appSecretId,		
			NULL,
			NULL);

	return rcode;
}


#ifdef __cplusplus
}
#endif