

#include "common.js"

importPackage(javax.swing);
importPackage(java.security);


var userName;
var password;
var asset;
var scenario;


function request(userName_, password_, asset_, scenario_) {
	userName = userName_;
	password = password_;
	asset = asset_;
	scenario = scenario_;
	
	transaction = server.createTransaction();

	message = server.createMessage();
	message.setMessageType (MESSAGE_TYPE_START);
	
	if (scenario == 1) {
		var avpAsset = server.createAvp();
		avpAsset.setType (AVP_ASSET_L);
		avpAsset.setValue (byteToArray (asset));

		message.addAvp (avpAsset);

		var avpLam = server.createAvp();
		avpLam.setType (AVP_LAM);
		avpLam.setValue (byteToArray (LAM_PASSWORD_CHALLENGE));

		message.addAvp (avpLam);
	}
	
	var avpUserName = server.createAvp();
	avpUserName.setType (AVP_NAME_SUP_G);
	avpUserName.setValue (stringToBytes (userName));

	message.addAvp (avpUserName);
	
	transaction.sendMessage(message);
}

function onMessage (transaction, receivedMessage) {
	var messageType = receivedMessage.getMessageType();
	
	switch (messageType) {
		case MESSAGE_TYPE_REQUEST:
			processRequest (transaction, receivedMessage);
			break;

		case MESSAGE_TYPE_OFFER:
			processOffer (transaction, receivedMessage);
			break;
			
		case MESSAGE_TYPE_FINISH:
			processFinish (transaction, receivedMessage);
			break;
		
		default:
			server.destroyTransaction (transaction);
			break;
	}
}

function processRequest (transaction, receivedMessage) {
	// Process received message
	var avpChallenge = receivedMessage.findAvpWithType (AVP_MESS_CHAL);
	var challenge = avpChallenge.getValue();
	
	var response = getPasswordResponse (password, challenge);

	// Send message
	var responseAvp = server.createAvp();
	
	responseAvp.setType (AVP_MESS_RES);
	responseAvp.setValue (response);
	
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_RESPONSE);
	messageToSend.addAvp (responseAvp);
	
	transaction.sendMessage (messageToSend);
}

function processOffer (transaction, receivedMessage) {
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_SPECIFY);

	var receivedAvpLam = receivedMessage.findAvpWithType (AVP_LAM);

	if (receivedAvpLam != null) {
		var avpLamToSend = server.createAvp();
	
		avpLamToSend.setType (AVP_LAM);
		avpLamToSend.setValue (receivedAvpLam.getValue());

		messageToSend.addAvp (avpLamToSend);
	}

	var receivedAvpAssetL = receivedMessage.findAvpWithType (AVP_ASSET_L);

	if (receivedAvpAssetL != null) {
		var avpAssetLToSend = server.createAvp();
	
		avpAssetLToSend.setType (AVP_ASSET_L);
		avpAssetLToSend.setValue (receivedAvpAssetL.getValue());

		messageToSend.addAvp (avpAssetLToSend);
	}

	transaction.sendMessage (messageToSend);
}

function processFinish (transaction, receivedMessage) {
	var avpResult = receivedMessage.findAvpWithType (AVP_RESULT);
	var result = avpResult.getValue();

	if (result.length == 1 && result[0] == RESULT_OK) {
		var avpAsset = receivedMessage.findAvpWithType (AVP_ASSET_CONTENT);
		var asset = bytesToString (avpAsset.getValue());
	
		JOptionPane.showMessageDialog(null, 'Received asset: ' + asset);
	}
	else {
		JOptionPane.showMessageDialog(null, 'Supplier returned error');
	}
}

function test1() {
	request ('petr', 'kocka', 2, 1);
}

function test2() {
	request ('petr', 'kocka', null, 2);
}

