/**
 * Get Started button on dashboard notice.
 *
 * @package CodeVibrant
 * @subpackage Enrollment
 * @since 1.1.2
 */

jQuery(document).ready(function($) {
    var WpAjaxurl       = cvAdminObject.ajax_url;
    var _wpnonce        = cvAdminObject._wpnonce;
    var buttonStatus    = cvAdminObject.buttonStatus;

    /**
     * Popup on click demo import if cv demo importer plugin is not activated.
     */
    if( buttonStatus === 'disable' ) $( '.enrollment-demo-import' ).addClass( 'disabled' );

    switch( buttonStatus ) {
        case 'activate' :
            $( '.enrollment-get-started' ).on( 'click', function() {
                var _this = $( this );
                enrollment_do_plugin( 'enrollment_activate_plugin', _this );
            });
            $( '.enrollment-activate-demo-import-plugin' ).on( 'click', function() {
                var _this = $( this );
                enrollment_do_plugin( 'enrollment_activate_plugin', _this );
            });
            break;
        case 'install' :
            $( '.enrollment-get-started' ).on( 'click', function() {
                var _this = $( this );
                enrollment_do_plugin( 'enrollment_install_plugin', _this );
            });
            $( '.enrollment-install-demo-import-plugin' ).on( 'click', function() {
                var _this = $( this );
                enrollment_do_plugin( 'enrollment_install_plugin', _this );
            });
            break;
        case 'redirect' :
            $( '.enrollment-get-started' ).on( 'click', function() {
                var _this = $( this );
                location.href = _this.data( 'redirect' );
            });
            break;
    }
    
    enrollment_do_plugin = function ( ajax_action, _this ) {
        $.ajax({
            method : "POST",
            url : WpAjaxurl,
            data : ({
                'action' : ajax_action,
                '_wpnonce' : _wpnonce
            }),
            beforeSend: function() {
                var loadingTxt = _this.data( 'process' );
                _this.addClass( 'updating-message' ).text( loadingTxt );
            },
            success: function( response ) {
                if( response.success ) {
                    var loadedTxt = _this.data( 'done' );
                    _this.removeClass( 'updating-message' ).text( loadedTxt );
                }
                location.href = _this.data( 'redirect' );
            }
        });
    }

    $('.cv-action-btn').click(function(){
        var _this = $(this), actionBtnStatus = _this.data('status'), pluginSlug = _this.data('slug');
        
        switch(actionBtnStatus){
            case 'install':
                enrollment_do_free_plugin( 'enrollment_install_free_plugin', pluginSlug, _this );
                break;

            case 'active':
                enrollment_do_free_plugin( 'enrollment_activate_free_plugin', pluginSlug, _this );
                break;
        }

    });

    enrollment_do_free_plugin = function ( ajax_action, pluginSlug, _this ) {
        $.ajax({
            method : "POST",
            url : WpAjaxurl,
            data : ({
                'action' : ajax_action,
                'plugin_slug': pluginSlug,
                '_wpnonce' : _wpnonce
            }),
            beforeSend: function() {
                var loadingTxt = _this.data( 'process' );
                _this.addClass( 'updating-message' ).text( loadingTxt );
            },
            success: function( response ) {
                if( response.success ) {
                    var loadedTxt = _this.data( 'done' );
                    _this.removeClass( 'updating-message' ).text( loadedTxt );
                }
                location.href = _this.data( 'redirect' );
            }
        });
    }

});