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



    "use strict";

    function SourceComponentCollection( context, parameters ){

        var multipleSourcesAllowed = parameters.get("disallow_multiple_sources") !== "true",
            defaultDestinationOffset = {
                "left" : parameters.asNumber( "offset-left", 10 ),
                "top" : parameters.asNumber( "offset-top", 40 )
            },
            spacer = parameters.asNumber("entry-space", 8),
            sources = [],
            offsetService = new OffsetService(context);

        function isValidSource( source ){

            if( !source ){
                return false;
            }

            if( typeof(source.getHeight) !== "function" ){
                return false;
            }

            if( typeof(source.isCorrectAssignment) !== "function" ){
                return false;
            }

            if( typeof(source.onPositionOnTargetChanged) !== "function" ){
                return false;
            }

            return true;
        }

        function add(sourceObject) {

            if (!isValidSource(sourceObject)) {
                return false;
            }

            sources.push(sourceObject);
            return true;
        }

        function refreshPositions() {

            var positioningOffset = getSourceDestinationOffset();

            jq.each(sources, function (index, source) {
                source.onPositionOnTargetChanged( positioningOffset, index );
                positioningOffset.top += source.getHeight() + spacer;
            });
        }

        function getSourceDestinationOffset() {

            var positioningElement = jq(context.getPositioningElement()),
                positioningOffset = offsetService.getGlobalOffset( positioningElement );

            return {
                left : positioningOffset.left + defaultDestinationOffset.left,
                top : positioningOffset.top + defaultDestinationOffset.top
            };
        }

        return {

            add: add,

            addByUser: function (sourceObject) {

                if (!add(sourceObject)) {
                    return;
                }

                refreshPositions();
            },

            remove: function (sourceObject) {

                var newSources = [];

                jq.each( sources, function (index, source) {
                    if (sourceObject.getId() !== source.getId()) {
                        newSources.push(source);
                    }
                });

                sources = newSources;
                refreshPositions();
            },

            removeAll: function () {
                sources = [];
            },

            isCapacityAvailable : function() {
                return multipleSourcesAllowed || sources.length === 0;
            },

            addForSolution: function (sourceObject) {

                if (!add(sourceObject)) {
                    return;
                }

                refreshPositions();
            }
        };
    }
    return SourceComponentCollection;
});
