(function(context,$) {

    "use strict";

    context.JK = context.JK || {};
    context.JK.HomeScreen = function(app) {
        var logger = context.JK.logger;
        var isFtueComplete = false;

        function beforeShow(data) {
            refreshDeviceState();
        }

        function refreshDeviceState() {
            isFtueComplete = checkDeviceState();
            updateTiles();
        }

        function checkDeviceState() {
            var devices = context.jamClient.TrackGetDevices();
            return Object.keys(devices).length > 0;
        }

        function mouseenterTile() {
            $(this).addClass('hover');
        }

        function mouseleaveTile() {
            $(this).removeClass('hover');
        }

        function switchClientMode(e) {
            // ctrl + shift + 0
            if(e.ctrlKey && e.shiftKey && e.keyCode == 48) {
                console.log("switch client mode!");
                var act_as_native_client = $.cookie('act_as_native_client');

                console.log("currently: " + act_as_native_client);
                if(act_as_native_client == null || act_as_native_client != "true") {
                    console.log("forcing act as native client!");
                    $.cookie('act_as_native_client', 'true', { expires: 120, path: '/' });
                }
                else {
                    console.log("remove act as native client!");
                    $.removeCookie('act_as_native_client');
                }
                window.location.reload();
            }
        }

        function userHasDevices() {
            var $createSession = $('div[type="createSession"]');
            var $findSession = $('div[type="findSession"]');

            $createSession.attr('layout-link', 'createSession');
            $findSession.attr('layout-link', 'findSession');

            // undo any earlier disabling
            $('h2', $createSession).removeClass('disabled');
            $('h2', $findSession).removeClass('disabled');
            $createSession.removeClass('createsession-disabled');
            $createSession.addClass('createsession');
            $findSession.removeClass('findsession-disabled');
            $findSession.addClass('findsession');

            // and enable features
            $($createSession).on('mouseenter', mouseenterTile);
            $($createSession).on('mouseleave', mouseleaveTile);
            $($findSession).on('mouseenter', mouseenterTile);
            $($findSession).on('mouseleave', mouseleaveTile);
        }

        function userHasNoDevices() {

            var $createSession = $('div[type="createSession"]');
            var $findSession = $('div[type="findSession"]');

            $createSession.removeAttr('layout-link');
            $findSession.removeAttr('layout-link');

            // undo any earlier enabling
            $($createSession).off('mouseenter', mouseenterTile);
            $($createSession).off('mouseleave', mouseleaveTile);

            $($findSession).off('mouseenter', mouseenterTile);
            $($findSession).off('mouseleave', mouseleaveTile);

            // disable Create Session and Find Session tiles
            $('h2', $createSession).addClass('disabled');
            $('h2', $findSession).addClass('disabled');
            $createSession.addClass('createsession-disabled');
            $createSession.removeClass('createsession');
            $findSession.addClass('findsession-disabled');
            $findSession.removeClass('findsession');
        }

        // used to initialize things that do not have to be touched up in the same UI sessiion
        function events() {
            // initialize profile, feed and account tiles normally
            $('.homecard.profile, .homecard.feed, .homecard.account').on('mouseenter', mouseenterTile);
            $('.homecard.profile, .homecard.feed, .homecard.account').on('mouseleave', mouseleaveTile);
            $('div[layout-id=ftue]').on("ftue_success", refreshDeviceState);
            if(gon.allow_force_native_client) {
                $('body').on('keyup', switchClientMode);
            }
        }

        function updateTiles() {
            logger.debug("isFtueComplete=" + isFtueComplete);

            if(isFtueComplete) {
                userHasDevices();
            }
            else {
                userHasNoDevices();
            }

            $('.profile').on('click', function() {
                context.location = '#/profile/' + context.JK.currentUserId;
            });
        }


        this.initialize = function() {
            var screenBindings = { 'beforeShow': beforeShow };
            app.bindScreen('home', screenBindings);
            events();


        };

        this.beforeShow = beforeShow;
    };

 })(window,jQuery);