/**
 * Created by Benny on 12.10.2014.
 */
"use strict";

var TB = TB || {};

TB.Controller = {

    init : function() {
        $( window ).on( 'hashchange', function() {
            TB.Controller.executeHashAction();
        });
        this.reload();
    },

    reload : function() {
        var pageData = this.getPageDataFromUrl();
        this.executeHashAction();
    },

    executeHashAction : function() {
        var pageData = TB.Controller.getPageDataFromUrl();
        TB.GUI.updateMainNavigation( pageData.c );
        if ( TB.Control.hasOwnProperty( pageData.c ) &&
            TB.Control[ pageData.c].hasOwnProperty( pageData.a ) ) {
            TB.Control[ pageData.c ][ pageData.a ]( pageData.params );

            // Track pageview
            try {
                if ( ga ) {
                    ga( 'send', 'pageview', pageData.c + '/' + pageData.a );
                }
            } catch (e) {
                // Fail silently
            }

        } else {
            // 404
            TB.log( 'Controller says 404' );
        }
    },

    getPageDataFromUrl : function() {
        var hashvar = window.location.hash;
        var vars = hashvar.substring(2).split( '&' );
        var keys = {}, ctrl, action;
        for ( var i=0; i < vars.length; i++ ) {
            var tmp = vars[ i ].split( '=' );
            if ( tmp[1] ) {
                if( 'c' === tmp[0] ) {
                    ctrl = tmp[1];
                } else if ( 'a' === tmp[0] ) {
                    action = tmp[1];
                } else {
                    keys[ tmp[0] ] = tmp[1];
                }
            }
        }

        // If you come fresh on this page an are not logged in
        if ( !ctrl && !action && !TB.Client.isLoggedIn() ) {
            return {
                'c' : 'Auth',
                'a' : 'login'
            };
        } else {
            return {
                'c' : ctrl || 'News',
                'a' : action || 'index',
                'params' : keys
            };
        }
    },

    do : function( ctrl, action, params) {
        var a = (action && action.toLowerCase() ) || 'index',
            c = TB.Utils.ucfirst( ctrl ) || 'News';

        // Login check
        if ( c != 'Auth' && false == TB.Client.isLoggedIn() ) {
            c = 'Auth';
            a = 'login';
            // @todo Save original controller+action for redirectioning after successful login
        }

        this.updateHash( c, a, params );
    },

    updateHash : function( ctrl, action, params ) {
        window.location.hash =  this.getUrl( ctrl, action, params );
    },

    getUrl : function ( ctrl, action, params ) {
        var args = null, hash = "", ctrl = ctrl || 'News', action = action || 'index';
        if ( params ) {
            args = Object.keys( params ).map(function(key) {
                return [key, params[key]].map(encodeURIComponent).join("=");
            }).join("&");
        }
        hash = '#/c=' + ctrl + '&a=' + action;
        if ( args ) {
            hash += '&' + args;
        }

        return hash;
    }
};