

#include "common.js"


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

var assetArray = new Array(
	'Znicte palebne prostredky nepritele',
	'Provedte utok na letadlovou lod',
	'Vypustte jaderne strely'
);

var passwordMap = new HashMap();
passwordMap.put('petr', 'kocka');
passwordMap.put('pavel', '1234');
passwordMap.put('franta', 'rock');

var rng = new SecureRandom();
var CHALLENGE_SIZE = 16;

var requestedAsset;
var requestedLam;
var userName;
var challengeSent;


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

		case MESSAGE_TYPE_SPECIFY:
			readSelection (transaction, receivedMessage);
			break;
					
		case MESSAGE_TYPE_RESPONSE:
			processResponse (transaction, receivedMessage);
			break;

		default:
			server.destroyTransaction (transaction);
			break;
	}
}

function readSelection (transaction, receivedMessage) {
	var avpAsset = receivedMessage.findAvpWithType (AVP_ASSET_L);

	if (avpAsset != null) {
		var assetValue = avpAsset.getValue();

		if (assetValue.length == 1)
			requestedAsset = assetValue[0];
	}

	var avpLam = receivedMessage.findAvpWithType (AVP_LAM);

	if (avpLam != null) {
		var lamValue = avpLam.getValue();

		if (lamValue.length == 1) {
			requestedLam = lamValue[0];
		}
	}

	if (requestedAsset == null) {
		offerAsset(transaction);
	}
	else if (requestedLam == null) {
		offerLam(transaction);
	}
	else {
		sendRequest(transaction);
	}
}

function sendRequest(transaction) {
	challengeSent = newByteArray (CHALLENGE_SIZE);
	rng.nextBytes (challengeSent);
	
	var avpChallenge = server.createAvp();
	avpChallenge.setType (AVP_MESS_CHAL);
	avpChallenge.setValue (challengeSent);
	
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_REQUEST);
	messageToSend.addAvp (avpChallenge);
	
	transaction.sendMessage (messageToSend);
}

function offerAsset(transaction) {
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_OFFER);

	var index = 0;

	for (var i = 0; i < assetArray.length; i++) {
		// ASSET_L
		var avpAssetL = server.createAvp();
		avpAssetL.setType (AVP_ASSET_L);
		avpAssetL.setValue (byteToArray (i));

		messageToSend.addAvp (avpAssetL);
	}

	transaction.sendMessage (messageToSend);
}

function offerLam(transaction) {
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_OFFER);

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

	messageToSend.addAvp (avpLam);

	transaction.sendMessage (messageToSend);
}


function processStart (transaction, receivedMessage) {
	// Process received message
	var avpUser = receivedMessage.findAvpWithType (AVP_NAME_SUP_G);
	userName = bytesToString(avpUser.getValue());

	// Send message
	readSelection(transaction, receivedMessage);
}

function processResponse (transaction, receivedMessage) {
	// Process received message
	var avpResponse = receivedMessage.findAvpWithType (AVP_MESS_RES);
	var response = avpResponse.getValue();
	
	var password = '' + passwordMap.get (userName);
	var expectedResponse = getPasswordResponse (password, challengeSent);
	
	// Send message
	var messageToSend = server.createMessage();
	messageToSend.setMessageType (MESSAGE_TYPE_FINISH);

	var avpResult = server.createAvp();
	avpResult.setType (AVP_RESULT);

	if (Arrays.equals (response, expectedResponse)) {
		var asset = '' + assetArray[requestedAsset];
		var avpAssetContent = server.createAvp();
	
		avpAssetContent.setType (AVP_ASSET_CONTENT);
		avpAssetContent.setValue (stringToBytes (asset));

		messageToSend.addAvp (avpAssetContent);

		avpResult.setValue (byteToArray (RESULT_OK));
	}
	else {
		avpResult.setValue (byteToArray (RESULT_ERROR));
	}

	messageToSend.addAvp (avpResult);
	transaction.sendMessage (messageToSend);
}

