let tabData = {};

let MCMS_MCSS_13 = {};
MCMS_MCSS_13.questionData = {};
MCMS_MCSS_13.optionArr = [];
MCMS_MCSS_13.mainContainer;
MCMS_MCSS_13.maxOptions = 4;
MCMS_MCSS_13.prevOption;
MCMS_MCSS_13.userAnswer;
MCMS_MCSS_13.correctAnswer;
MCMS_MCSS_13.currentAttempt;
MCMS_MCSS_13.activityCompleted = false;
MCMS_MCSS_13.currentGroup;
MCMS_MCSS_13.group;
var result_response;

let eventMgr = new EventManager();
let _shell_wrapper = $('#shell_wrapper');


MCMS_MCSS_13.initMCMS = function (data) {

	MCMS_MCSS_13.mainContainer = $("#mcmsContainer").find("#mcmsHolder");

	MCMS_MCSS_13.questionData = data;
	MCMS_MCSS_13.currentAttempt = 0;
	MCMS_MCSS_13.currentGroup = 0;
	MCMS_MCSS_13.group = null;

	Feedback.init($("#mcmsFeedbackBox"), $("#mcmsContainer"));

	eventMgr.removeControlEventListener(Feedback, "feedbackClosed", MCMS_MCSS_13.closeFeedback);
	eventMgr.addControlEventListener(Feedback, "feedbackClosed", MCMS_MCSS_13.closeFeedback);

	//initialize submit button

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

	$("#mcmsContinueBtn").addClass("disabled")
	$("#feedbackClose").addClass("disabled").addClass("d-hide");

	MCMS_MCSS_13.createOptions();
	MCMS_MCSS_13.disableOptions();

	MCMS_MCSS_13.showQuestion();
	MCMS_MCSS_13.mainContainer.find("#mcmsNextBtn").hide();


	$("#mcmsHolder").addClass(data.id);
	$(".popup").addClass('iGPopup');

	setTimeout(function () {
		eventMgr.removeControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS_13.submitClicked);
		eventMgr.addControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS_13.submitClicked);
		MCMS_MCSS_13.addTabIndexingOnQuestion();
	}, 300);
	$("#btn_cc").addClass("CC_disabled");
}


MCMS_MCSS_13.hintBox = function () {

	let _policyText = MCMS_MCSS_13.questionData.policyText;
	let _clueText = MCMS_MCSS_13.questionData.clueText;

	if (_policyText && _policyText != '') {
		$("#policyIcon").removeClass('d-hide');
	}

	if (_clueText && _clueText != '') {
		$("#clueIcon").removeClass('d-hide');
	}

	let _hint_btns = $(".hint_btn");
	_hint_btns.off("click").removeClass("active").addClass("disabled");

}


MCMS_MCSS_13.addEventOnHint = function () {

	let _hint_btns = $(".hint_btn");

	_hint_btns.off("click").on("click", function () {

		let _btn = $(this);
		if (_btn.hasClass("active")) {


			let _text = MCMS_MCSS_13.questionData[_btn.attr('data-text')];
			$("#hintContent").html(_text);

			$("#hintHeader").html(_btn.text());
			$("#hintIcon").removeAttr('class').addClass(_btn.attr('id'));

			let _holder = $("#hintBoxPopup")
			_holder.removeClass('d-hide').addClass("fadeInUpAnim");

			let _holderCover = $("#hintCover")
			_holderCover.addClass('d-show');

			let _close_btn = _holder.find("#hintBoxClose")
			_close_btn.unbind();
			_close_btn.off("click").on("click", function () {
				_holder.addClass('d-hide').removeClass("fadeInUpAnim");
				_holderCover.removeClass('d-show');
			}).addClass("active");

		}
	}).removeClass("disabled").addClass("active")
}


