let tabData = {};


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

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


MCMS_MCSS.initMCMS = function (data) {

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

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

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

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

	//initialize submit button

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

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


	MCMS_MCSS.createOptions();
	MCMS_MCSS.disableOptions();

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

	eventMgr.removeControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS.submitClicked);
	eventMgr.addControlEventListener(SubmitButton, "submitClicked", MCMS_MCSS.submitClicked);

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

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

}

MCMS_MCSS.createOptions = function () {

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

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

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

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

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

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

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

		}
	}
};

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

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

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

	MCMS_MCSS.userAnswer = [];
	MCMS_MCSS.correctAnswer = [];

	MCMS_MCSS.activityCompleted = false;

	let _mcmsContainer = $("#mcmsContainer");

	let _qTxt = MCMS_MCSS.questionData.questionText;
	_mcmsContainer.find("#mcmsQuestionTxt").html(_qTxt);

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

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

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

	for (let c = 0; c < MCMS_MCSS.questionData.optionsGroup.length; c++) {
		MCMS_MCSS.userAnswer[c] = [];
		for (let i = 0; i < MCMS_MCSS.questionData.optionsGroup[c].group.length; i++) {
			MCMS_MCSS.userAnswer[c][i] = 0;
			MCMS_MCSS.correctAnswer[c] = MCMS_MCSS.questionData.optionsGroup[c].correctAnswer;
			let option = MCMS_MCSS.optionArr[c][i];
			option.showOption("" + MCMS_MCSS.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.questionData.id + ' - ' + MCMS_MCSS.questionData.questionText + ' - ' + MCMS_MCSS.correctAnswer;
			_mcmsContainer.find("#mcmsQuestionTxt").html(__qTxt);
		}
	} catch (err) {

	}

}

MCMS_MCSS.resetMCMS = function () {

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


MCMS_MCSS.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.currentGroup = parseInt(clicked.parent().attr("class").slice(5, 6));

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


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

}

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

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



MCMS_MCSS.chkUserAnswer = function () {

	SubmitButton.chkSubmitEnable(false);

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

	_checkResult = result;
	MCMS_MCSS.currentAttempt++;
	_shell_wrapper.trigger('activityResult', {
		'qNo': parseInt(MCMS_MCSS.questionData['id']),
		'attemptNo': MCMS_MCSS.currentAttempt,
		'cAns': MCMS_MCSS.correctAnswer[0],
		'uAns': MCMS_MCSS.userAnswer[0],
		'result': result,
	});
	// MCMS_MCSS.showFeedback(result);
};


MCMS_MCSS.showFeedback = function (result) {

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

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

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

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

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

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

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

};

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

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

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


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

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

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

MCMS_MCSS.chkActivityCompletion = function () { }




var initMCMS_MCSS = (function () {

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

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

		MCMS_MCSS.initMCMS(_aData);

		_shell_wrapper.off("start_mcms_mcss_1").on("start_mcms_mcss_1", function () {
			MCMS_MCSS.addListeners();
		});

	};

	return this;

})