let tabData = {};

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

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


MCMS_MCSS_1.initMCMS = function (data) {

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

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

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

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

	//initialize submit button

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

	$("#mcmsContinueBtn").addClass("disabled")


	MCMS_MCSS_1.createOptions();
	MCMS_MCSS_1.disableOptions();

	//MCMS_MCSS_1.addListeners();
	MCMS_MCSS_1.showQuestion();
	MCMS_MCSS_1.mainContainer.find("#mcmsNextBtn").hide();

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

	setTimeout(function () {
		$("#mcmsHolder").focus();
		eventMgr.removeControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS_1.submitClicked);
		eventMgr.addControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS_1.submitClicked);
		MCMS_MCSS_1.hintBox();
	}, 600)

}

MCMS_MCSS_1.hintBox = function () {

	let _policyText = MCMS_MCSS_1.questionData.policyText;
	let _clueText = MCMS_MCSS_1.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_1.addEventOnHint = function () {


	let _hint_btn = $(".hint_btn");

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

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


			let _text = MCMS_MCSS_1.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_1.createOptions = function () {

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

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

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

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

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

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

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

		}
	}
};

MCMS_MCSS_1.addListeners = function () {

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

	MCMS_MCSS_1.addEventOnHint();
};

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

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

	MCMS_MCSS_1.userAnswer = [];
	MCMS_MCSS_1.correctAnswer = [];

	MCMS_MCSS_1.activityCompleted = false;

	let _mcmsContainer = $("#mcmsContainer");

	$(".mcmsQuestionTxt").html(MCMS_MCSS_1.questionData.questionText);
	$(".questionID").html(MCMS_MCSS_1.questionData.questionID);

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

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

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

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

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

}

MCMS_MCSS_1.resetMCMS = function () {

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


MCMS_MCSS_1.mcmsOptionClicked = function (evt) {

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

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

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

	if (MCMS_MCSS_1.questionData.type == "mcss") {
		MCMS_MCSS_1.resetMCMS();
		clicked.find("#optionBox").addClass("MCSSbulletPointsSelected");
		MCMS_MCSS_1.userAnswer[MCMS_MCSS_1.currentGroup - 1][_oNo - 1] = 1;
	} else {
		if (clicked.find("#optionBox").hasClass("MCMSbulletPointsSelected")) {
			MCMS_MCSS_1.userAnswer[MCMS_MCSS_1.currentGroup - 1][_oNo - 1] = 1;
		} else {
			MCMS_MCSS_1.userAnswer[MCMS_MCSS_1.currentGroup - 1][_oNo - 1] = 0;
		}
	}


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

}

MCMS_MCSS_1.submitClicked = function (evt) {

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

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



MCMS_MCSS_1.chkUserAnswer = function () {

	SubmitButton.chkSubmitEnable(false);

	let result = 1;
	for (let i = 0; i < MCMS_MCSS_1.userAnswer.length; i++) {
		for (let c = 0; c < MCMS_MCSS_1.userAnswer[i].length; c++) {
			if (MCMS_MCSS_1.userAnswer[i][c] === MCMS_MCSS_1.correctAnswer[i][c]) { } else {
				result = 0;
			}
		}
	}

	MCMS_MCSS_1.currentAttempt++;

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

	MCMS_MCSS_1.showFeedback(result);

};


MCMS_MCSS_1.showFeedback = function (result) {
	if (MCMS_MCSS_1.questionData.responseType !== "") {
		$(".feedBackContent").show();
		$("#hintCoverFeedback").addClass('d-show');
		//$("#mcmsFeedbackBox").addClass("fadeInUpAnim");
		//$("#mcmsFeedbackBox").removeClass("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")

		if (MCMS_MCSS_1.questionData.responseType == "generic") {
			let _genericResponse = MCMS_MCSS_1.questionData.genericResponse
			if (result) {
				Feedback.showFeedback(_genericResponse.correct);
			} else {
				Feedback.showFeedback(_genericResponse.incorrect);
			}
			MCMS_MCSS_1.checkUserAns();
		} else if (MCMS_MCSS_1.questionData.responseType == "specificResponse") {
			let feedStr = "";
			let count = 0;
			for (let c = 0; c < MCMS_MCSS_1.userAnswer[0].length; c++) {
				let i = MCMS_MCSS_1.userAnswer[0][c]
				if (i) {
					count++;
					feedStr += MCMS_MCSS_1.questionData.specificResponse.response[c].description;
					if (count > 0) {
						feedStr += "<br>"
					}
				}
			}
			Feedback.showFeedback(feedStr);

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

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

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

MCMS_MCSS_1.closeFeedback = function () {
	//$("#mcmsFeedbackBox").toggleClass("fadeInUpAnim");
	/*$("#mcmsFeedbackBox").removeClass("fadeInUpAnim");
	$("#mcmsFeedbackBox").addClass("fadeInUpAnim");*/
	$("#hintCoverFeedback").removeClass('d-show');
	//$(".feedBackContent").hide();
	_shell_wrapper.trigger('closeActivity');

};

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

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

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


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

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

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

MCMS_MCSS_1.chkActivityCompletion = function () { }




var initMCMS_MCSS_1 = (function () {

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

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

		MCMS_MCSS_1.initMCMS(_aData);

		_shell_wrapper.off("start_mcms_mcss_11").on("start_mcms_mcss_11", function () {
			MCMS_MCSS_1.addListeners();
		});

		if (callback) {
			callback()
		}
	};

	return this;

})