MCMS_MCSS_13.createOptions = function () {

	MCMS_MCSS_13.userAnswer = [];
	let type = MCMS_MCSS_13.questionData.type;

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

		for (let i = 0; i < MCMS_MCSS_13.questionData.optionsGroup[c].group.length; i++) {

			let option = $(MCMS_MCSS_13.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_MCSS_13.userAnswer[c][i] = 0;

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

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

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

		}
	}
};

MCMS_MCSS_13.addListeners = function () {

	for (let c = 0; c < MCMS_MCSS_13.questionData.optionsGroup.length; c++) {
		for (let i = 0; i < MCMS_MCSS_13.questionData.optionsGroup[c].group.length; i++) {
			let option = MCMS_MCSS_13.optionArr[c][i];
			option.addListeners();
		}
	}
	// MCMS_MCSS_13.addEventOnHint();
};

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

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

	MCMS_MCSS_13.userAnswer = [];
	MCMS_MCSS_13.correctAnswer = [];

	MCMS_MCSS_13.activityCompleted = false;

	let _mcmsContainer = $("#mcmsContainer");

	$(".mcmsQuestionTxt").html(MCMS_MCSS_13.questionData.questionText);
	$(".qBoxImg").attr("alt", MCMS_MCSS_13.questionData.qBoxImg);
	$(".char-img").attr("alt", MCMS_MCSS_13.questionData.charImg);
	// $(".questionID").html(MCMS_MCSS_13.questionData.questionID);

	//_mcmsContainer.find("#mcmsScenarioTxt").html(MCMS_MCSS_13.questionData.scenarioText);

	_mcmsContainer.find("#mcmsInstructionTxt").html(MCMS_MCSS_13.questionData.instructionText);

	_mcmsContainer.find(".mcssOption").addClass("hiddenOption");

	for (let c = 0; c < MCMS_MCSS_13.questionData.optionsGroup.length; c++) {
		MCMS_MCSS_13.userAnswer[c] = [];
		for (let i = 0; i < MCMS_MCSS_13.questionData.optionsGroup[c].group.length; i++) {
			MCMS_MCSS_13.userAnswer[c][i] = 0;
			MCMS_MCSS_13.correctAnswer[c] = MCMS_MCSS_13.questionData.optionsGroup[c].correctAnswer;
			let option = MCMS_MCSS_13.optionArr[c][i];
			option.showOption("" + MCMS_MCSS_13.questionData.optionsGroup[c].group[i] + "");
		}
	}

	$(".hiddenOption").remove();

	let _isDebugging = false;

	try {
		if (_user_details) {
			_isDebugging = _user_details['isDebugging'] || false;
		}
	} catch (err) {

	}

	try {
		if (_isDebugging) {
			let __qTxt = MCMS_MCSS_13.questionData.id + ' - ' + MCMS_MCSS_13.questionData.questionText + ' - ' + MCMS_MCSS_13.correctAnswer;
			_mcmsContainer.find("#mcmsQuestionTxt").html(__qTxt);
		}
	} catch (err) {

	}

	$(".activityHolder").removeClass("d-hide")

}

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


MCMS_MCSS_13.mcmsOptionClicked = function (evt) {

	let clicked = $(evt.target).parent().parent().parent();

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

	MCMS_MCSS_13.currentGroup = parseInt(clicked.parent().attr("class").slice(5, 6));

	let _optionbox = clicked.find("#optionBox");
	if (MCMS_MCSS_13.questionData.type === "mcss") {
		MCMS_MCSS_13.resetMCMS();
		_optionbox.addClass("MCSSbulletPointsSelected");
		_optionbox.parents(".mcssOption").addClass("selected")
		_optionbox.attr({ "aria-checked": "true" });
		MCMS_MCSS_13.userAnswer[MCMS_MCSS_13.currentGroup - 1][_oNo - 1] = 1;
	} else {
		if (_optionbox.hasClass("MCMSbulletPointsSelected")) {
			_optionbox.attr({ "aria-checked": "true" });
			MCMS_MCSS_13.userAnswer[MCMS_MCSS_13.currentGroup - 1][_oNo - 1] = 1;
		} else {
			_optionbox.attr({ "aria-checked": "false" });
			MCMS_MCSS_13.userAnswer[MCMS_MCSS_13.currentGroup - 1][_oNo - 1] = 0;
		}
	}



	let _count = 0;
	for (let i = 0; i < MCMS_MCSS_13.userAnswer[MCMS_MCSS_13.currentGroup - 1].length; i++) {
		if (MCMS_MCSS_13.userAnswer[MCMS_MCSS_13.currentGroup - 1][i] == 1) {
			_count++
		}
	}
	if (_count >= 1) {
		_isActive = true;
	}
	SubmitButton.chkSubmitEnable(_isActive);

}

