 endpointBase = "http://api.c4w.jp/api/v1/"
+getEndpoint = (key) -> "#{endpointBase}#{key}.json"
 
 app = angular.module 'disaster-information-client', [
     'pascalprecht.translate'
-    #'ngSanitize'
+    'ngSanitize'
 ]
 
 



app.run [
    '$http'
    '$rootScope'
    ($http, $rootScope) ->
        getEndpoint = (key) -> "#{endpointBase}#{key}.json"
        # method = 'GET'
        # url = getEndpoint('locale')
        # $http { method, url }
        #     .then (res) ->
        #         locales = res.data
        #
        #         $rootScope.locales = locales
        locales = ['ja', 'en'] # [mocks] locale should be obtained by ajax
        $rootScope.locales = locales
        $rootScope.selectedLocale = locales[0]

        $rootScope.languageLabel = {
            ja: '日本語'
            en: 'English'
        }

        $rootScope.entries = []

        promises = []

        locales.forEach (locale) ->

            promises.push ->
                $http { Method: 'GET', url: getEndpoint locale } #[bugs] need to do this with Promise.all and emit 'loaded' event
                    .then ({data}) ->

                        desc = (a, b) -> Date.parse(b.date) - Date.parse(a.date)
                        data.entries
                            .sort desc
                            .forEach (entry) ->
                                rawDate = Date.parse(entry.date);
                                entry.date = new Date(rawDate).toLocaleDateString()
                                entry.time = new Date(rawDate).toLocaleTimeString()
                                entry.body = entry.body # need sanitization?
                                entry.locale = locale
                                $rootScope.entries.push data.entries
                        # .shim always refer selected locale
                        if locale is $rootScope.selectedLocale
                            $rootScope.entries.shim = $rootScope.entries.filter (entry) ->
                                entry.locale = locale

        Promise.all promises
            .then ->
                $rootScope.
]


app.service 'route', [

        $rootScope.$watch ->
            $location.path()
        , (aa) ->
            console.log $location.path()
            console.log aa


        return {
