var tabData = {};


var MCMS = {};
MCMS.questionData = {};
MCMS.optionArr = [];
MCMS.mainContainer;
MCMS.maxOptions = 20;
MCMS.prevOption;
MCMS.userAnswer;
MCMS.correctAnswer;
MCMS.currentAttempt;
MCMS.activityCompleted = false;
MCMS.currentGroup;
MCMS.group;

var eventMgr = new EventManager();
var _shell_wrapper = $('#shell_wrapper');
var _checkResult = null;


MCMS.initMCMS = function (data) {
	MCMS.mainContainer = $("#mcmsContainer").find("#mcmsHolder");
	MCMS.questionData = data;
	MCMS.currentAttempt = 0;
	MCMS.currentGroup = 0;
	MCMS.group = null;
	//initialize Feedback
	Feedback.init($("#mcmsFeedbackBox"), $("#mcmsContainer"));

	eventMgr.removeControlEventListener(Feedback, "feedbackClosed", MCMS.closeFeedback);
	eventMgr.addControlEventListener(Feedback, "feedbackClosed", MCMS.closeFeedback);
	//initialize submit button

	SubmitButton.init($("#mcmsHolder").find("#mcmsSubmitBtn"));
	SubmitButton.chkSubmitEnable(false);
	$("#mcmsContinueBtn").addClass("disabled")

	eventMgr.removeControlEventListener(SubmitButton, "submitClicked", MCMS.submitClicked);
	eventMgr.addControlEventListener(SubmitButton, "submitClicked", MCMS.submitClicked);
	
	MCMS.createOptions();
	MCMS.disableOptions();
	//MCMS.addListeners();
	MCMS.showQuestion();
	MCMS.mainContainer.find("#mcmsNextBtn").hide();

	$("#mcmsHolder").addClass(data.id);

	var _shell_wrapper = $('#shell_wrapper');

	

	$(".popup").addClass('iGPopup')
	
	$(".popup").focus();

	setTimeout(function () {
		$("#mcmsHolder").focus();
	}, 1000)

}

MCMS.createOptions = function () {

	MCMS.userAnswer = [];

	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		MCMS.userAnswer[c] = []
		MCMS.optionArr.push([])

		var type = '';

		(MCMS.questionData.optionsGroup[c].correctAnswer.length == 1) ? (type = "mcss") : (type = "mcms");

		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			
			var option = $(MCMS.mainContainer.find("#mcmsOptionContainer").find(".cloneOption")).clone();

			option.removeClass("cloneOption");

			if (type == "mcss") {
				option.find('#optionBox').addClass('radio').removeClass('checkBox');
			} else {
				option.find('#optionBox').addClass('checkBox').removeClass('radio');
			}

			MCMS.userAnswer[c][i] = 0;

			var opt = new Option(option, type, i);
			MCMS.optionArr[c][i] = opt;

			eventMgr.addControlEventListener(opt.holder, "optionClicked", MCMS.mcmsOptionClicked);

			MCMS.mainContainer.find(".group" + (c + 1)).append(opt.holder)
			option.appendTo(MCMS.mainContainer.find(".group" + (c + 1)));

		}
	}
};

MCMS.addListeners = function () {
	 
	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			 	var option = MCMS.optionArr[c][i];
			option.addListeners();
		}
	}
};

MCMS.showQuestion = function () {
	MCMS.initializeMCMS();
};

/*MCMS functionality*/
MCMS.initializeMCMS = function () {

	MCMS.userAnswer = [];
	MCMS.correctAnswer = [];

	MCMS.activityCompleted = false;
	$("#mcmsContainer").find("#mcmsQuestionTxt").html(MCMS.questionData.question);

	$("#mcmsContainer").find("#mcmsScenarioTxt").html(MCMS.questionData.scenarioText);

	$("#mcmsContainer").find("#mcmsInstructionTxt").html(MCMS.questionData.instructionText);

	$("#mcmsContainer").find(".mcssOption").addClass("hiddenOption");

	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		MCMS.userAnswer[c] = [];
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			MCMS.userAnswer[c][i] = 0;
			MCMS.correctAnswer[c] = MCMS.questionData.optionsGroup[c].correctAnswer;
			var option = MCMS.optionArr[c][i];
			option.showOption("<span>" + MCMS.questionData.optionsGroup[c].group[i] + "</span>");
		}
	}

}