MCMS_MCSS_13.submitClicked = function (evt) {
	if (MCMS_MCSS_13.userAnswer.length > 0 && !SubmitButton.holder.eq(0).hasClass('disabled')) {
		MCMS_MCSS_13.chkUserAnswer();
		MCMS_MCSS_13.disableOptions();
	}

	let _btn_prev = $(".btn_prev");
	enabledDisable(_btn_prev, false);
};

MCMS_MCSS_13.disableOptions = function () {
	for (let c = 0; c < MCMS_MCSS_13.questionData.optionsGroup.length; c++) {
		for (let i = 0; i < MCMS_MCSS_13.questionData.optionsGroup[c].group.length; i++) {
			let option = MCMS_MCSS_13.optionArr[c][i];
			option.disable();
		}
	}
}



MCMS_MCSS_13.chkUserAnswer = function () {

	SubmitButton.chkSubmitEnable(false);

	let result = 1;
	let uResult = {}
	let cCount = 0;
	let type = MCMS_MCSS_13.questionData.type;

	for (let i = 0; i < MCMS_MCSS_13.userAnswer.length; i++) {
		for (let c = 0; c < MCMS_MCSS_13.userAnswer[i].length; c++) {
			if ((MCMS_MCSS_13.userAnswer[i][c] === MCMS_MCSS_13.correctAnswer[i][c]) && (MCMS_MCSS_13.correctAnswer[i][c] == 1)) {
				uResult[i] = 1;
				cCount++;
			} else {
				uResult[i] = 0;
			}
			if (MCMS_MCSS_13.userAnswer[i][c] != MCMS_MCSS_13.correctAnswer[i][c]) {
				result = 0;
			}
		}
	}
	
	uResult['cCount'] = cCount;
	uResult['result'] = result;
	uResult['type'] = type;


	MCMS_MCSS_13.currentAttempt++;

	_shell_wrapper.trigger('activityResult_3', {
		'qNo': parseInt(MCMS_MCSS_13.questionData['id']),
		'attemptNo': MCMS_MCSS_13.currentAttempt,
		'cAns': MCMS_MCSS_13.correctAnswer[0],
		'uAns': MCMS_MCSS_13.userAnswer[0],
		'type': type,
		'result': result,
		'uResult': uResult
	});

	MCMS_MCSS_13.showFeedback(uResult);


};


