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

"use strict";

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

TB.Control.News =  {

    index : function() {
        TB.GUI.showLoader();
        var page = new TB.Page('news');
        TB.Service.request('News', 'get', { filter : TB.Client.getConfig( 'news_filter' ) }, function (data) {
            TB.GUI.hideLoader();
            data.news_filter = TB.Client.getConfig( 'news_filter' );

            // Only scroll down if there are at least 3 news
            if ( data && data.news.length > 2 ) {
                TB.GUI.show(page, data, true );
            } else {
                TB.GUI.show(page, data );
            }
            TB.GUI.updateNewsBadge(0);

            page.$container.find( '#btn_news_create').click( function() {
                TB.Controller.do( 'News', 'create' );
            });

            page.$container.find( '#btn-coach-help').click( function() {
                TB.GUI.showModal(
                    "News",
                    TB.Config.Content.helpHtml.news
                );
            });

            page.$container.find( 'button[data-dropdown-action="toggle-chat-filter"]' ).click( function() {
                if ( "all" == $(this).attr( 'data-current-val' ) ) {
                    TB.Client.setConfig( 'news_filter', 'custom' );
                } else {
                    TB.Client.setConfig( 'news_filter', 'all' );
                }

                TB.Controller.reload();
            });

            page.$container.find( 'button[data-action="delete"]').click( function() {
                $(this).blur();
                var newsId = $(this).attr( 'data-news_id' );
                TB.Ext.Alert.modal({
                    title: 'Diesen Eintrag wirklich löschen?'
                }, function() {
                    TB.Service.request( 'News', 'delete', { news_id : newsId }, function( data ) {
                        if ( data.res && data.res == 'ok' ) {
                            TB.Ext.Notification.show( 'News Eintrag erfolgreich entfernt.', TB.Ext.Notification.TYPE_SUCCESS );
                            TB.Controller.reload();
                        } else {
                            TB.Ext.Notification.show( 'Ein Fehler ist aufgetreten', TB.Ext.Notification.TYPE_DANGER );
                        }
                    });
                });
            });


            page.$container.find( '#btn_news_chat').click( function() {

                var chatContent = $( '#chat_content').val();
                if ( !chatContent || 0 == chatContent.length ) {
                    return false;
                }

                TB.Service.request( 'News', 'createChat', { content : chatContent }, function( data ) {
                    if ( data && data.res && data.res == 'ok' ) {
                        TB.Ext.Notification.show( 'Chat Eintrag erstellt.', TB.Ext.Notification.TYPE_SUCCESS );
                        TB.Controller.reload();
                    } else {
                        TB.Ext.Notification.show( 'Ein Fehler ist aufgetreten', TB.Ext.Notification.TYPE_DANGER );
                    }
                }, $(this) );

                return false;
            });
        });
    },

    create : function() {
        var page = new TB.Page( 'news_create' );
        TB.GUI.show( page );
        page.$container.find( '#btn_news_create_cancel').click( function() {
            TB.Controller.do('News', 'index');
        });
        page.$container.find( '#btn_news_create_submit').click( function() {
            if (!TB.GUI.performFormValidation($('#form_news_create'))) {
                return false;
            }

            var props = $('#form_news_create').serializeObject();
            TB.Service.request( 'News', 'create', props, function( data ) {
                if ( data.res = 'ok' ) {
                    TB.Ext.Notification.show( 'News Eintrag erfolgreich erstellt.', TB.Ext.Notification.TYPE_SUCCESS );
                } else {
                    TB.Ext.Notification.show( 'Ein Fehler ist aufgetreten', TB.Ext.Notification.TYPE_DANGER );
                }
                TB.Controller.do( 'News', 'index' );
            }, $(this) );

            return false;
        });

    }
};