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

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

app.state.AppointmentEditAttendee = function()
{
    var state = app.core.StateManager.createState( 'appointment-edit-attendee' );

    state.onEnter = function( p )
    {
        var $content = app.core.View.getContent(),
            notDecidedMember = [],
            appointment = null,
            isSerial = p.hasOwnProperty( 'serial' ) ? p.serial : false,
            appointmentId = p.id;

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Appointment',
            'getDetail',
            {
                appointmentId : appointmentId
            },
            function( res )
            {
                appointment = new app.model.Appointment( res.appointment, res.attendee_data, res.attendee_profiles );

                app.core.Rpc.call(
                    'Team',
                    'getMembers',
                    { teamId : appointment.getTeamId() },
                    function( res2 )
                    {
                        for ( var tmi = 0; tmi < res2.members.length; tmi++ )
                        {
                            if ( null == appointment.getAttendeeByProfileId( res2.members[ tmi ].id ) )
                            {
                                notDecidedMember.push( new app.model.Profile( res2.members[ tmi ] ));
                            }
                        }

                        app.core.View.setContent(
                            app.core.View.getTemplate(
                                'appointment-edit-attendee',
                                {
                                    a: appointment,
                                    members : notDecidedMember
                                }
                            )
                        );

                        $content.find( '[data-id="btn-save"]' ).click( function()
                        {
                            var statusChanges = [],
                                currentCount = 0,
                                numChanges = 0,
                                fnProgress,fnOnDone;

                            fnProgress = function()
                            {
                                currentCount++;
                                if ( numChanges === currentCount )
                                {
                                    fnOnDone();
                                }

                            };
                            fnOnDone = function()
                            {
                                app.core.View.toastSuccess( _lc( 'ATTENDEE_LIST_SUCCESSFULLY_UPDATED' ) );
                                setTimeout( function()
                                {
                                    app.core.Controller.redirect( '#/appointment/' + appointmentId + '/attendee' );
                                }, 250 );
                            };

                            if ( appointment.hasStarted() )
                            {
                                $content.find( '[data-type="checkbox-attendee-final-status"]' ).each( function( i, v )
                                {
                                    $checkbox = $(this);

                                    if ( true === $checkbox.prop( 'checked' ) &&
                                         'checked' !== $checkbox.attr( 'data-orig-status' ) )
                                    {
                                        statusChanges.push({
                                            appointmentId : appointmentId,
                                            profileId : $checkbox.attr( 'data-profile-id' ),
                                            status : 'accepted',
                                            isLastInBatch : false
                                        });
                                    }
                                    else if ( false == $checkbox.prop( 'checked' ) &&
                                              'not-checked' !== $checkbox.attr( 'data-orig-status' ) )
                                    {
                                        statusChanges.push({
                                            appointmentId : appointmentId,
                                            profileId : $checkbox.attr( 'data-profile-id' ),
                                            status : 'declined',
                                            isLastInBatch : false
                                        });
                                    }
                                });
                            }
                            else
                            {
                                $content.find( '[data-type="select-attendee-status"]' ).each( function( i, v )
                                {
                                    var $select = $(this);

                                    // If status has changed
                                    if ( $select.val() !== 'no-change' )
                                    {
                                        statusChanges.push({
                                            appointmentId : appointmentId,
                                            processSerial : isSerial,
                                            profileId : $select.attr( 'data-profile-id' ),
                                            status : $select.val(),
                                            isLastInBatch : false
                                        });
                                    }

                                });
                            }

                            if ( statusChanges.length > 0 )
                            {
                                app.gui.PageLoader.show();

                                numChanges = statusChanges.length;
                                statusChanges[ (numChanges-1) ].isLastInBatch = true;

                                for ( var si = 0; si < statusChanges.length; si++ )
                                {
                                    app.core.Rpc.call(
                                        'Appointment',
                                        'setAttendanceStatusByTrainer',
                                        statusChanges[ si ],
                                        function()
                                        {
                                            fnProgress();
                                        }
                                        ,
                                        function()
                                        {
                                            fnProgress();
                                        }
                                    );
                                }
                            }
                        });

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

    state.onExit = function( p )
    {
    };

    return state;
};