/**
 * Created by Benny on 12.10.2014.
 */

"use strict";

var TB = TB || {};
TB.Control = TB.Control || {};

TB.Control.Profiles = {
    // Overview
    index : function( params ) {

        TB.GUI.showLoader();
        var page = new TB.Page( 'profiles' );

        TB.Service.request('Profile', 'getAll', {}, function (data) {
            TB.GUI.hideLoader();

            if ( data && data.profiles ) {
                TB.Client.updateSessionProfiles( data.profiles) ;
            }

            TB.GUI.show( page, { profile : TB.Client.profile, profiles: TB.Client.profiles, teams : TB.Client.teams } );

            page.$container.find( 'button[data-action="change_profile"]' ).on( "click", function() {
                TB.Service.request( 'Profile', 'change', { profile_id : $(this).attr( "data-profile_id" ) },
                    function( data ) {
                        if ( data && data.session && data.profile && data.profiles && data.teams ) {
                            TB.Client.setSessionData( TB.Client.account, data.profile, data.session, data.profiles, data.teams );
                            location.reload( true );
                        }
                    });
            });

            page.$container.find( 'button[data-action="new-team"]').on( 'click', function() {
                TB.Controller.do( 'Profiles', 'newteam' );
            });

        });
    },

    newteam : function( params ) {
        var page = new TB.Page( 'profiles_newteam' );
        TB.GUI.show( page, {} );

        page.$container.find( '#btn_cancel_add_new_team').click( function() {
            TB.Controller.do( 'Profiles' );
        });

        page.$container.find( '#registerteamcategory' ).easyAutocomplete({
            // Note:
            // Duplicate list
            data : TB.Config.General.team_categories,
            list: {
                maxNumberOfElements: 10,
                match: {
                    enabled: true
                }
            }
        });

        page.$container.find( '#btn_add_new_team').click( function() {
            if ( !TB.GUI.performFormValidation( $( '#form_add_new_team' ) ) ) {
                return false;
            }



            TB.Service.request( 'Profile', 'addNewTeam', {
                'team_name' : page.$container.find( '#form_add_new_team input[name="register.team_name"]' ).val(),
                'team_category' : page.$container.find( '#form_add_new_team input[name="register.team_category"]' ).val()
            }, function( res ) {

                if ( res && res.new_profile_id ) {
                    TB.Service.request( 'Profile', 'change', { profile_id : res.new_profile_id },
                        function( data ) {
                            if ( data && data.session && data.profile && data.profiles && data.teams ) {
                                TB.Client.setSessionData( TB.Client.account, data.profile, data.session, data.profiles, data.teams );
                                TB.Controller.do( 'Profiles' );
                                location.reload( true );
                            }
                        });
                } else {
                    TB.Ext.Alert.show( 'Fehler', 'Ein Fehler ist aufgetreten. Bitte versuche es später erneut.' );
                }

                return false;
            });


            return false;
        });
    }
};