/**
 * (c) by aheadware.com
 */

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

app.state.MessageList = function()
{
    var state = app.core.StateManager.createState('message-list');

    state.onEnter = function (p)
    {
        let $content = app.core.View.getContent();

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Message',
            'getList',
            null,
            function (res)
            {
                var messages = [];

                if ( res.message_data.length )
                {
                    for ( var m = 0; m < res.message_data.length; m++ )
                    {
                        messages.push( new app.model.Message( res.message_data[ m ] ) );
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'message-list',
                        {
                            messages : messages
                        })
                );

                $content.find( '[data-type="checkbox-all-toggle"]' ).click( function()
                {
                    if ( $(this).is( ':checked' ) )
                    {
                        // Unselect all
                        $content.find( '[data-type="checkbox-message-deletion"]' ).each( function()
                        {
                            $(this).prop( 'checked', true );
                        });
                    }
                    else
                    {
                        // Select all
                        $content.find( '[data-type="checkbox-message-deletion"]' ).each( function()
                        {
                            $(this).prop( 'checked', false );
                        });
                    }
                });

                $content.on( 'click', '[data-type="message-click-link"]', function(e)
                {
                    app.gui.PageLoader.show();
                    app.core.Controller.redirect( '#/message/' + $(this).closest( 'tr' ).attr( 'data-message-id' ) );
                });

                $content.find( '[data-id="btn-delete-selected-messages"]' ).click( function()
                {
                    var messageIds = [];
                    $content.find( '[data-type="checkbox-message-deletion"]' ).each( function()
                    {
                        if ( $(this).is( ':checked' ) )
                        {
                            messageIds.push( $(this).attr( 'data-message-id' ) );
                        }
                    });

                    if ( 0 === messageIds.length )
                    {
                        app.core.View.toastInfo( _lc( 'PLEASE_SELECT_MESSAGES_TO_DELETE' ) );
                        return false;
                    }

                    app.gui.PageLoader.show();

                    app.core.Rpc.call(
                        'Message',
                        'delete',
                        {
                            messageIds : messageIds.join( "," )
                        },
                        function( res )
                        {
                            app.core.View.toastSuccess( _lc( 'SELECTED_MAILS_DELETED', [ messageIds.length ] ) );
                            app.core.Controller.reload();
                        }
                    );
                });
                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};