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

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

app.state.GroupInvite = function()
{
    var state = app.core.StateManager.createState( 'group-invite' );

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Team',
            'getDetails',
            { teamId : groupId },
            function( res )
            {
                var mGroup = new app.model.Group( res.team ),
                    currentProfile = null;

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'group-invite',
                        {
                            group : mGroup,
                            currentProfile : app.model.SessionUser.getUserProfile()
                        } )
                );

                $content.find( '[data-id="btn-share-invitation"]' ).first().click( function()
                {
                    app.core.View.showModal({
                        title : _lc( 'INVITE_MEMBERS_HEADER' ),
                        body : app.core.View.getTemplate( 'group-invite-modal-share' ),
                        okButtonText : _lc( 'BTN_MOBILE_SHARE' ),
                        onConfirm : function()
                        {
                            if ( window.plugins && window.plugins.socialsharing )
                            {
                                var $modal = app.core.View.getModalContent(),
                                    msg = $modal.find( '[data-id="input-share-invitation-text"]' ).first().val();

                                window.plugins.socialsharing.shareWithOptions({
                                        message : msg,
                                        subject : _lc ( 'SHARE_MEMBERS_INVITATION_EMAIL_SUBJECT' ),
                                        url : app.core.App.getConfig( 'url' ) + '/#/auth/join/' + mGroup.getPublicId()
                                    },
                                    function()
                                    {
                                        app.core.View.closeModal();
                                    }
                                    ,
                                    function()
                                    {
                                        app.core.View.closeModal();
                                    }
                                );
                            }
                            else
                            {
                                app.core.View.toastError( _lc( 'SOMTEHING_WENT_WRONG' ) );
                            }

                        }
                    });
                });

                $content.find( '[data-id="btn-copy-clipboard"]' ).first().click ( function()
                {
                    var inputElement = $content.find( '[data-id="input-join-url"]' ).first().get( 0 );
                    if ( app.util.Helper.copyInputElementClipboard( inputElement ) )
                    {
                        app.core.View.toastSuccess( _lc( 'COPIED_TO_CLIPBOARD' ) );
                    }
                });

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

    return state;
};