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

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

app.state.ConfigurationProfile = function()
{
    var state = app.core.StateManager.createState( 'configuration-profile' );

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

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Account',
            'getAccountRelatedData',
            null,
            function( res )
            {
                app.gui.PageLoader.hide();

                app.core.View.setContent(
                    app.core.View.getTemplate(
                        'configuration-profile',
                        {
                            profile : new app.model.Profile( res.profile, res.own_teams )
                        }
                    )
                );

                $content.find( '[data-id="btn-update-pic"]' ).click( function()
                {
                    app.core.View.showModal({
                        title: _lc( 'HEADER_UPDATE_PROFILE_PIC' ),
                        body : app.core.View.getTemplate( 'configuration-profile-modal-edit-pic' ),
                        onShow : function()
                        {
                            var $modalRoot = app.core.View.getModalContent();

                            $modalRoot.find( '[data-id="btn-get-pic-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 );
                                    }
                                    ,
                                    function( error )
                                    {
                                        app.core.View.closeModal();
                                    }
                                    ,
                                    app.util.Environment.getCameraOptions( Camera.PictureSourceType.CAMERA )
                                );

                            });

                            $modalRoot.find( '[data-id="btn-get-pic-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 );
                                    }
                                    ,
                                    function( error )
                                    {
                                        app.core.View.closeModal();
                                    }
                                    ,
                                    app.util.Environment.getCameraOptions( Camera.PictureSourceType.PHOTOLIBRARY )
                                );

                            });
                            $modalRoot.find( '[data-id="btn-delete-pic"]' ).first().click( function()
                            {
                                app.core.View.closeModal( function()
                                {
                                    app.core.View.confirm(
                                        _lc( 'DO_YOU_REALLY_WANT_TO_DELETE_THIS_PROFILE_PIC' ),
                                        function()
                                        {
                                            app.gui.PageLoader.show();
                                            app.core.View.closeModal();
                                            app.core.Rpc.call(
                                                'Profile',
                                                'updatePic',
                                                {
                                                    imageData : null
                                                },
                                                function()
                                                {
                                                    app.core.View.toastSuccess( _lc( 'PROFILE_IMAGE_DELETE' ) );
                                                    setTimeout( function()
                                                    {
                                                        app.core.Controller.reload( true );
                                                    }, 250 );
                                                }
                                            );
                                        },
                                        _lc( 'BTN_CONFIRM_PROFILE_IMAGE_DELETION' )
                                    );
                                });
                            });
                        }
                    });

                    return false;
                });
            }
        );
    };

    return state;
};