
var ActionButton = function (button, animation, repetitions) {
	this.button = button;
	this.animation = animation;
	this.repetitions = repetitions;
};

function init() {

	// Get A-Frame Assets
	var sceneEl = document.querySelector('a-scene');
	let penModel = sceneEl.querySelector('#pen-model');

	// Get buttons
	let idleLyingBtn = new ActionButton(document.getElementById("idleLyingBtn"), "IdleLying", 1);
	let standUpBtn = new ActionButton(document.getElementById("standUpBtn"), "StandUp", 1);
	let shakeBtn = new ActionButton(document.getElementById("shakeBtn"), "Shake", Infinity);
	let idleStandingBtn = new ActionButton(document.getElementById("idleStandingBtn"), "IdleStanding", 1);
	let unlockBtn = new ActionButton(document.getElementById("unlockBtn"), "Unlock", 1);
	let idleStandingUnlockedBtn = new ActionButton(document.getElementById("idleStandingUnlockedBtn"), "IdleStandingUnlocked", 1);
	let unscrewBtn = new ActionButton(document.getElementById("unscrewBtn"), "Unscrew", 1);
	let idleUnscrewedBtn = new ActionButton(document.getElementById("idleUnscrewedBtn"), "IdleUnscrewed", 1);
	let injectFlipBtn = new ActionButton(document.getElementById("injectFlipBtn"), "InjectFlip", 1);
	let injectBtn = new ActionButton(document.getElementById("injectBtn"), "Inject", 1);
	let liftBtn = new ActionButton(document.getElementById("liftBtn"), "Lift", 1);
	let unexplodedBtn = new ActionButton(document.getElementById("unexplodedBtn"), "Unexploded", 1);
	let explodeBtn = new ActionButton(document.getElementById("explodeBtn"), "Explode", 1);
	let explodedBtn = new ActionButton(document.getElementById("explodedBtn"), "Exploded", 1);
	let implodeBtn = new ActionButton(document.getElementById("implodeBtn"), "Implode", 1);

	var actionButtons = [idleLyingBtn, standUpBtn, shakeBtn, idleStandingBtn, unlockBtn, idleStandingUnlockedBtn, unscrewBtn, idleUnscrewedBtn, injectFlipBtn, injectBtn, liftBtn, unexplodedBtn, explodeBtn, explodedBtn, implodeBtn];

	let currentSelected;

	actionButtons.forEach(actionButton => {
		actionButton.button.addEventListener('click', function(e){
			penModel.removeAttribute('animation-mixer');
			penModel.setAttribute('animation-mixer', {
				clip: actionButton.animation,
				loop: 'repeat',
				repetitions: actionButton.repetitions,
				clampWhenFinished: true,
				crossFadeDuration: .4
			});
			currentSelected?.classList.remove("selected");
			currentSelected = actionButton.button;
			currentSelected.classList.add("selected");
		});
	});

	penModel.setAttribute('animation-mixer', {
		clip: actionButtons[0].animation,
		loop: 'repeat',
		repetitions: actionButtons[0].repetitions,
		clampWhenFinished: true,
		crossFadeDuration: .4
	});
	actionButtons[0].button.classList.add("selected");
	currentSelected = actionButtons[0].button;
	
}

window.onload = function() { setTimeout(init(), 1000); };