"use strict";
jQuery( document ).ready( function( $ ) {

    setTimeout( function() {

        init_notices();

    }, 50 );

    function init_notices() {

        jQuery( '.pricely-notice' ).each( function() {

            var obj = $( this );

            var notice_id = 'lite';

            var notice_type = '';

            if( obj.attr( 'data-pricely_id' ) ) {

                notice_id = obj.attr( 'data-pricely_id' );
            }

            if( obj.attr( 'data-pricely_type' ) ) {

                notice_type = obj.attr( 'data-pricely_type' );
            }

            obj.find( '.notice-dismiss,.pricely-btn-secondary' ).each( function() {

                var btn = jQuery( this );

                var remind_me = 'no';

                if( btn.attr( 'data-pricely_remind' ) ) {

                    remind_me = btn.attr( 'data-pricely_remind' );
                }

                btn.on( 'click', function( event ) {

                    if( is_disabled( btn ) ) {

                        event.preventDefault();

                        return false;
                    }

                    var btn_is_wp = true;

                    if( btn.hasClass( 'pricely-btn-secondary' ) ) {

                        event.preventDefault();

                        btn_is_wp = false;

                    }

                    close_notice( notice_id, notice_type, remind_me );

                    if( !btn_is_wp ) {

                        obj.animate( {height: '0px', opacity: 0}, 300, 'swing', function() {

                            obj.remove();
                        } );
                    }

                } );
            } );


            obj.find( '.pricely-sgtn' ).each( function() {

                var btn = jQuery( this );
                var msg = btn.attr( 'data-pricely_msg' );
                var notice_type = obj.attr( 'data-pricely_type' );


                var remind_me_btn = btn.parent( 'p' ).find( '.pricely-btn-secondary' );

                var msg_wait_cont = btn.parent( 'p' ).find( '.pricely-wait-msg' );
                var msg_error_cont = btn.parent( 'p' ).find( '.pricely-msg' );

                btn.on( 'click', function( event ) {

                    event.preventDefault();

                    if( is_disabled( btn ) ) {

                        return false;
                    }

                    disable_button( btn );
                    disable_button( remind_me_btn );

                    var suggestion_id = obj.attr( 'data-pricely_id' );


                    show_wait_msg( msg, msg_wait_cont, msg_error_cont );

                    jQuery.post(
                            pricely_ajax_notice.url,
                            {
                                action: 'pricely_install_suggestion',
                                suggestion_id: suggestion_id,
                            },
                            function( response, status, xhr ) {

                                var resp_msg = '';
                                var resp_status = 'unknown';

                                if( response.message ) {

                                    resp_msg = response.message;
                                }

                                if( response.message ) {

                                    resp_status = response.status;
                                }

                                if( 'activated' == resp_status ) {

                                    show_error_msg( resp_msg, msg_wait_cont, msg_error_cont );

                                    enable_button( btn );
                                    enable_button( remind_me_btn );

                                    close_notice( suggestion_id, notice_type, 'no' );

                                    obj.animate( {height: '0px', opacity: 0}, 300, 'swing', function() {

                                        obj.remove();
                                    } );

                                    return;
                                }

                                if( 'unknown' != resp_status ) {

                                    show_error_msg( resp_msg, msg_wait_cont, msg_error_cont );

                                    return;
                                }

                                jQuery.post(
                                        pricely_ajax_notice.url,
                                        {
                                            action: 'pricely_install_suggestion',
                                            suggestion_id: suggestion_id,
                                        },
                                        function( se_response, se_status, se_xhr ) {

                                            if( se_response.message ) {

                                                resp_msg = se_response.message;
                                            }

                                            if( se_response.message ) {

                                                resp_status = se_response.status;
                                            }

                                            show_error_msg( resp_msg, msg_wait_cont, msg_error_cont );

                                            enable_button( btn );
                                            enable_button( remind_me_btn );

                                            close_notice( suggestion_id, notice_type, 'no' );

                                            obj.animate( {height: '0px', opacity: 0}, 300, 'swing', function() {

                                                obj.remove();
                                            } );

                                        }
                                ).fail( function( xhr, status, error_msg ) {

                                    enable_button( btn );
                                    enable_button( remind_me_btn );

                                    close_notice( suggestion_id, notice_type, 'no' );

                                    obj.animate( {height: '0px', opacity: 0}, 300, 'swing', function() {

                                        obj.remove();
                                    } );
                                } );

                            }


                    ).fail( function( xhr, status, error_msg ) {

                        enable_button( btn );
                        enable_button( remind_me_btn );

                        close_notice( suggestion_id, notice_type, 'no' );

                        obj.animate( {height: '0px', opacity: 0}, 300, 'swing', function() {

                            obj.remove();
                        } );
                    } );

                } );


            } );

        } );

    }

    function show_wait_msg( msg, msg_wait_cont, msg_error_cont ) {

        msg_error_cont.hide();

        msg_wait_cont.html( msg );

        msg_wait_cont.hide().fadeIn( 1000, 'swing', function() {

            msg_wait_cont.show();
        } );

    }

    function show_error_msg( msg, msg_wait_cont, msg_error_cont ) {

        msg_wait_cont.hide();

        msg_error_cont.html( msg );

        msg_error_cont.hide().fadeIn( 1000, 'swing', function() {

            msg_error_cont.show();
        } );

    }

    function close_notice( notice_id, notice_type, remind_me ) {

        jQuery.post( pricely_ajax_notice.url, {
            action: 'pricely_dismiss_notice',
            dismiss_notice_id: notice_id,
            dismiss_notice_type: notice_type,
            mayme_later: remind_me,
        } );
    }

    function disable_button( btn ) {

        btn.addClass( 'pricely-btn-disabled' );
    }

    function enable_button( btn ) {

        btn.removeClass( 'pricely-btn-disabled' );
    }

    function is_disabled( btn ) {

        return btn.hasClass( 'pricely-btn-disabled' );

    }


} );