(function (paw, utils) {
    function PawSelect2MultiselectHelper() {

    }

    PawSelect2MultiselectHelper.prototype.init = function ($elt) {
        if (!this.isPawMultiselect2($elt)) {
            return;
        }

        $elt.next('.select2').addClass("paw-select2-multiselect");
        if(utils.value.strToBool($elt.data('allow-clear'))){
            $elt.next('.select2').addClass("paw-select2-multiselect-allow-clear");
        }

        var select2 = $elt.data('select2');
        select2.$selection.append('<div class="select2-selection__arrow"  role="presentation"><b role="presentation"></b></div>');

        this.renderSelectedItems($elt);
        this.handleChangeEvent($elt);
    };

    PawSelect2MultiselectHelper.prototype.isPawMultiselect2 = function($elt) {
        return paw.utils.value.strToBool($elt.data('paw-multiselect')) && $elt.attr('multiple') === 'multiple';
    };

    PawSelect2MultiselectHelper.prototype.handleChangeEvent = function($elt){
        var self = this;
        if(this.isPawMultiselect2($elt) && utils.value.strToBool($elt.data('allow-clear'))){
            $elt.on('change', function(){
                self.renderSelectedItems($elt);
                if($elt.val().length === 0){
                    var select2 = $elt.data('select2');
                    select2.$dropdown.find('.paw-select2-elt-selected').removeClass('paw-select2-elt-selected')
                            .find('.paw-select2-elt-selection-checked').remove();
                }
            });
        }
    };

    PawSelect2MultiselectHelper.prototype.onItemSelected = function ($elt, dataId) {
        if (!this.isPawMultiselect2($elt)) {
            return;
        }
        var select2 = $elt.data('select2');
        var $item = select2.$dropdown.find('[data-select2-id="' + dataId + '"]');
        if ($item.hasClass('paw-select2-elt-selected')) {
            $item.removeClass('paw-select2-elt-selected');
            $item.find('.paw-select2-elt-selection-checked').remove();
            $elt.val($elt.val().filter(function (value) {
                return value != dataId;
            })).trigger('change');

            select2.$selection.find('.paw-custom-selection');
        }
        else {
            $item.addClass('paw-select2-elt-selected')
            $item.append('<div class="paw-select2-elt-selection-checked"><span class="glyphicon glyphicon-ok check-mark"></span></div>')
        }
        this.getInputField($elt).val('');
    };

    PawSelect2MultiselectHelper.prototype.renderSelectedItems = function ($elt) {
        if (!this.isPawMultiselect2($elt)) {
            return;
        }

        var select2 = $elt.data('select2');
        var $selOpts = select2.$selection.find('.paw-custom-selection');
        if ($selOpts.length === 0) {
            $selOpts = $('<div class="paw-custom-selection"></div>');
            select2.$selection.append($selOpts);
        }

        var selVals = [];
        select2.$element.find('option:selected').each(function () {
            var $opt = $(this);
            selVals.push($opt.val());
        });
        $selOpts.text(selVals.join(', '));
        $selOpts.show();
    };

    PawSelect2MultiselectHelper.prototype.isMultiValueSelected = function ($elt, dataId) {
        return paw.utils.value.isSet($elt.val()) && $elt.val().indexOf(dataId) > -1;
    };

    PawSelect2MultiselectHelper.prototype.multivalueSelectedClass = function ($elt, dataId) {
        if (this.isPawMultiselect2($elt) && this.isMultiValueSelected($elt, dataId)) {
            return 'paw-select2-elt-selected';
        }
        return '';
    };

    PawSelect2MultiselectHelper.prototype.markupSelectionChecked = function ($elt, dataId) {
        if (this.isPawMultiselect2($elt) && this.isMultiValueSelected($elt, dataId)) {
            return '<div class="paw-select2-elt-selection-checked"><span class="glyphicon glyphicon-ok check-mark"></span></div>';
        }
        return '';
    };

    PawSelect2MultiselectHelper.prototype.getInputField = function($elt){
        var select2 = $elt.data('select2');
        return select2.$selection.find('.select2-search__field');
    };

    paw.helper.select2MultiselectHelper = new PawSelect2MultiselectHelper();
})(paw, paw.utils);