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

    "use strict";

    function TargetRegistry( dropSourceStore, context, metadata ) {

        var targets = null;

        function getTargetForCurrentPosition( position ) {

            var overTarget = null;
            jq.each( targets, function( index, target ){
                if( isValidTarget( target ) && isPositionOnTarget( target.getPosition(), position ) ){
                    overTarget = target;
                    return false; // jquery.each break
                }

                return true; // jquery.each continue
            });

            return overTarget;
        }

        function isPositionOnTarget( targetPosition, position ) {

            var offset = targetPosition.offset;

            if( !offset ) {
                return false;
            }

            if( position.left < offset.left || position.left > (offset.left + targetPosition.size.width) ) {
                return false;
            }

            if( position.top < offset.top || position.top > (offset.top + targetPosition.size.height) ) {
                return false;
            }

            return true;
        }


        function isValidTarget(target) {

            if( !target ){
                return false;
            }

            if( typeof(target.getPosition) !== "function" ){
                return false;
            }

            if( typeof(target.getTargetId) !== "function" ){
                return false;
            }

            if( typeof(target.onDragStart) !== "function" ){
                return false;
            }

            if( typeof(target.onDragOver) !== "function" ){
                return false;
            }

            if( typeof(target.onDragOut) !== "function" ){
                return false;
            }

            if( typeof(target.onDrop) !== "function" ){
                return false;
            }

            if( typeof(target.setAsBestPracticeSolution) !== "function" ){
                return false;
            }

            return true;
        }

        function getTargetById( targetId ){

            var targetComponent = null,
                currentTargetId;

            jq.each( targets, function( index, target ) {
                currentTargetId = target.getTargetId();
                if(  currentTargetId === targetId ){
                    targetComponent = target;
                    return false; // jquery.each break
                }

                return true; // jquery.each continue
            });

            return targetComponent;
        }

        return {

            initialize : function() {

                var stepId = metadata.page.getId(),
                    targetRegistry = dropSourceStore.getTargetRegistry();

                if( targetRegistry === null ){
                    return;
                }

                targets = targetRegistry[stepId] || [];
            },

            getTargetByPosition : getTargetForCurrentPosition,
            getTargetById : getTargetById
        }
    }
    return TargetRegistry;
});
