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

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

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

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Appointment',
            'getDetail',
            {
                appointmentId : appointmentId
            },
            function( res )
            {
                if ( null === res.appointment )
                {
                    app.core.Controller.redirect( '#/appointment/notfound' );
                    return;
                }

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

                if ( appointment.isDraft() )
                {
                    app.core.Controller.redirect( '#/home' );
                    return false;
                }

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

                $content.on( 'click', '[data-type="btn-appoint-cancel"]', function()
                {
                    var appointmentId = $(this).attr( 'data-appointment-id' );

                    app.core.View.confirm(
                        _lc( 'DO_YOU_REALLY_WANT_TO_CANCEL_APPOINTMENT' ),
                        function()
                        {
                            app.core.Rpc.call(
                                'Appointment',
                                'cancel',
                                {
                                    appointmentId : appointmentId
                                },
                                function( res )
                                {
                                    app.core.View.toastSuccess( _lc( 'CANCEL_APPOINTMENT_SUCCESS' ) );
                                    app.core.Controller.reload();
                                }
                            );
                        },
                        _lc( 'BTN_CONFIRM_CANCEL_APPOINTMENT' )
                    );
                });

                $content.on( 'click', '[data-type="btn-appoint-delete"]', function()
                {
                    var appointmentId = $(this).attr( 'data-appointment-id' );

                    app.core.View.confirm(
                        _lc( 'DO_YOU_REALLY_WANT_TO_DELETE_APPOINTMENT' ),
                        function()
                        {
                            app.core.Rpc.call(
                                'Appointment',
                                'delete',
                                {
                                    appointmentId : appointmentId
                                },
                                function( res )
                                {
                                    app.core.View.toastSuccess( _lc( 'DELETE_APPOINTMENT_SUCCESS' ) );
                                    app.core.Controller.redirect( '#/home' );
                                }
                            );
                        },
                        _lc( 'BTN_CONFIRM_DELETE_APPOINTMENT' )
                    );
                });


                $content.on( 'click', '[data-type="rsvp-button-accept"]', function()
                {
                    var $btn = $(this),
                        appId = $btn.attr('data-appointment-id'),
                        $rsvpContainer = $content.find( '[data-type="appointment-rsvp-container"][data-appointment-id="' + appId + '"]' ).first(),
                        profileStatus = appointment.getAttendeeStatusForProfileId( app.model.SessionUser.getUserProfile().getId() );

                    if ( "accepted" === appointment.getAttendeeStatusForProfileId( app.model.SessionUser.getUserProfile().getId() ) )
                    {
                        return false;
                    }

                    $rsvpContainer.find( '[data-type="section-rsvp-icon-unchecked"]' ).first().hide();
                    $rsvpContainer.find( '[data-type="section-rsvp-icon-loading"]' ).first().show();

                    app.core.Rpc.call(
                        'Appointment',
                        'setAttendanceStatus',
                        {
                            appointmentId : appId,
                            status : "accepted"
                        },
                        function( res )
                        {
                            if ( false === res.hasOwnProperty( 'err' ) )
                            {
                                app.core.View.toastSuccess( _lc( 'APPOINTMENT_STATUS_SAVED' ) );
                                app.core.Controller.reload();
                            }
                            else
                            {
                                if ( res.err == 'max_reached' )
                                {
                                    $rsvpContainer.find( '[data-type="section-rsvp-icon-loading"]' ).first().hide();
                                    $rsvpContainer.find( '[data-type="section-rsvp-icon-unchecked"]' ).first().show();

                                    app.core.View.confirm(
                                        _lc( 'MAX_REACHED_DO_YOU_WANT_ON_BE_ADDED_TO_THE_WAITLIST' ),
                                        function() {

                                            $rsvpContainer.find( '[data-type="section-rsvp-icon-unchecked"]' ).first().hide();
                                            $rsvpContainer.find( '[data-type="section-rsvp-icon-loading"]' ).first().show();

                                            app.core.Rpc.call(
                                                'Appointment',
                                                'setAttendanceStatus',
                                                {
                                                    appointmentId : appId,
                                                    status : 'waiting'
                                                },
                                                function( res )
                                                {
                                                    var newAppointment = new app.model.Appointment( res.appointment, res.attendee_data );
                                                    app.core.View.toastWarning(
                                                        _lc(
                                                            'SUCCESSFULLY_ADDED_TO_WAITING_LIST',
                                                            [
                                                                newAppointment.getWaitingPositionForProfileId( app.model.SessionUser.getUserProfile().getId() )
                                                            ]
                                                        )
                                                    );
                                                    app.core.Controller.reload();
                                                }
                                            );
                                        },
                                        _lc( 'BTN_OK' ),
                                    );
                                }
                                else if ( "deadline_over" === res.err )
                                {
                                    app.core.View.toastError( _lc( 'DEADLINE_IS_OVER' ) );
                                    app.core.Controller.reload();
                                }
                                else
                                {
                                    app.core.View.toastError( _lc( 'GENERAL_SERVER_ERROR' ) );
                                    app.core.Controller.reload();
                                }
                            }
                        },
                        function( res )
                        {
                            app.core.Controller.reload();
                        }
                    );

                });

                $content.on( 'click', '[data-type="rsvp-button-recall"]', function()
                {
                    var $btn = $(this),
                        appId = $btn.attr('data-appointment-id'),
                        $rsvpContainer = $content.find( '[data-type="appointment-rsvp-container"][data-appointment-id="' + appId + '"]' ).first(),
                        profileStatus = appointment.getAttendeeStatusForProfileId( app.model.SessionUser.getUserProfile().getId() );

                    if ( "accepted" !== profileStatus && "waiting" !== profileStatus )
                    {
                        return false;
                    }

                    app.core.View.confirm(
                        _lc( 'REALLY_RECALL_ATTENDEE_STATUS' ),
                        function()
                        {

                            $rsvpContainer.find( '[data-type="section-rsvp-icon-accepted"]' ).first().hide();
                            $rsvpContainer.find( '[data-type="section-rsvp-icon-waiting"]' ).first().hide();
                            $rsvpContainer.find( '[data-type="section-rsvp-icon-loading"]' ).first().show();

                            app.core.Rpc.call(
                                'Appointment',
                                'setAttendanceStatus',
                                {
                                    appointmentId : appId,
                                    status : "declined"
                                },
                                function( res )
                                {
                                    if ( false === res.hasOwnProperty( 'err' ) )
                                    {
                                        app.core.View.toastSuccess( _lc( 'SAVED' ) );
                                        app.core.Controller.reload();
                                    }
                                    else if ( "deadline_over" === res.err )
                                    {
                                        app.core.View.toastError( _lc( 'DEADLINE_IS_OVER' ) );
                                    }
                                    else if ( "deadline_reject_over" === res.err )
                                    {
                                        app.core.View.alert(
                                            _lc( 'DEADLINE_REJECT_IS_OVER' ),
                                            function()
                                            {
                                                app.core.Controller.reload();
                                            }
                                        );
                                    }
                                    else if ( "appointment_is_cancelled" === res.err )
                                    {
                                        app.core.View.alert(
                                            _lc( 'APPOINTMENT_IS_ALREADY_CANCELLED' ),
                                            function()
                                            {
                                                app.core.Controller.reload();
                                            }
                                        );
                                    }
                                    else if ( "appointment_not_accessible" === res.err )
                                    {
                                        app.core.View.toastSuccess( _lc( 'SAVED' ) );
                                        setTimeout( function()
                                        {
                                            app.core.Controller.redirect( '#/home' );
                                        }, 250 );
                                    }
                                    else
                                    {
                                        app.core.View.toastError( _lc( 'GENERAL_SERVER_ERROR' ) );
                                        app.core.Controller.reload();
                                    }
                                }
                            );
                        }
                    );
                });


                /*
                $content.on( 'click', '[data-type="rsvp-button"]', function() {
                    var $btn = $(this),
                        status = $btn.attr( 'data-status' ),
                        appId = $btn.attr( 'data-appointment-id' );

                    if ( appointment && status === appointment.getAttendeeStatusForProfileId( app.model.SessionUser.getUserProfile().getId() ) )
                    {
                        return false;
                    }
                    
                    app.gui.PageLoader.show();
                    
                    app.core.Rpc.call(
                        'Appointment',
                        'setAttendanceStatus',
                        {
                            appointmentId : appId,
                            status : status
                        },
                        function( res )
                        {
                            app.core.View.toastSuccess( _lc( 'APPOINTMENT_STATUS_SAVED' ) );
                            app.core.Controller.reload();
                        }
                    );

                });
                */

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

    state.onExit = function( p )
    {
    };

    return state;
};