'use strict';

(function() {
    
     angular
        .module('simpleArmoryApp')
        .factory('BlizzardRealmService', BlizzardRealmService);

    function BlizzardRealmService($http, $q, $log, $window) {

        return {

            getAllRealms: function() {
                var defer = $q.defer();
                
                $q.all([
                    $http.get(window.location.protocol + '//cdn.simplearmory.com/json/servers.us.json').then(function(data) {

                        // initial fetch for us looks good.  TODO: if our update ever goes bad, we'll need more checks here
                        console.log('Got realms for US');
                        return data;
                    }, function(reason) {
                        
                        // had some problems loading data from cdn.  lets load the local copy
                        $window.ga('send', 'event', 'CDNFailure', 'US');
                        console.log('Failed to get realms for US: ' + reason.status + ' ' + reason.statusText);
                        return $http.get('data/servers.us.json');
                    }),
                    $http.get(window.location.protocol + '//cdn.simplearmory.com/json/servers.eu.json').then(function(data) {
                        console.log('Got realms for EU');

                        // todo: verify data is good

                        return data;
                    }, function(reason) {

                       // had some problems loading data from cdn.  lets load the local copy
                       $window.ga('send', 'event', 'CDNFailure', 'EU');
                       console.log('Failed to get realms for EU: ' + reason.status + ' ' + reason.statusText);
                       return $http.get('data/servers.eu.json'); 
                    }),
                ]).then(function(data) {

                    // combine results here so that its clean to the callers
                    var allServers = [];
                    var server;

                    // US
                    for (var i = 0; i < data[0].data.realms.length; i++) {
                        server = data[0].data.realms[i];

                        // tag region
                        server.region = 'us';

                        allServers.push(server);
                    }

                    // EU
                    for (i = 0; i < data[1].data.realms.length; i++) {
                        server = data[1].data.realms[i];

                        // tag region
                        server.region = 'eu';

                        allServers.push(server);
                    }

                    defer.resolve(allServers);
                });

                return defer.promise;
            }
        };
    }

})();
@@ -376,1420 +376,343 @@
 get(
-window.location.protocol + '//cdn.simplearmory.com/json/servers.us.json').then(function(data) %7B%0A%0A                        // initial fetch for us looks good.  TODO: if our update ever goes bad, we'll need more checks here%0A                        console.log('Got realms for US');%0A                        return data;%0A                    %7D, function(reason) %7B%0A                        %0A                        // had some problems loading data from cdn.  lets load the local copy%0A                        $window.ga('send', 'event', 'CDNFailure', 'US');%0A                        console.log('Failed to get realms for US: ' + reason.status + ' ' + reason.statusText);%0A                        return $http.get('data/servers.us.json');%0A                    %7D),%0A                    $http.get(window.location.protocol + '//cdn.simplearmory.com/json/servers.eu.json').then(function(data) %7B%0A                        console.log('Got realms for EU');%0A%0A                        // todo: verify data is good%0A%0A                        return data;%0A                    %7D, function(reason) %7B%0A%0A                       // had some problems loading data from cdn.  lets load the local copy%0A                       $window.ga('send', 'event', 'CDNFailure', 'EU');%0A                       console.log('Failed to get realms for EU: ' + reason.status + ' ' + reason.statusText);%0A                       return $http.get('data/servers.eu.json'); 
+'data/servers.us.json').then(function(data) %7B%0A                        console.log('Got realms for US');%0A                        return data;%0A                    %7D),%0A                    $http.get('data/servers.eu.json').then(function(data) %7B%0A                        console.log('Got realms for EU');%0A                        return data;
 %0A   

remove cdn and instead always use data from repo