// com.tts.initial.html5.quiz.checkstatus
define([
        "jquery"
		], function (
			jq
		){

	"use strict";

	/**
	* @constructor 
	*/
	return function (context, metadata) {

        var shellElement,
            instance,
            isInAuthoringMode = false,
            isInPreviewMode = false,
            isStandardPlayer = context.getPlayerName() === "standard",
            mode = metadata.parameters.asNumber( "mode", 0 ),
            valueToPass = metadata.parameters.asNumber("value-to-pass", 0),
            id = metadata.parameters.asNumber("id", 1),
            allPages = metadata.stage.pages,
            allQuestions = context.quiz.getQuestions();

        function initializeButton(){
            shellElement.css({
                "width": metadata.getWidth() + "px",
                "height": metadata.getHeight() + "px"
            });

            if ( isInAuthoringMode ) {
                createAuthoringShield();
            }
        }

        function fireTrigger() {
            var questionsToCheck = getQuestionToCheck();

            if ( !questionsToCheck ) {
                return;
            }

            var triggerToFire = "passed",
                maxScore = 0,
                userScore = 0;

            for ( var i = 0; i < questionsToCheck.length; i++ ) {
                var resultParams = {
                        scores: getQuestionResultObject( questionsToCheck[i] ),
                        status: questionsToCheck[i].getStatus()
                    };

                if ( mode == 0 ) {
                    if ( resultParams.status === "failed" || resultParams.status === "parttialy_correct" || resultParams.status === "unknown" ) {
                        triggerToFire = "notpassed";
                        break;
                    }
                } else {
                    maxScore = maxScore + resultParams.scores.maxScore;
                    userScore = userScore + resultParams.scores.userScore;
                }
            }

            if ( mode == 1 ) {
                triggerToFire = ( userScore >= valueToPass ) ? "passed" : "notpassed";
            }

            if ( mode == 2 ) {
                triggerToFire = ( ( userScore * 100 ) / maxScore ) >= valueToPass ? "passed" : "notpassed";
            }

            setTimeout( function () {
                    context.page.event.trigger( instance, triggerToFire );
            }, 10);

            context.page.event.fire(instance, "onCheckQuizPageStatusEvent", { id: id, result: triggerToFire });
        }

        function getQuestionToCheck() {
            var quizPagesToCheck = metadata.parameters.get( "quiz_steps" );

            if ( quizPagesToCheck === "" ) {
                return false;
            }

            var result = new Array();

            quizPagesToCheck = quizPagesToCheck.split( ';' );
            for ( var i = 0; i < quizPagesToCheck.length; i++ ) {
                if ( quizPagesToCheck[i].toString().indexOf( '-' ) == -1 ) {
                    if ( allPages[parseInt(quizPagesToCheck[i]) - 1] && getQuestion( allPages[parseInt(quizPagesToCheck[i]) - 1] ) ) {
                        result.push( getQuestion( allPages[parseInt(quizPagesToCheck[i]) - 1] ) );
                    }
                } else {
                    var startEndIndex;

                    startEndIndex = quizPagesToCheck[i].split( '-' );
                    for ( var j = parseInt(startEndIndex[0]); j <= parseInt(startEndIndex[1]); j++ ) {
                        if ( allPages[j - 1] && getQuestion( allPages[j - 1] ) ) {
                            result.push( getQuestion( allPages[j - 1] ) );
                        }
                    }
                }
            }

            return result;
        }

        function getQuestion( page ) {
            for ( var i = 0; i < allQuestions.length; i++ ) {
                if ( page.getId() === allQuestions[i].getPageMetadata().getId() ) {
                    return allQuestions[i];
                }
            }
            return null;
        }

        function getQuestionResultObject( question ) {
            var userScore = parseInt( question.getScore() ),
                maxScore = parseInt( question.getMaxScore() );

            return {
                userScore: userScore,
                maxScore: maxScore
            }
        }

        // create an object shield so we are able to select and drag element
        function createAuthoringShield() {
            var shield = jq( document.createElement( "div" ) )
                .css( {
                    "width": "100%",
                    "height": "100%",
                    "display": "block",
                    "opacity": "0.15",
                    "background-image": "url(\"" + context.getPath() + "invis.png\")",
                    "overflow": "hidden"
                } ),
                caption = jq( document.createElement( "span" ) )
                    .css( {
                        "position": "absolute",
                        "display": "block",
                        "top": "100%",
                        "left": "5px",
                        "color": "#000000",
                        "font-size": "16px",
                        "line-height": "100%"
                    } )
                    .text( "checkQuiz" );

            shield.append( caption );

            shellElement.append( shield );
        }

		// revealing public API
		return {

            onLoading: function () {
                instance = this;

                context.page.event.bindOnEnter( this );

                shellElement = jq(context.getElement());

                isInAuthoringMode = !!context.stage.isInAuthoringMode();
                isInPreviewMode = !!context.stage.isInPreviewMode();

                initializeButton();
            },

            onEnterPage: function () {
                if( !isInPreviewMode && !isStandardPlayer ) {
                    fireTrigger();
                }
            }
        };
	};
});