(function ($) {

    $.fn.settingsListen = function (options) {
        var $this = this;
        var cols_seo = [];
        var cols_content = [];
        var settings = $.extend({
            message_div: $('.page-message'),
            max_cols_seo: $('#max_cols_number').val(),
            max_cols_content: $('#max_cols_number').val(),
            total_pressed_seo: 0,
            total_pressed_content: 0
        }, options);

        //Init cols
        $this.find('.set-group').each(function () {
                var checkbox = $(this).find('input[type=checkbox]');

                if (checkbox.prop('checked') == 'checked' || checkbox.prop('checked') == true) {
                    var subcategory = $(this).data('subcategory');
                    switch (subcategory) {
                        case 'seo':
                            if (settings.total_pressed_seo < settings.max_cols_seo) {
                                settings.total_pressed_seo++;
                                cols_seo.push($(this).data('id'));
                            }
                            else {
                                $(this).find('input[type=checkbox]').prop('checked', false);
                            }
                            break;
                        case 'content':
                            if (settings.total_pressed_content < settings.max_cols_content) {
                                settings.total_pressed_content++;
                                cols_content.push($(this).data('id'));
                            }
                            else {
                                $(this).find('input[type=checkbox]').prop('checked', false);
                            }
                            break;
                    }
                }

            }
        );

        //console.log(cols);
        $this.find(".set-group").unbind().on('click', function () {
            var column_id = $(this).data('id');
            var checkbox = $(this).find('input[type=checkbox]');
            var attribute = checkbox.prop('checked');
            var subcategory = $(this).data('subcategory');


            if (attribute == 'undefined' || attribute == false) {
                switch (subcategory) {
                    case 'seo':
                        if (settings.total_pressed_seo >= settings.max_cols_seo) {
                            jQuery(settings.message_div).showMessage('warning', 'You can set max ' + settings.max_cols_seo + ' "SEO" columns in posts');
                        } else {
                            checkbox.attr('checked', 'checked');
                            settings.total_pressed_seo++;
                            cols_seo.push(column_id);
                        }
                        break;
                    case 'content':
                        if (settings.total_pressed_content >= settings.max_cols_content) {
                            jQuery(settings.message_div).showMessage('warning', 'You can set max ' + settings.max_cols_content + ' "Content" columns in posts');
                        } else {
                            checkbox.attr('checked', 'checked');
                            settings.total_pressed_content++;
                            cols_content.push(column_id);
                        }
                        break;
                }

            } else if (attribute == 'checked' || attribute == true) {
                switch (subcategory) {
                    case 'content':
                        checkbox.attr('checked', false);
                        if (cols_content.indexOf(column_id) > -1) {
                            cols_content.splice(cols_content.indexOf(column_id), 1);
                            settings.total_pressed_content--;
                        }
                        break;
                    case 'seo':
                        checkbox.attr('checked', false);
                        if (cols_seo.indexOf(column_id) > -1) {
                            cols_seo.splice(cols_seo.indexOf(column_id), 1);
                            settings.total_pressed_seo--;
                        }
                        break;
                }
            }
        });

        $this.find(".btn-group.save.settings").off().on('click', function () {
            $.post(
                ctlppuQuery.ajaxurl,
                {
                    action: 'ctlppu_savesettings',
                    cols: cols_seo.concat(cols_content),
                    ctlppu_nonce: ctlppuQuery.nonce

                }
            ).done(function (response) {
                if (typeof response.error !== 'undefined') {
                    jQuery(settings.message_div).showMessage('warning', response.error);
                }
                else if (response.msg !== 'undefined') {
                    jQuery(settings.message_div).showMessage('success', response.msg);

                }
            }, 'json').error(function () {
                console.log("error");
            });


        });

        return $this;
    }

})(jQuery);


jQuery(document).ready(function () {
    if (jQuery("#ctlppu_settings").length > 0) {
        jQuery("#ctlppu_settings").settingsListen();
    }
});