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

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

app.state.AppointmentDetailChat = function()
{
    var state = app.core.StateManager.createState( 'appointment-detail-chat' );

    state.onEnter = function( p )
    {
        let $content = app.core.View.getContent(),
            chats = [],
            appointment,
            appointmentId = p.id,
            fnUpdateChats;

        fnUpdateChats = function( lastChatId )
        {
            app.core.Rpc.call(
                'Appointment',
                'getChat',
                {
                    appointmentId : appointmentId,
                    lastId : lastChatId
                },
                function( res )
                {
                    if ( appointment.isDraft() )
                    {
                        app.core.Controller.redirect( '#/home' );
                        return false;
                    }

                    // Remove 'no chats' message from container in case of first message
                    $content.find( '[data-id="chat-frame-container"]' ).first().find( '[data-id="no-messages"]' ).remove();

                    for ( var ci = 0; ci < res.chats.length; ci++ )
                    {
                        $content.find( '[data-id="chat-frame-container"]' ).first().append(
                            app.core.View.getTemplate(
                            'appointment-detail-chat-message',
                            {
                                c: new app.model.Chat( res.chats[ ci ] )
                            })
                        );
                    }

                }
            );
        };

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Appointment',
            'getDetail',
            {
                appointmentId : appointmentId
            },
            function( res ) {

                appointment = new app.model.Appointment( res.appointment, res.attendee_data );

                app.core.Rpc.call(
                    'Appointment',
                    'getChat',
                    {
                        appointmentId: appointmentId
                    },
                    function (res2)
                    {
                        for (var ci = 0; ci < res2.chats.length; ci++)
                        {
                            chats.push(new app.model.Chat(res2.chats[ci]));
                        }

                        app.core.View.setContent(
                            app.core.View.getTemplate(
                                'appointment-detail-chat',
                                {
                                    a : appointment,
                                    chats: chats
                                })
                        );

                        $content.find( '[data-id="btn-send-chat"]' ).click( function()
                        {
                            var msg = $content.find( '[data-id="input-chat-message"]' ).first().val();
                            msg = app.util.Helper.trim( msg );

                            if ( app.model.SessionUser.getUserProfile().isAnonymousInGroup( appointment.getTeamId() ) )
                            {
                                app.core.View.confirm(
                                    _lc( 'SWITCH_ANONYMOUS_MODE_OFF_TO_CHAT' ),
                                    function()
                                    {
                                        app.core.Controller.redirect( '#/configuration/privacy/edit' )
                                    }
                                );
                                return false;
                            }

                            if ( 0 === msg.length )
                            {
                                return false;
                            }

                            app.core.Rpc.call(
                                'Appointment',
                                'chat',
                                {
                                    appointmentId : appointmentId,
                                    message : msg
                                },
                                function( res )
                                {
                                    var lastChatId = $content.find( '.chat-row:last' ).attr( 'data-chat-id' );
                                    fnUpdateChats( lastChatId );
                                    $content.find( '[data-id="input-chat-message"]' ).first().val( "" );
                                }
                            );
                        });

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

    state.onExit = function( p )
    {
    };

    return state;
};