MCMS_MCSS_13.showFeedback = function (uResult) {
	
	let result = uResult['result'] || 0;
	let cCount = uResult['cCount'] || 0;

	if (MCMS_MCSS_13.questionData.responseType !== "") {

		$(".feedBackContent").show();
		$("#hintCoverFeedback").addClass('d-show');
		$("#mcmsFeedbackBox").addClass("fadeInUpAnim");

		setTimeout(function () {
			$(".full_box").focus();
			$(".full_box").focusout(function () {
				$(".feedBackContent .close_icon").focus();
			})
		}, 500);

		$(".feedBackContent").show().css("display", "block !important")
		$("#mcmsFeedbackBox").show().css("display", "block !important")

		let _response = null;

		if (MCMS_MCSS_13.questionData.responseType == "genericResponse") {
			_response = null;
			let __response = MCMS_MCSS_13.questionData.genericResponse;

			let type = MCMS_MCSS_13.questionData.type;
			let isGroup = MCMS_MCSS_13.questionData.isGroup;

			if ((type == "mcss") || (isGroup == "security")) {
				if (result) {
					_response = __response.correct;
					result_response = "y";
				} else {
					if(MCMS_MCSS_13.currentAttempt == 1){
						_response = __response.partialCorrect;
						result_response = "n";
					}else{
						_response = __response.incorrect;
						result_response = "n";
					}
				}
			} else {
				if (result) {
					_response = __response.correct;
					result_response = "y";
				} else {
					if(MCMS_MCSS_13.currentAttempt == 1){
						_response = __response.partialCorrect;
						result_response = "n";
					}else{
						_response = __response.incorrect;
						result_response = "n";
					}
				}
			}

			_shell_wrapper.trigger('activityResult_3', {
				'finalresponse': result_response
			});
			
			Feedback.showFeedback(_response.description);

		} else if (MCMS_MCSS_13.questionData.responseType == "specificResponse") {

			let feedStr = "";
			let count = 0;
			_response = null;

			for (let c = 0; c < MCMS_MCSS_13.userAnswer[0].length; c++) {
				let i = MCMS_MCSS_13.userAnswer[0][c]
				if (i) {
					count++;
					if(MCMS_MCSS_13.currentAttempt == 1 && result == 0){
						_response = MCMS_MCSS_13.questionData.specificResponse.response[MCMS_MCSS_13.userAnswer[0].length];
					}else{
						_response = MCMS_MCSS_13.questionData.specificResponse.response[c];
					}
					result_response = _response.isCorrect;
					feedStr += _response.description;

					if (count > 0) {
						feedStr += ""
					}
				}
			}
			
			_shell_wrapper.trigger('activityResult_3', {
				'finalresponse': result_response
			});

			Feedback.showFeedback(feedStr);
		}

		if (result) {
			MCMS_MCSS_13.showTickOnly();
		} else {
			if (MCMS_MCSS_13.currentAttempt >= MCMS_MCSS_13.questionData.maxAttempts) {
				MCMS_MCSS_13.showTickOnly();
			}
		}

		if (_response && _response.audio) {

			MCMS_MCSS_13.updateAudio(_response.audio, function () {
				MCMS_MCSS_13.checkIsNextQuestion(result);
			})
		} else {
			MCMS_MCSS_13.checkIsNextQuestion(result);
		}


		setTimeout(function () {
			let isNextQuestion = MCMS_MCSS_13.questionData.isNextQuestion;
			if (isNextQuestion && !result) {
				MCMS_MCSS_13.addTabIndexingOnFeedback();
			} else {
				MCMS_MCSS_13.addTabIndexingOnFeedback_1();
			}

			_shell_wrapper.off("activityResult_3");
			_shell_wrapper.off("activityResult");
			// _shell_wrapper.off("closeActivity");
			Utils.pageCompleted();
		}, 600)

		$("#mcmsFeedbackBox").draggable({
			disabled: true
		});
		$("#feedbackHeader").css('cursor', 'default');

	}


};

MCMS_MCSS_13.checkIsNextQuestion = function (result) {
	let isNextQuestion = MCMS_MCSS_13.questionData.isNextQuestion;

	if (isNextQuestion && !result) {
		$("#feedbackClose").removeClass("disabled").removeClass("d-hide");
	} else {
		$("#feedbackClose").removeClass("disabled");
		Utils.pageCompleted();
	}
	//	if(isNextQuestion){
	$("#feedbackClose").removeClass("disabled").removeClass("d-hide");
	//	}
}

MCMS_MCSS_13.updateAudio = function (_aData, callback) {
	try {
		_aData['callback'] = function () {
			callback();
		}
		if (_simple_audio) {
			_simple_audio.changeAudio(_aData);
		}
	} catch (err) {

	}
}

