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

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

app.state.CourseCategories = function()
{
    var state = app.core.StateManager.createState( 'course-categories' );

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

        app.gui.PageLoader.show();

        app.core.Rpc.call(
            'Profile',
            'get',
            {},
            function( res )
            {
                // Make sure we have an up-to-date user profile incl category settings
                app.model.SessionUser.updateUserData( res.profile, res.profile_teams );

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

                        app.core.View.setContent(
                            app.core.View.getTemplate(
                                'course-categories',
                                {
                                    cp : app.model.SessionUser.getUserProfile(),
                                    groups : app.model.SessionUser.getAdminGroups(),
                                    g: group
                                } )
                        );

                        $content.find( '[data-id="select-group-id"]' ).first().change( function()
                        {
                            app.core.Controller.redirect( '#/course/categories/' + $(this).val() );
                        });

                        $content.on( 'click', '[data-type="btn-edit-category"]', function()
                        {
                            var categoryId = $(this).attr( 'data-category-id' ),
                                category = group.getCourseCategoryById( categoryId );

                            if ( null === category ) return;

                            var fnUpdateCategory = function()
                            {
                                var $modalContent = app.core.View.getModalContent(),
                                    $form = $modalContent.find( '[data-id="form-category"]' ).first(),
                                    isValid = app.util.Form.bootstrapValidate( $form );

                                if ( isValid )
                                {
                                    app.core.View.closeModal();
                                    app.gui.PageLoader.show();
                                    app.core.Rpc.call(
                                        'Team',
                                        'updateCourseCategory',
                                        {
                                            groupId : groupId,
                                            categoryId : categoryId,
                                            name : $form.find( '[data-id="input-category-name"]' ).first().val()
                                        },
                                        function( res )
                                        {
                                            app.core.View.toastSuccess( _lc( 'CATEGORY_UPDATED' ) );
                                            app.core.Controller.reload();
                                        }
                                    );
                                }

                            }.bind( this );

                            app.core.View.showModal(
                                {
                                    'title' : _lc( 'UPDATE_CATEGORIE' ),
                                    'body' : app.core.View.getTemplate( 'course-categories-modal-form', { category : category } ),
                                    'okBtnText' : _lc( 'UPDATE' ),
                                    'onConfirm' : fnUpdateCategory
                                }
                            );

                        });


                        $content.on( 'click', '[data-type="btn-edit-category-users"]', function()
                        {
                            var categoryId = $(this).attr( 'data-category-id' ),
                                members = group.getMembers();

                            app.core.View.showModal({
                                title : _lc( 'MANAGE_USERS_BY_CATEGORY' ),
                                body : app.core.View.getTemplate(
                                    'course-categories-modal-users-form',
                                    {
                                        members : members,
                                        cg : group.getCourseCategoryById( categoryId ),
                                        g : group
                                    }
                                ),
                                okButtonText : _lc( 'BTN_SAVE' ),
                                onConfirm : function()
                                {
                                    var $m = app.core.View.getModalContent(),
                                        categoryUserIds = $m.find( '[data-id="select-category-users"]' ).first().val();

                                    app.gui.PageLoader.show();
                                    app.core.View.closeModal();
                                    app.core.Rpc.call(
                                        'Team',
                                        'setCategoryIdsToProfileIds',
                                        {
                                            groupId : group.getId(),
                                            categoryId : categoryId,
                                            profileIds : categoryUserIds
                                        },
                                        function()
                                        {
                                            app.core.View.toastSuccess( _lc( 'CATEGORY_USER_SUCCESSFUL_UPDATED') );
                                            setTimeout(
                                                function()
                                                {
                                                    app.core.Controller.reload();
                                                },
                                                250
                                            );
                                        }
                                    );
                                }
                            });
                        });

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

                            app.core.View.confirm(
                                _lc( 'DO_YOU_REALLY_WANT_TO_DELETE_THIS_CATEGORY' ),
                                function()
                                {
                                    app.core.View.closeModal();
                                    app.gui.PageLoader.show();
                                    app.core.Rpc.call(
                                        'Team',
                                        'removeCourseCategory',
                                        {
                                            groupId : groupId,
                                            groups : app.model.SessionUser.getAdminGroups(),
                                            categoryId : categoryId
                                        },
                                        function( res )
                                        {
                                            app.core.View.toastSuccess( _lc( 'NOTIFY_COURSE_CATEGORY_DELETED' ) );
                                            app.core.Controller.reload();
                                        }
                                    );
                                },
                                _lc( 'YES_DELETE' )
                            );

                        });

                        $content.find( '[data-id="btn-create-new-category"]' ).first().click( function()
                        {
                            var fnCreateCategory = function()
                            {
                                var $modalContent = app.core.View.getModalContent(),
                                    $form = $modalContent.find( '[data-id="form-category"]' ).first(),
                                    isValid = app.util.Form.bootstrapValidate( $form );

                                if ( isValid )
                                {
                                    app.core.View.closeModal();
                                    app.gui.PageLoader.show();
                                    app.core.Rpc.call(
                                        'Team',
                                        'addCourseCategory',
                                        {
                                            groupId : groupId,
                                            name : $form.find( '[data-id="input-category-name"]' ).first().val()
                                        },
                                        function( res )
                                        {
                                            app.core.View.toastSuccess( _lc( 'NEW_CATEGORY_ADDED' ) );
                                            app.core.Controller.reload();
                                        }
                                    );
                                }

                            }.bind( this );

                            app.core.View.showModal(
                                {
                                    'title' : _lc( 'CREATE_CATEGORIE' ),
                                    'body' : app.core.View.getTemplate( 'course-categories-modal-form', { category : null } ),
                                    'okBtnText' : _lc( 'CREATE' ),
                                    'onConfirm' : fnCreateCategory
                                }
                            );
                        });

                        app.gui.PageLoader.hide();

                    }
                );
            }
        );
    };

    return state;
};