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

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

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

    state.cropImage = function( imgUri, groupId )
    {
        var alloyCrop = new AlloyCrop({
            image_src: "data:image/jpeg;base64," + imgUri,
            circle: false,
            width: 256,
            height: 256,
            output: 1,
            ok: function (base64, canvas)
            {
                app.gui.PageLoader.show();
                alloyCrop.destroy();
                app.core.Rpc.call(
                    'Team',
                    'updateEmblem',
                    {
                        teamId : groupId,
                        imageData : base64
                    },
                    function()
                    {
                        app.core.View.toastSuccess( _lc( 'LOGO_SUCCESSFULLY_UPDATED' ) );
                        app.core.Controller.reload( true );
                    }
                );
            },
            cancel: function ()
            {
                alloyCrop.destroy();
            }
        });
    };

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

        selfState.groupId = groupId;

        app.gui.PageLoader.show();

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

                for ( var mi = 0; mi < res.members.length; mi++ )
                {
                    member = new app.model.Profile( res.members[ mi ] );
                    members.push(  member );
                    if ( member.getId() == app.model.SessionUser.getProfileId() )
                    {
                        currentProfile = member;
                    }
                }

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'group-detail',
                        {
                            members : members,
                            group : group,
                            currentProfile : currentProfile
                        } )
                );

                $content.find( '[data-id="btn-edit-group"]' ).first().click( function()
                {
                    app.core.Controller.redirect( '#/group/' + groupId + '/edit' );
                });

                $content.find( '[data-id="btn-edit-group-photo"]' ).click( function()
                {

                    app.core.View.showModal({
                        title: _lc( 'HEADER_EDIT_LOGO' ),
                        body : app.core.View.getTemplate( 'group-detail-modal-edit-logo' ),
                        onShow : function()
                        {
                            var $modalRoot = app.core.View.getModalContent();

                            $modalRoot.find( '[data-id="btn-get-logo-from-camera"]' ).first().click( function()
                            {
                                if ( !app.util.Environment.isCameraAvailable() )
                                {
                                    app.core.View.closeModal();
                                    app.core.View.toastError( _lc( 'THIS_FEATURE_IS_NOT_AVAILABLE_IN_DESKTOP_VERSION' ) );
                                    return false;
                                }

                                app.core.View.closeModal();

                                navigator.camera.getPicture(
                                    function( imageUri )
                                    {
                                        app.core.View.closeModal();
                                        selfState.cropImage( imageUri, selfState.groupId );
                                    }
                                    ,
                                    function( error )
                                    {
                                        app.core.View.closeModal();
                                    }
                                    ,
                                    app.util.Environment.getCameraOptions( Camera.PictureSourceType.CAMERA )
                                );

                            });

                            $modalRoot.find( '[data-id="btn-get-logo-from-gallery"]' ).first().click( function()
                            {

                                if ( !app.util.Environment.isCameraAvailable() )
                                {
                                    app.core.View.closeModal();
                                    app.core.View.toastError( _lc( 'THIS_FEATURE_IS_NOT_AVAILABLE_IN_DESKTOP_VERSION' ) );
                                    return false;
                                }

                                app.core.View.closeModal();

                                navigator.camera.getPicture(
                                    function( imageUri )
                                    {
                                        app.core.View.closeModal();
                                        selfState.cropImage( imageUri, selfState.groupId );
                                    }
                                    ,
                                    function( error )
                                    {
                                        app.core.View.closeModal();
                                    }
                                    ,
                                    app.util.Environment.getCameraOptions( Camera.PictureSourceType.PHOTOLIBRARY )
                                );

                            });
                            $modalRoot.find( '[data-id="btn-delete-logo"]' ).first().click( function()
                            {
                                app.core.View.closeModal( function()
                                {
                                    app.core.View.confirm(
                                        _lc( 'DO_YOU_REALLY_WANT_TO_DELETE_THE_LOGO' ),
                                        function()
                                        {
                                            app.gui.PageLoader.show();
                                            app.core.View.closeModal();
                                            app.core.Rpc.call(
                                                'Team',
                                                'updateEmblem',
                                                {
                                                    teamId : selfState.groupId,
                                                    imageData : null
                                                },
                                                function()
                                                {
                                                    app.core.View.toastSuccess( _lc( 'LOGO_DELETED' ) );
                                                    setTimeout( function()
                                                    {
                                                        app.core.Controller.reload( true );
                                                    }, 250 );
                                                }
                                            );
                                        },
                                        _lc( 'BTN_CONFIRM_DELETE' )
                                    );
                                });
                            });
                        }
                    });

                    return false;
                });


                $content.find( '[data-id="btn-delete-group"]' ).first().click( function()
                {
                    app.core.View.toastError( 'Not implemented atm.' );
                });


                $content.find( '[data-id="btn-send-message-to-group-owner"]' ).first().click( function()
                {
                    app.core.View.toastError( 'Not implemented atm.' );
                });

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

    return state;
};