(function (paw, utils) {
    function LoginUserLicenceSelectionComponent(settings) {
        this.verifyLicences = true;

        this.settings = paw.lang.append({
            $form: undefined,
            loginCheck: undefined,
            licencesApiUrl: undefined
        }, settings);

        this.$modal = null;
        this.$proceedBtn = null;
        this.$activeLicence = null;
        this.$cancelBtn = null;
        this.$preloader = null;
        this.verifyLicencesCheckReset = false;
    }

    LoginUserLicenceSelectionComponent.prototype.init = function () {
        var self = this;
        this.$modal = $('#licensesModal');
        this.$proceedBtn = $('#proceedLicence');
        this.$cancelBtn = $('#cancelLicence');
        this.$activeLicence = $('#ActivateUserLicense');
        this.$preloader = $('#licenceLoginPreloader');

        this.$proceedBtn.on('click', function () {
            self.verifyLicences = false;
            self.$proceedBtn.attr('disabled', true);
            self.$cancelBtn.attr('disabled', true);
            $('#licensesModal ul li button.licence-selector').attr('disabled', true);
            self.$preloader.removeClass('hidden');

            self.settings.$form.submit();
        });
        this.$cancelBtn.on('click', function () {
            self.verifyLicencesCheckReset = true;
            self.$modal.modal('hide');
        });
    };

    LoginUserLicenceSelectionComponent.prototype.destroy = function () {
        this.verifyLicences = true;

        $('#user-licence-list').empty();
        this.$activeLicence.val('');

        // enable login button
        $("button[type='submit']").attr("disabled", false);
        $("button[type='submit'] i").addClass("hidden");
        $('#licensesModal ul li button.licence-selector').off('click');
        this.settings.loginCheck();
    };

    LoginUserLicenceSelectionComponent.prototype.showModal = function () {
        var self = this;
        this.$modal.modal('show');
        this.$modal.on('hidden.bs.modal', function () {
            self.destroy();
        });
    };

    LoginUserLicenceSelectionComponent.prototype.renderLicencesList = function (licences) {
        paw.utils.iterable.iterate(licences, function (userLicence) {
            var markup = [
                '<li>',
                '   <button class="licence-selector" type="button" data-licence="' + userLicence.Code + '">',
                '      <span>' + userLicence.Name + '</span>',
                '   </button>',
                '</li>'
            ];
            $('#user-licence-list').append(DOMPurify.sanitize(markup.join('\n')));
        });
    };

    LoginUserLicenceSelectionComponent.prototype.selectLicence = function (code) {
        this.$activeLicence.val(code);
        var $active = $('#licensesModal ul li button.licence-selector[data-licence=' + code + ']');
        $('#licensesModal ul li button.licence-selector').removeClass('active');
        $active.addClass('active');
        this.$proceedBtn.removeAttr('disabled');
    };

    LoginUserLicenceSelectionComponent.prototype.selectLicences = function () {
        var self = this;
        if (!this.verifyLicences) {
            return false;
        }
        $.post(this.settings.licencesApiUrl, $('#login-form').serialize()).done(function (licences) {
            $('#user-licence-list').empty();
            
            if (utils.value.isArray(licences) && licences.length > 1) {
                self.renderLicencesList(licences);
                self.selectLicence(licences[0].Code);

                self.showModal();

                $('#licensesModal ul li button.licence-selector').on('click', function () {
                    var $btn = $(this);
                    var code = $btn.attr('data-licence');
                    self.selectLicence(code);
                });
                
            }
            else {
                // select single licence
                // submit form in any case (has single licence, no user licence set)
                if (utils.value.isArray(licences) && licences.length === 1) {
                    self.selectLicence(licences[0].Code);
                }

                self.verifyLicences = false;
                self.settings.$form.submit();
            }
        }).fail(function () {
            self.$proceedBtn.removeAttr('disabled');
            self.$cancelBtn.removeAttr('disabled');
            $('#licensesModal ul li button.licence-selector').removeAttr('disabled');
            self.$preloader.addClass('hidden');

            self.verifyLicences = false;
            self.settings.$form.submit();
        });
        return this.verifyLicences;
    };

    paw.com.login.LoginUserLicenceSelectionComponent = LoginUserLicenceSelectionComponent;
})(paw, paw.utils);