MCMS_MCSS_13.closeFeedback = function () {
	$("#mcmsFeedbackBox").removeClass("fadeInUpAnim");
	$("#mcmsFeedbackBox").addClass("fadeInUpAnim");
	$("#hintCoverFeedback").removeClass('d-show');
	if (MCMS_MCSS_13.currentAttempt >= MCMS_MCSS_13.questionData.maxAttempts || result_response=="y") {
		_shell_wrapper.trigger('closeActivity');
		Utils.pageCompleted();
	} else {
		MCMS_MCSS_13.resetMCMS()
	}
	// Utils.pageCompleted();
};

MCMS_MCSS_13.showTickOnly = function () {
	MCMS_MCSS_13.showSolution();
	// MCMS_MCSS_13.checkUserAns();
}

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

MCMS_MCSS_13.showSolution = function () {

	// MCMS_MCSS_13.markPageComplete();

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


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

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

MCMS_MCSS_13.shuffle = function () {
	let ran = 0.5 - Math.random();
	return ran;
};

MCMS_MCSS_13.chkActivityCompletion = function () { }

MCMS_MCSS_13.addTabIndexingOnQuestion = function () {
	var _tabElements = {
		'elements': [
			{
				'selector': "#mcmsHolder",
				'aria-label': ' ',
				'role': 'none'
			},
			{
				'selector': "#mcmsHolder .imageArea .qBoxImg"
			},
			{
				'selector': "#mcmsHolder .queContainer .mcmsQuestionTxt"
			},
			{
				'selector': "#mcmsHolder .activityinstruction"
			},
			{
				'selector': "#mcmsHolder .option_box_wrap .option-holder .checkBox",
				'role': 'checkbox',
				'aria-checked': 'false'
			},
			{
				'selector': "#mcmsHolder .option_box_wrap .option-holder .radio",
				'role': 'radio',
				'aria-checked': 'false'
			},
			{
				'selector': "#mcmsHolder .btnCont .pop_btn",
				"role": "button"
			}
		],
		'sIndex': 500,
		'nIndex': 1
	};
	Utils.addEventsForTabIndex(_tabElements)

}

MCMS_MCSS_13.addTabIndexingOnFeedback = function () {
	var _tabElements = {
		'elements': [
			{
				'selector': "#mcmsFeedbackBox",
				'aria-label': ' ',
				'role': 'none'
			},
			{
				'selector': "#mcmsFeedbackBox #feedbackContent"
			},
			{
				'selector': "#mcmsFeedbackBox .close_btn",
				"role": "button"
			}
		],
		'sIndex': 500,
		'nIndex': 1
	};
	$("#mcmsFeedbackBox").addClass('iGPopup');
	Utils.addEventsForTabIndex(_tabElements)

	setTimeout(function () {
		$("#mcmsFeedbackBox").focus();
	}, 600)
}

MCMS_MCSS_13.addTabIndexingOnFeedback_1 = function () {
	var _tabElements = {
		'elements': [
			{
				'selector': "#mcmsFeedbackBox",
				'aria-label': ' ',
				'role': 'none'
			},
			{
				'selector': "#mcmsFeedbackBox #feedbackContent"
			},
			{
				'selector': "#mcmsFeedbackBox .close_btn",
				"role": "button"
			}
		],
		'sIndex': 500,
		'nIndex': 1
	};
	$("#mcmsFeedbackBox").removeClass('iGPopup');
	Utils.addEventsForTabIndex(_tabElements);
	setTimeout(function () {
		$("#mcmsFeedbackBox").focus();
	}, 600)

}

var initMCMS_MCSS_13 = (function () {

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

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

		MCMS_MCSS_13.initMCMS(_aData);

		_shell_wrapper.off("start_MCMS_MCSS_13").on("start_MCMS_MCSS_13", function () {
			MCMS_MCSS_13.addListeners();
		});

	};

	return this;

})