MCMS.resetMCMS = function () {
	MCMS.userAnswer = [];
	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		MCMS.userAnswer[c] = [];		
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			MCMS.userAnswer[c][i] = 0;
			var option = MCMS.optionArr[c][i];
			option.reset();
		}
	}
}

MCMS.mcmsOptionClicked = function (evt) {
	
	var clicked = $(evt.target).parent().parent();

	var str = clicked.attr("id");
	let _oNo = parseInt(str.split("_")[1]);

	MCMS.currentGroup = parseInt(clicked.parent().attr("class").slice(5, 6));
	let _userAns = MCMS.userAnswer[MCMS.currentGroup - 1];

	if (MCMS.questionData.optionsGroup[MCMS.currentGroup - 1].correctAnswer.length == 1) {
		if (clicked.find("#optionBox").hasClass("MCSSbulletPointsSelected")) {
			$(this).parents(".mcssOption").addClass("selected");
			if (MCMS.prevOption && MCMS.group == MCMS.currentGroup) {
				MCMS.prevOption.find("#optionBox").removeClass("MCSSbulletPointsSelected");
				$(this).parents(".mcssOption").removeClass("selected");
			}
			MCMS.group = MCMS.currentGroup;
			MCMS.prevOption = clicked;
			_userAns = [];
			_userAns.push(_oNo);
		} else {
			_userAns.splice(jQuery.inArray(str.split("_")[1], _userAns), 1);
		}
	} else {
		if (clicked.find("#optionBox").hasClass("MCMSbulletPointsSelected")) {
			_userAns[_oNo - 1] = 1;
		} else {
			_userAns[_oNo - 1] = 0;
		}
	}
 
	let _isActive = false; 
	let _count = 0;
	for (let i = 0; i < _userAns.length; i++){
		if (_userAns[i]==1) {
			_count++
		}
	}

	if (_count >= 1) {
		_isActive = true;
	}

	SubmitButton.chkSubmitEnable(_isActive);

}

MCMS.submitClicked = function (evt) {

	if (MCMS.userAnswer.length > 0 && !SubmitButton.holder.eq(0).hasClass('disabled')) {
		MCMS.disableOptions();
		MCMS.chkUserAnswer();

		$(".feedBackContent").show();
		//$(".feedBackPopup").attr('style', 'display: block !important');
		$(".feedBackPopup").toggleClass("slide-right");
		setTimeout(function () {
			
			$(".full_box").focus();
			$(".full_box").focusout(function () {
				$(".feedBackContent .close_icon").focus();
			})
		}, 500)
	}
};

MCMS.disableOptions = function () {

	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			var option = MCMS.optionArr[c][i];
			option.disable();
		}
	}
}



MCMS.chkUserAnswer = function () {

	let _obj = {
		'oNo': MCMS.userAnswer,
		'rP': []
	};
 
	let result = 'Correct';
	for (i = 0; i < MCMS.userAnswer.length; i++) {
		for (var c = 0; c < MCMS.userAnswer[i].length; c++) {
			if (MCMS.userAnswer[i][c] === MCMS.correctAnswer[i][c]) { 
				_obj.rP[c] = MCMS.correctAnswer[i][c];
			} else {
				_obj.rP[c] = 0; 
				result = "Incorrect";
			}			
		} 
	}

	//console.log("result " + result);
	_checkResult = result; 
	MCMS.showFeedback(result, _obj);
};


