/**
 * Created by aheadware
 */

"use strict";

var app = app || {};
app.core = app.core || {};

/**
 * View singleton
 * @type {{getBody, getTemplate, init}}
 */
app.core.View = (function(){

    // Private vars
    var $body = null,
        $authSection = null,
        $authContent = null,
        $contentSection = null,
        $content = null;

    // Private methods


    return {
        alert : function( message, fnOkPressed )
        {
            var onOk = fnOkPressed ||  null,
                fnOk = function()
            {
                app.core.View.closeModal();
                onOk && onOk();
            };

            app.core.View.showModal({
                body : message,
                cancelButtonText : null,
                onConfirm : fnOk
            });

        }
        ,
        confirm : function( message, fnOk, txtBtnOk, fnCancel, txtBtnCancel )
        {
            var fnOk = fnOk,
                fnCacnel = fnCacnel || null;
            app.core.View.showModal({
                body : message,
                okButtonText : txtBtnOk || null,
                cancelButtonText : txtBtnCancel || _lc( 'MODAL_BTN_CANCEL' ),
                onConfirm : function()
                {
                    fnOk();
                    app.core.View.closeModal();
                }
            });
        }
        ,
        getAuthContent : function()
        {
            return $authContent;
        }
        ,
        /**
         * Get jQuery Body element
         * @returns {*}
         */
        getBody : function()
        {
            return $body;
        }
        ,
        getContent : function()
        {
            return $content;
        }
        ,
        /**
         * Get rendered template
         *
         * @param {string] }id
         * @param {Object||null} data
         * @returns {*}
         */
        getTemplate : function( id, data )
        {
            let tmplId = 'tpl-' + id;

            if ( !$body.find( '#' + tmplId ).length )
            {
                console.log( "Template id: " + tmplId + " not found." );
            }

            return template( tmplId, data || {} );
        }
        ,
        /**
         * Init view
         */
        init : function()
        {
            // Get HTML elements
            $body = $( 'body' );
            $body.append( this.getTemplate( 'layout' ) );
            $content = $body.find( '[data-id="content"]' ).first();
            $contentSection = $body.find( '[data-id="section-content"]' ).first();
            $authContent = $body.find( '[data-id="auth-content"]' ).first();
            $authSection = $body.find( '[data-id="section-auth"]' ).first();

            // Init GUI component
            app.gui.OffcanvasMenu.init();
            //app.gui.PageLoader.hide();
            app.gui.Navbar.init();
            app.core.NotificationManager.initHandlers();
            app.gui.ActionButton.init();
        }
        ,
        setAuthContent : function( html )
        {
            // Unbind all content relative listeners
            $authContent.find( "*" ).off();
            $authContent.off();
            $content.find( "*" ).off();
            $content.off();

            // Toggle views
            $contentSection.hide();
            $body.removeClass( 'body-content' );
            $body.addClass( 'body-auth' );
            $authSection.show();

            // Show content
            $authContent.html( html );

            // Animate scroll to top
            $("html, body").animate({ scrollTop: 0 });
        }
        ,
        setContent : function( html )
        {
            // Unbind all content relative listeners
            $authContent.find( "*" ).off();
            $authContent.off();
            $content.find( "*" ).off();
            $content.off();

            // Toggle views
            $authSection.hide();
            $body.removeClass( 'body-auth' );
            $body.addClass( 'body-content' );
            $contentSection.show();

            // Dispose previously initialized pop overs
            $content.find( '[data-type="popover"]' ).popover( 'dispose' );
            $content.off( 'click', '[data-type="btn-toggle-info"]' );

            // Set content
            $content.html( html );

            // Animate scroll to top
            $("html, body").animate({ scrollTop: 0 });

            // Init popovers/help automatically
            $content.find( '[data-type="popover"]' ).popover();
            $content.on( 'click', '[data-type="btn-toggle-info"]', function()
            {
                $content.find( '[data-type="form-info"]' ).toggle();
            });
        },
        setContentLoader : function( $elem )
        {
            if ( $elem && $elem.length > 0 )
            {
                $elem.html( '<i class="fas fa-spinner fa-spin"></i>' );
            }
        }
        ,
        getModalContent : function()
        {
            return $( '[data-id="modal-general"]' ).first();
        }
        ,
        showModal : function( params )
        {
            var $modal = $( '[data-id="modal-general"]' ).first(),
                title = params.title || "",
                body = params.body || "",
                okBtnText = params.okButtonText || _lc( 'MODAL_BTN_OK' ),
                cancelBtnText = params.cancelButtonText || _lc( 'MODAL_BTN_CANCEL' ),
                showOnlyCloseButton = params.hasOwnProperty( 'showOnlyCloseButton' ) ? params.showOnlyCloseButton : false,
                cbOnConfirm = params.onConfirm || null,
                cbOnShow = params.onShow || null,
                cbOnHide = params.onHide || null,
                options = { show : true },
                isAlert = ( params.hasOwnProperty( 'cancelButtonText' ) && params.cancelButtonText === null );

            $modal.find( '[data-id="modal-general-title"]' ).first().html( title );
            $modal.find( '[data-id="modal-general-body"]' ).first().html( body );
            $modal.find( '[data-id="modal-general-btn-ok"]' ).first().html( okBtnText );
            $modal.find( '[data-id="modal-general-btn-cancel"]' ).first().html( cancelBtnText );

            if ( !title || !title.length )
            {
                $modal.find( '[data-id="modal-header"]' ).hide();
            }
            else
            {
                $modal.find( '[data-id="modal-header"]' ).show();
            }

            if ( true === isAlert )
            {
                $modal.find( '[data-id="modal-general-btn-cancel"]' ).first().hide();
                options.backdrop = 'static';
                options.keyboard = false;
            }
            else
            {
                $modal.find( '[data-id="modal-general-btn-cancel"]' ).first().show();
            }

            if ( cbOnConfirm )
            {
                $modal.find( '[data-id="modal-general-btn-ok"]' ).first().click( function()
                {
                    cbOnConfirm();
                });
            }

            if ( true === showOnlyCloseButton )
            {
                $modal.find( '[data-id="modal-general-btn-ok"]' ).first().hide();
            }
            else
            {
                $modal.find( '[data-id="modal-general-btn-ok"]' ).first().show();
            }

            $modal.on( 'shown.bs.modal', function( e )
            {
                cbOnShow && cbOnShow();
            });
            $modal.on( 'hide.bs.modal', function( e )
            {
                cbOnHide && cbOnHide();
                $modal.find( "*" ).off();
                $modal.off();
            });

            $modal.modal( options );
        }
        ,
        closeModal : function( fnOnHidden )
        {
            var $modal = $( '[data-id="modal-general"]' ).first(),
                onHidden = fnOnHidden || null;

            $modal.modal( 'hide' );

            if( onHidden )
            {
                setTimeout( function()
                {
                    onHidden && onHidden();
                }, 500 );
            }
        }
        ,
        toastError : function( message )
        {
            $.notiny( {
                text: message,
                position: 'fluid-bottom',
                template : '<div class="notiny-base" style="background-color: #C13C2A"><img class="notiny-img"><div class="notiny-text" style="text-align:center"></div></div>'
            } );
        }
        ,
        toastInfo : function( message )
        {
            $.notiny( {
                text: message,
                position: 'fluid-bottom',
                template : '<div class="notiny-base" style="background-color: #2B2D42"><img class="notiny-img"><div class="notiny-text" style="text-align:center"></div></div>'
            } );
        }
        ,
        toastWarning : function( message )
        {
            $.notiny( {
                text: message,
                position: 'fluid-bottom',
                template : '<div class="notiny-base" style="background-color: #E1B860"><img class="notiny-img"><div class="notiny-text" style="text-align:center"></div></div>'
            } );

        }
        ,
        toastSuccess : function( message )
        {
            $.notiny( {
                text: message,
                position: 'fluid-bottom',
                template : '<div class="notiny-base" style="background-color: #87AD71"><img class="notiny-img"><div class="notiny-text" style="text-align:center"></div></div>'
            } );

        }
    };
})();