// com.tts.initial.html5.quiz.ddsource.autoform
define( function() {

    "use strict";

    function SourceState( dropSourceStore ) {

        var currentTargetId = null,
            targetIndex = -1,
            droppedCorrectly = false;

        function hasValidTargetId(){
            return currentTargetId && currentTargetId !== "";
        }

        return {

        	reset : function( sourceHasTarget ) {
                currentTargetId = null;
                droppedCorrectly = !sourceHasTarget;
            },

            restore : function() {

                var sourceInfo = dropSourceStore.getDropStatus();
                if( sourceInfo === null ) {
                    return false;
                }

                currentTargetId = sourceInfo.droppedOnTarget;
                targetIndex = sourceInfo.targetIndex;
                droppedCorrectly = sourceInfo.droppedCorrect;

                return true;
            },

            store : function() {
                dropSourceStore.setDropStatus( currentTargetId, targetIndex, droppedCorrectly );
            },

            droppedOnCorrectTarget : function( target ){
                droppedCorrectly = true;
                currentTargetId = target.getTargetId();
            },

            droppedOnWrongTarget : function( target ){
                droppedCorrectly = false;
                currentTargetId = target ? target.getTargetId() : null;
            },

            droppedOnInvalidTarget : function( sourceHasTarget ){
                droppedCorrectly = !sourceHasTarget;
                currentTargetId = null;
            },

            isDroppedCorrectly : function(){
                return droppedCorrectly;
            },

            hasValidTargetId : hasValidTargetId,
            getTargetId : function(){
                return hasValidTargetId() ? currentTargetId : null;
            },

            setTargetIndex : function( index ){
                targetIndex = index;
            },
            getTargetIndex : function(){
                return targetIndex;
            }
        }
    }

    return SourceState;
});