MCMS.showFeedback = function (result, _obj) {

	MCMS.currentAttempt++;
	$(".feedBackContent").show().css("display", "block !important")

	if (MCMS.questionData.responseType == "generic") {
		if (result == "Correct") {
			Feedback.showFeedback(MCMS.questionData.feedback.genericResponse.correct.content);
			MCMS.checkUserAns();
		} else {
			if (MCMS.currentAttempt < MCMS.questionData.maxAttempts) {
				Feedback.showFeedback(MCMS.questionData.feedback.genericResponse.incorrect.content);
			} else {
				Feedback.showFeedback(MCMS.questionData.feedback.genericResponse.solution.content);
				MCMS.checkUserAns();
			}
		}
	} else {
		var feedStr = "";
		for (var c = 0; c < MCMS.userAnswer.length; c++) {
			feedStr += MCMS.questionData.specificResponse.response[MCMS.userAnswer[c] - 1].content;
			if (c > 0)
				feedStr += "<br>"
		}

		if (result == "Correct") {
			MCMS.showTickOnly();
		} else {
			if (MCMS.currentAttempt >= MCMS.questionData.maxAttempts)
				MCMS.showTickOnly();
		}
	}

	$("#mcmsFeedbackBox").draggable({
		disabled: true
	});

	$("#feedbackHeader").css('cursor', 'default');

	_shell_wrapper.trigger('activityResult', {
		'result': result,
		'attemptNo': MCMS.currentAttempt,
		'userAnswer': _obj,
		
	});

};

MCMS.closeFeedback = function () {
	_shell_wrapper.trigger('closeActivity');
	$(".feedBackContent").hide();

};

MCMS.showTickOnly = function () {
	MCMS.showSolution();
}

MCMS.checkUserAns = function () {
	MCMS.markPageComplete();
	$(".mcssOption").addClass("eventNone");
	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			var option = MCMS.optionArr[c][i];
			// if (MCMS.correctAnswer[c][i]==1) { 
			if (MCMS.userAnswer[c][i] == 1) {
				if (MCMS.correctAnswer[c][i] == 1) {
					option.showTickCross("mcssTick");
				} else {
					option.showTickCross("mcssCross");
				}
			}
		}
	}
}

MCMS.showSolution = function () {
	
	MCMS.markPageComplete();
	$(".mcssOption").addClass("eventNone");
	for (c = 0; c < MCMS.questionData.optionsGroup.length; c++) {
		for (var i = 0; i < MCMS.questionData.optionsGroup[c].group.length; i++) {
			var option = MCMS.optionArr[c][i];
			if (MCMS.correctAnswer[c][i]==1) { 
				option.showTickCross("mcssTick");
			}else{ 
				option.showTickCross("mcssCross");
			}				
		}
	}
}


MCMS.markPageComplete = function () {
	if (!MCMS.activityCompleted) {
		MCMS.activityCompleted = true;
		eventMgr.dispatchCustomEvent(MCMS, "templateActivityCompleted", "", "");
	}
}

//randomization
MCMS.randomizeArr = function (arr) {
	var randomizeArr = arr.concat();
	randomizeArr.sort(MCMS.shuffle);
	return randomizeArr;
};

MCMS.shuffle = function () {
	var ran = 0.5 - Math.random();
	return ran;
};

MCMS.chkActivityCompletion = function () {}




var init_activity = (function () {
	this.init = function (data, callback) {
		_aData = data;

		eventMgr.addControlEventListener(MCMS, "templateActivityCompleted", MCMS.chkActivityCompletion);


		MCMS.initMCMS(_aData);

		_shell_wrapper.off("startEvents_3").on("startEvents_3", function () {
			MCMS.addListeners();
		});

		// setTimeout(function () {
		// 	MCMS.addListeners();
		// }, 500)

		markcompletepage = $('#shell_wrapper');
		markcompletepage.off("markcompletepage").on("markcompletepage", function (e) {
			markcompletepage.off("markcompletepage");
			if (callback) {
				callback();
			}

		})
	};
	return this;

})