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

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

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

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            previouslySelectedProfileIds = [],
            recipients = [];

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Message',
            'getRecipients',
            null,
            function( res )
            {
                if ( 0 === recipients.length )
                {
                    // @todo
                }

                for ( var ri = 0; ri < res.recipients.length; ri++ )
                {
                    recipients.push( new app.model.Profile( res.recipients[ ri ] ) );
                }

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

                $content.find( '[data-id="btn-send-message"]' ).first().click( function()
                {
                    let $form = $content.find( '[data-id="form-message"]' ).first(),
                        recipientIds = [],
                        isValid = app.util.Form.bootstrapValidate( $form );

                    if ( isValid )
                    {
                        app.gui.PageLoader.show();

                        $form.find( '[data-id="select-recipients"] > option:selected' ).each( function()
                        {
                            recipientIds.push( $(this).val() );
                        });

                        app.core.Rpc.call(
                            'Message',
                            'create',
                            {
                                recipientIds : recipientIds.join( ',' ),
                                subject : $form.find( '[data-id="input-subject"]' ).first().val(),
                                message : $form.find( '[data-id="textarea-message"]' ).first().val()
                            },
                            function( res )
                            {
                                app.core.View.toastSuccess( _lc( 'MESSAGE_SUCCESSFULLY_CREATED' ) );
                                app.core.NotificationManager.forceUpdate();
                                app.core.Controller.redirect( '#/home' );
                            }
                        );
                    }
                });


                $content.find( '[data-id="input-select-recipients-toggle-all"]' ).first().change( function()
                {
                    if ( $(this).is( ':checked' ) )
                    {
                        previouslySelectedProfileIds = [];
                        $content.find( '[data-id="select-recipients"]' ).first().find( 'option' ).each( function()
                        {
                            if ( $(this).is( ':selected' ) )
                            {
                                previouslySelectedProfileIds.push( $(this).attr( 'data-profile-id' ) );
                            }
                            $(this).prop( "selected", true );
                        });
                        $content.find( '[data-id="collapse-select-recipients"]' ).first().collapse( 'hide' );
                    }
                });

                $content.find( '[data-id="input-select-recipients-toggle-selected"]' ).first().change( function()
                {
                    if ( $(this).is( ':checked' ) )
                    {
                        $content.find( '[data-id="select-recipients"]' ).first().find( 'option' ).each( function()
                        {
                            if ( -1 === previouslySelectedProfileIds.indexOf( $(this).attr( 'data-profile-id' ) ) )
                            {
                                $(this).prop( "selected", false );
                            }
                        });
                        $content.find( '[data-id="collapse-select-recipients"]' ).first().collapse( 'show' );
                    }
                });

                app.gui.PageLoader.hide();
            }
        );
    };

    return state;
};