var Wick = Wick || {};
Wick.ReferenceField = (function () {
    var refVar = {
        $ref: $('.ref'),
        $checkbox: $('#ref-add'),
        $checkboxLabel: $('.ref__add-label'),
        $inputLabel: $('.ref__add-text'),
        $info: $('.ref__info'),
        $input: $('.ref__input'),
        $link: $('.ref__link'),
        $count: $('.ref__count'),
        $remove: $('.ref__remove'),
        $countValue: $('.ref__count-val'),
        $refField: $('.ref__field'),
        infoHide: 'ref__info_hide',
        countHide: 'ref__count_hide',
        removeHide: 'ref__remove_hide',
        countMax: 'ref__count_max',
        checkboxLabelHide: 'ref__add-label_hide',
        refFieldError: 'ref__field_error',
        refWide: 'ref_wide',
        maxInputLength: 50,
    };

    function init() {
        var inputValue;

        refVar.$countValue.text(refVar.maxInputLength);

        refVar.$checkbox.on('click', function () {
            if (refVar.$checkbox.attr('checked') === 'checked') {
                refVar.$checkbox.attr('checked', false);
                refVar.$info.addClass(refVar.infoHide);
            } else {
                refVar.$checkbox.attr('checked', 'checked');
                refVar.$info.removeClass(refVar.infoHide);
                refVar.$refField.removeClass(refVar.refFieldError);
                refVar.$countValue.text(refVar.maxInputLength);
                refVar.$input.val('');
            }
        });

        refVar.$input.on('input', function () {
            var currentLength = $(this).val().length,
                left = refVar.maxInputLength - currentLength;

            refVar.$refField.removeClass(refVar.refFieldError);

            if (currentLength < refVar.maxInputLength) {
                refVar.$count.removeClass(refVar.countMax);
            } else {
                refVar.$count.addClass(refVar.countMax);
            }

            inputValue = $(this).val();
            refVar.$countValue.text(left);
        });

        refVar.$link.on('click', function (e) {
            e.preventDefault();
            var linkText = $(this).text().toLowerCase().trim();

            refVar.$refField.removeClass(refVar.refFieldError);

            if (refVar.$input.val().length > 0) {
                refVar.$count.addClass(refVar.countHide);
                refVar.$input.attr('disabled', 'disabled');

                if (linkText === 'save') {
                    $(this).text('edit');
                    refVar.$remove.removeClass(refVar.removeHide);
                    refVar.$checkboxLabel.addClass(refVar.checkboxLabelHide);
                    refVar.$inputLabel.text('Project Reference');

                    if ($(window).width() < 767) {
                        refVar.$ref.addClass(refVar.refWide);
                    }
                }

                else if (linkText === 'edit') {
                    refVar.$count.removeClass(refVar.countHide);
                    refVar.$input.removeAttr('disabled').focus();
                    $(this).text('save');
                    refVar.$inputLabel.text('Add Project Reference');
                }
            }
            else {
                refVar.$refField.addClass(refVar.refFieldError);
            }
        });

        refVar.$remove.click('click', function (e) {
            e.preventDefault();
            refVar.$link.text('save');
            refVar.$input.val('');
            inputValue = '';
            refVar.$remove.addClass(refVar.removeHide);
            refVar.$refField.removeClass(refVar.refFieldError);
            refVar.$checkboxLabel.removeClass(refVar.checkboxLabelHide);
            refVar.$countValue.text(refVar.maxInputLength);
            refVar.$checkbox.trigger('click');
            refVar.$ref.removeClass(refVar.refWide);
            refVar.$count.removeClass(refVar.countHide);
            refVar.$input.removeAttr('disabled').focus();
            refVar.$inputLabel.text('Add Project Reference');
        });
    }

    if (!refVar.$ref.length) {
        return;
    }

    init();
})();
